root/tags/RELEASE_0_1_2/XJConf/GenericTag.php

Revision 14, 4.3 kB (checked in by schst, 2 years ago)

Fixed DefinitionParser?, changed ValueConverter? interface to allow more flexible converters.
This is only a first draft and should probably be cleaned up.

Line 
1 <?php
2 /**
3  * Generic Tag wrapper that can be used by extensions to dynamically add
4  * children to other tags.
5  *
6  * @author Stephan Schmidt <me@schst.net>
7  */
8 XJConfLoader::load('Tag');
9 /**
10  * Generic Tag wrapper that can be used by extensions to dynamically add
11  * children to other tags.
12  *
13  * @package  XJConf
14  */
15 class GenericTag implements Tag
16 {
17     /**
18      * name of the tag
19      *
20      * @var  string
21      */
22     private $name     = null;
23     /**
24      * character data
25      *
26      * @var  string
27      */
28     private $data     = null;
29     /**
30      * content of the tag (overrides data)
31      *
32      * @var  mixed
33      */
34     private $content  = null;
35     /**
36      * attributes of the tag
37      *
38      * @var  array
39      */
40     private $atts     = array();
41     /**
42      * Children of the tag
43      *
44      * @var  array
45      */
46     private $children = array();
47     /**
48      * value of the tag
49      *
50      * @var  mixed
51      */
52     private $value    = null;
53     /**
54      * Key of the tag
55      *
56      * @var  string
57      */
58     private $key      = null;
59
60     /**
61      * Create a new tag with or without attributes
62      *
63      * @param  string $name  name of the tag
64      * @param  array  $atts  optional  list of attributes
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  string
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->key;
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  string
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   Tag  $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->value;
231     }
232
233     /**
234      * Get the type of the value
235      *
236      * @return  string
237      */
238     public function getValueType(Tag $tag)
239     {
240         if (null == $this->value) {
241             return null;
242         }
243
244         if (is_object($this->value) == true) {
245             return get_class($this->value);
246         }
247
248         return gettype($this->value);
249     }
250
251     /**
252      * Get the setter method
253      */
254     public function getSetterMethod()
255     {
256         if (null == $this->key) {
257             return null;
258         }
259         return 'set' . ucfirst($this->key);
260     }
261
262     /**
263      * Checks, whether the tag supports indexed children
264      *
265      * @return  boolean
266      */
267     public function supportsIndexedChildren()
268     {
269         return true;
270     }
271
272     /**
273      * Set the value of the tag
274      *
275      * @param  mixed
276      */
277     public function setValue($value)
278      {
279         $this->value = $value;
280     }
281 }
282 ?>
Note: See TracBrowser for help on using the browser.