|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Markup |
|
17 * @subpackage Parser |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Token.php 20277 2010-01-14 14:17:12Z kokx $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Markup_TokenList |
|
25 */ |
|
26 require_once 'Zend/Markup/TokenList.php'; |
|
27 |
|
28 /** |
|
29 * @category Zend |
|
30 * @package Zend_Markup |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_Markup_Token |
|
35 { |
|
36 const TYPE_NONE = 'none'; |
|
37 const TYPE_TAG = 'tag'; |
|
38 |
|
39 /** |
|
40 * Children of this token |
|
41 * |
|
42 * @var Zend_Markup_TokenList |
|
43 */ |
|
44 protected $_children; |
|
45 |
|
46 /** |
|
47 * The complete tag |
|
48 * |
|
49 * @var string |
|
50 */ |
|
51 protected $_tag; |
|
52 |
|
53 /** |
|
54 * The tag's type |
|
55 * |
|
56 * @var string |
|
57 */ |
|
58 protected $_type; |
|
59 |
|
60 /** |
|
61 * Tag name |
|
62 * |
|
63 * @var string |
|
64 */ |
|
65 protected $_name = ''; |
|
66 |
|
67 /** |
|
68 * Tag attributes |
|
69 * |
|
70 * @var array |
|
71 */ |
|
72 protected $_attributes = array(); |
|
73 |
|
74 /** |
|
75 * The used tag stopper (empty when none is found) |
|
76 * |
|
77 * @var string |
|
78 */ |
|
79 protected $_stopper = ''; |
|
80 |
|
81 /** |
|
82 * The parent token |
|
83 * |
|
84 * @var Zend_Markup_Token |
|
85 */ |
|
86 protected $_parent; |
|
87 |
|
88 |
|
89 /** |
|
90 * Construct the token |
|
91 * |
|
92 * @param string $tag |
|
93 * @param string $type |
|
94 * @param string $name |
|
95 * @param array $attributes |
|
96 * @param Zend_Markup_Token $parent |
|
97 * @return void |
|
98 */ |
|
99 public function __construct( |
|
100 $tag, |
|
101 $type, |
|
102 $name = '', |
|
103 array $attributes = array(), |
|
104 Zend_Markup_Token $parent = null |
|
105 ) { |
|
106 $this->_tag = $tag; |
|
107 $this->_type = $type; |
|
108 $this->_name = $name; |
|
109 $this->_attributes = $attributes; |
|
110 $this->_parent = $parent; |
|
111 } |
|
112 |
|
113 // accessors |
|
114 |
|
115 /** |
|
116 * Set the stopper |
|
117 * |
|
118 * @param string $stopper |
|
119 * @return Zend_Markup_Token |
|
120 */ |
|
121 public function setStopper($stopper) |
|
122 { |
|
123 $this->_stopper = $stopper; |
|
124 |
|
125 return $this; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Get the stopper |
|
130 * |
|
131 * @return string |
|
132 */ |
|
133 public function getStopper() |
|
134 { |
|
135 return $this->_stopper; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Get the token's name |
|
140 * |
|
141 * @return string |
|
142 */ |
|
143 public function getName() |
|
144 { |
|
145 return $this->_name; |
|
146 } |
|
147 |
|
148 /** |
|
149 * Get the token's type |
|
150 * |
|
151 * @return string |
|
152 */ |
|
153 public function getType() |
|
154 { |
|
155 return $this->_type; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Get the complete tag |
|
160 * |
|
161 * @return string |
|
162 */ |
|
163 public function getTag() |
|
164 { |
|
165 return $this->_tag; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Get an attribute |
|
170 * |
|
171 * @param string $name |
|
172 * |
|
173 * @return string |
|
174 */ |
|
175 public function getAttribute($name) |
|
176 { |
|
177 return isset($this->_attributes[$name]) ? $this->_attributes[$name] : null; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Check if the token has an attribute |
|
182 * |
|
183 * @param string $name |
|
184 * |
|
185 * @return bool |
|
186 */ |
|
187 public function hasAttribute($name) |
|
188 { |
|
189 return isset($this->_attributes[$name]); |
|
190 } |
|
191 |
|
192 /** |
|
193 * Get all the attributes |
|
194 * |
|
195 * @return array |
|
196 */ |
|
197 public function getAttributes() |
|
198 { |
|
199 return $this->_attributes; |
|
200 } |
|
201 |
|
202 /** |
|
203 * Add an attribute |
|
204 * |
|
205 * @return Zend_Markup_Token |
|
206 */ |
|
207 public function addAttribute($name, $value) |
|
208 { |
|
209 $this->_attributes[$name] = $value; |
|
210 |
|
211 return $this; |
|
212 } |
|
213 |
|
214 /** |
|
215 * Check if an attribute is empty |
|
216 * |
|
217 * @param string $name |
|
218 * |
|
219 * @return bool |
|
220 */ |
|
221 public function attributeIsEmpty($name) |
|
222 { |
|
223 return empty($this->_attributes[$name]); |
|
224 } |
|
225 |
|
226 // functions for child/parent tokens |
|
227 |
|
228 /** |
|
229 * Add a child token |
|
230 * |
|
231 * @return void |
|
232 */ |
|
233 public function addChild(Zend_Markup_Token $child) |
|
234 { |
|
235 $this->getChildren()->addChild($child); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Set the children token list |
|
240 * |
|
241 * @param Zend_Markup_TokenList $children |
|
242 * @return Zend_Markup_Token |
|
243 */ |
|
244 public function setChildren(Zend_Markup_TokenList $children) |
|
245 { |
|
246 $this->_children = $children; |
|
247 return $this; |
|
248 } |
|
249 |
|
250 /** |
|
251 * Get the children for this token |
|
252 * |
|
253 * @return Zend_Markup_TokenList |
|
254 */ |
|
255 public function getChildren() |
|
256 { |
|
257 if (null === $this->_children) { |
|
258 $this->setChildren(new Zend_Markup_TokenList()); |
|
259 } |
|
260 return $this->_children; |
|
261 } |
|
262 |
|
263 /** |
|
264 * Does this token have any children |
|
265 * |
|
266 * @return bool |
|
267 */ |
|
268 public function hasChildren() |
|
269 { |
|
270 return !empty($this->_children); |
|
271 } |
|
272 |
|
273 /** |
|
274 * Get the parent token (if any) |
|
275 * |
|
276 * @return Zend_Markup_Token |
|
277 */ |
|
278 public function getParent() |
|
279 { |
|
280 return $this->_parent; |
|
281 } |
|
282 |
|
283 /** |
|
284 * Set a parent token |
|
285 * |
|
286 * @param Zend_Markup_Token $parent |
|
287 * @return Zend_Markup_Token |
|
288 */ |
|
289 public function setParent(Zend_Markup_Token $parent) |
|
290 { |
|
291 $this->_parent = $parent; |
|
292 return $this; |
|
293 } |
|
294 |
|
295 /** |
|
296 * Magic clone function |
|
297 * |
|
298 * @return void |
|
299 */ |
|
300 public function __clone() |
|
301 { |
|
302 $this->_parent = null; |
|
303 $this->_children = null; |
|
304 $this->_tag = ''; |
|
305 } |
|
306 } |