TYPO3  7.6
TagBuilder.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\ViewHelper;
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 
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  {
59  $this->setTagName($tagName);
60  $this->setContent($tagContent);
61  }
62 
70  public function setTagName($tagName)
71  {
72  $this->tagName = $tagName;
73  }
74 
81  public function getTagName()
82  {
83  return $this->tagName;
84  }
85 
93  public function setContent($tagContent)
94  {
95  $this->content = $tagContent;
96  }
97 
104  public function getContent()
105  {
106  return $this->content;
107  }
108 
115  public function hasContent()
116  {
117  if ($this->content === null) {
118  return false;
119  }
120  return $this->content !== '';
121  }
122 
131  {
133  }
134 
142  public function hasAttribute($attributeName)
143  {
144  return array_key_exists($attributeName, $this->attributes);
145  }
146 
154  public function getAttribute($attributeName)
155  {
156  if (!$this->hasAttribute($attributeName)) {
157  return null;
158  }
159  return $this->attributes[$attributeName];
160  }
161 
168  public function getAttributes()
169  {
170  return $this->attributes;
171  }
172 
182  public function addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters = true)
183  {
184  if ($escapeSpecialCharacters) {
185  $attributeValue = htmlspecialchars($attributeValue);
186  }
187  $this->attributes[$attributeName] = $attributeValue;
188  }
189 
198  public function addAttributes(array $attributes, $escapeSpecialCharacters = true)
199  {
200  foreach ($attributes as $attributeName => $attributeValue) {
201  $this->addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters);
202  }
203  }
204 
212  public function removeAttribute($attributeName)
213  {
214  unset($this->attributes[$attributeName]);
215  }
216 
223  public function reset()
224  {
225  $this->tagName = '';
226  $this->content = '';
227  $this->attributes = array();
228  $this->forceClosingTag = false;
229  }
230 
237  public function render()
238  {
239  if (empty($this->tagName)) {
240  return '';
241  }
242  $output = '<' . $this->tagName;
243  foreach ($this->attributes as $attributeName => $attributeValue) {
244  $output .= ' ' . $attributeName . '="' . $attributeValue . '"';
245  }
246  if ($this->hasContent() || $this->forceClosingTag) {
247  $output .= '>' . $this->content . '</' . $this->tagName . '>';
248  } else {
249  $output .= ' />';
250  }
251  return $output;
252  }
253 }