root/tags/RELEASE_0_2_1/XJConf/DefinedTag.php

Revision 94, 4.6 kB (checked in by schst, 2 years ago)

Add the possibility to call setter methods with any number of arguments. This fixed ticket #9. But beware: this feature is in alpha stage, it may break existing applications.

Line 
1 <?php
2 /**
3  * Contains data of tag in the default namespace.
4  *
5  * @author  Stephan Schmidt <me@schst.net>
6  * @author  Frank Kleine <frank.kleine@schlund.de>
7  */
8 XJConfLoader::load('Tag',
9                    'definitions.TagDefinition'
10 );
11 /**
12  * Contains data of tag in the default namespace.
13  *
14  * @package  XJConf
15  */
16 class DefinedTag implements Tag
17 {
18
19     /**
20      * name of the tag
21      *
22      * @var  string
23      */
24     private $name = null;
25
26     /**
27      * character data
28      *
29      * @var  string
30      */
31     private $data = null;
32
33     /**
34      * content of the tag
35      *
36      * @var  mixed
37      */
38     private $content = null;
39
40     /**
41      * attributes of the tag
42      *
43      * @var  array
44      */
45     private $atts = array();
46
47     /**
48      * Children of the tag
49      *
50      * @var  array
51      */
52     private $children = array();
53
54     /**
55      * value of the tag
56      *
57      * @var  TagDefinition
58      */
59     private $tagDef = null;
60
61     /**
62      * Create a new tag without attributes
63      *
64      * @param name   name of the tag
65      */
66     public function __construct($name, $atts = array())
67     {
68         $this->name = $name;
69         $this->atts = $atts;
70     }
71
72     /**
73      * Get the name of the tag
74      *
75      * @return   name of the tag
76      */
77     public function getName()
78     {
79         return $this->name;
80     }
81
82     /**
83      * Set the key
84      *
85      * @param  string  $key
86      */
87     public function setKey($key)
88     {
89         $this->key = $key;
90     }
91
92     /**
93      * Get the key under which the value will be stored
94      *
95      * @return  string
96      */
97     public function getKey()
98     {
99         return $this->tagDef->getKey($this);
100     }
101
102     /**
103      * Add text data
104      *
105      * @param   string  $buf
106      * @return  int     new length of data
107      */
108     public function addData($buf)
109     {
110         $this->data .= $buf;
111         return strlen($this->data);
112     }
113
114     /**
115      * Get the character data of the tag
116      *
117      * @return   character data
118      */
119     public function getData()
120     {
121         return $this->data;
122     }
123
124     /**
125      * Check, whether the tag has a certain attribute
126      *
127      * @param   string   $name
128      * @return  boolean
129      */
130     public function hasAttribute($name)
131     {
132         return isset($this->atts[$name]);
133     }
134
135     /**
136      * get an attribute
137      *
138      * @param   string  $name  name of the attribute
139      * @return  string  value of the attribute
140      */
141     public function getAttribute($name)
142     {
143         if ($this->hasAttribute($name) == true) {
144             return $this->atts[$name];
145         }
146
147         return null;
148     }
149
150     /**
151      * get all attributes
152      *
153      * @return  array
154      */
155     public function getAttributes()
156     {
157         return $this->atts;
158     }
159
160     /**
161      * Add a new child to this tag.
162      *
163      * @param child  child to add
164      * @return   int    number of childs added
165      */
166     public function addChild(Tag $child)
167     {
168         array_push($this->children, $child);
169         return count($this->children);
170     }
171
172     /**
173      * Get the child with a specific name
174      *
175      * @param   string  $name
176      * @return  Tag
177      */
178     public function getChild($name)
179     {
180         foreach ($this->children as $child) {
181             if ($child->getName() == $name) {
182                 return $child;
183             }
184         }
185
186         return null;
187     }
188
189     /**
190      * Get all children of the tag
191      *
192      * @return  array
193      */
194     public function getChildren()
195     {
196         return $this->children;
197     }
198
199     /**
200      * Set the content (overrides the character data)
201      *
202      * @param  mixed  $content
203      */
204     public function setContent($content)
205     {
206         $this->content = $content;
207     }
208
209     /**
210      * Get the content
211      *
212      * @return  mixed
213      */
214     public function getContent()
215     {
216         if (null != $this->content) {
217             return $this->content;
218         }
219
220         return $this->getData();
221     }
222
223     /**
224      * Fetch the value
225      *
226      * @return    mixed  the value of the tag
227      */
228     public function getConvertedValue()
229     {
230         return $this->tagDef->convertValue($this);
231     }
232
233     /**
234      * Get the type of the value
235      *
236      * @param   Tag     $tag
237      * @return  string
238      */
239     public function getValueType(Tag $tag)
240     {
241         return $this->tagDef->getValueType($tag);
242     }
243
244     /**
245      * Get the setter method
246      *
247      * @return  string
248      */
249     public function getSetterMethod()
250     {
251         return $this->tagDef->getSetterMethod($this);
252     }
253
254     /**
255      * Checks, whether the tag supports indexed children
256      *
257      * @return  boolean
258      */
259     public function supportsIndexedChildren()
260     {
261         return $this->tagDef->supportsIndexedChildren();
262     }
263
264     /**
265     * Set the tag definition object used for this tag
266     *
267     * @param  TagDefinition  $tagDef
268     */
269     public function setDefinition(Definition $tagDef)
270     {
271         if ($tagDef instanceof ConcreteTagDefinition) {
272             $this->tagDef = $tagDef;
273             return;
274         }
275
276         $this->tagDef = clone $tagDef;
277         if ($tagDef instanceof TagDefinition) {
278             $this->tagDef->setType($this->getAttribute($this->tagDef->getConcreteTypeAttributeName()));
279         }
280     }
281
282     /**
283      * get the tag definition object used for this tag
284      *
285      * @return  TagDefinition
286      */
287     public function getDefinition()
288     {
289         return $this->tagDef;
290     }
291 }
292 ?>
Note: See TracBrowser for help on using the browser.