root/trunk/XJConf/DefinedTag.php

Revision 121, 5.4 kB (checked in by mikey, 1 year ago)

whitespace fixes

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