TYPO3 CMS  TYPO3_6-2
TagBuilder.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
19 class TagBuilder {
20 
26  protected $tagName = '';
27 
33  protected $content = '';
34 
40  protected $attributes = array();
41 
48  protected $forceClosingTag = FALSE;
49 
57  public function __construct($tagName = '', $tagContent = '') {
58  $this->setTagName($tagName);
59  $this->setContent($tagContent);
60  }
61 
69  public function setTagName($tagName) {
70  $this->tagName = $tagName;
71  }
72 
79  public function getTagName() {
80  return $this->tagName;
81  }
82 
90  public function setContent($tagContent) {
91  $this->content = $tagContent;
92  }
93 
100  public function getContent() {
101  return $this->content;
102  }
103 
110  public function hasContent() {
111  if ($this->content === NULL) {
112  return FALSE;
113  }
114  return $this->content !== '';
115  }
116 
126  }
127 
135  public function hasAttribute($attributeName) {
136  return array_key_exists($attributeName, $this->attributes);
137  }
138 
146  public function getAttribute($attributeName) {
147  if (!$this->hasAttribute($attributeName)) {
148  return NULL;
149  }
150  return $this->attributes[$attributeName];
151  }
152 
159  public function getAttributes() {
160  return $this->attributes;
161  }
162 
172  public function addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters = TRUE) {
173  if ($escapeSpecialCharacters) {
174  $attributeValue = htmlspecialchars($attributeValue);
175  }
176  $this->attributes[$attributeName] = $attributeValue;
177  }
178 
187  public function addAttributes(array $attributes, $escapeSpecialCharacters = TRUE) {
188  foreach ($attributes as $attributeName => $attributeValue) {
189  $this->addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters);
190  }
191  }
192 
200  public function removeAttribute($attributeName) {
201  unset($this->attributes[$attributeName]);
202  }
203 
210  public function reset() {
211  $this->tagName = '';
212  $this->content = '';
213  $this->attributes = array();
214  $this->forceClosingTag = FALSE;
215  }
216 
223  public function render() {
224  if (empty($this->tagName)) {
225  return '';
226  }
227  $output = '<' . $this->tagName;
228  foreach ($this->attributes as $attributeName => $attributeValue) {
229  $output .= ' ' . $attributeName . '="' . $attributeValue . '"';
230  }
231  if ($this->hasContent() || $this->forceClosingTag) {
232  $output .= '>' . $this->content . '</' . $this->tagName . '>';
233  } else {
234  $output .= ' />';
235  }
236  return $output;
237  }
238 }
addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters=TRUE)
Definition: TagBuilder.php:172
__construct($tagName='', $tagContent='')
Definition: TagBuilder.php:57
addAttributes(array $attributes, $escapeSpecialCharacters=TRUE)
Definition: TagBuilder.php:187