Hoa central
Concrete.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Xyl\Interpreter\Html;
38 
39 use Hoa\Xyl;
40 
49 abstract class Concrete extends Xyl\Element\Concrete
50 {
59  protected static $_attributesMapping = …;
60 
66  protected $_htmlAttributes = [];
67 
73  protected $_htmlAttributesType = [];
74 
81  protected $_contentFlow = 2;
82 
83 
84 
90  public function construct()
91  {
92  parent::construct();
93  $this->mapAttributes();
94 
95  return;
96  }
97 
103  protected function mapAttributes()
104  {
105  $parent = get_called_class();
106  $mapped = [];
107 
108  do {
109  static::_mapAttributes($this, $parent, $mapped);
110  } while (__CLASS__ !== $parent = get_parent_class($parent));
111 
112  static::_mapAttributes($this, $parent, $mapped);
113 
114  return;
115  }
116 
126  private static function _mapAttributes($self, $parent, Array &$mapped)
127  {
128  if (!isset($parent::$_attributesMapping) ||
129  !isset($parent::$_attributes)) {
130  return;
131  }
132 
133  $map = $parent::$_attributesMapping;
134 
135  if (… === $map) {
136  $map = array_keys($parent::$_attributes);
137  }
138 
139  $map = array_diff($map, $mapped);
140 
141  foreach ($map as $from => $to) {
142  if (is_int($from)) {
143  $from = $to;
144  }
145 
146  $type = $parent::$_attributes[$from];
147  $self->_htmlAttributesType[$to] = $type;
148  $mapped[] = $from;
149 
150  if (self::ATTRIBUTE_TYPE_CUSTOM == $type) {
151  $values = $self->abstract->readCustomAttributes($from);
152 
153  foreach ($values as $name => $value) {
154  $self->writeAttribute($to . '-' . $name, $value);
155  }
156 
157  continue;
158  } elseif (null === $value = $self->abstract->readAttribute($from)) {
159  continue;
160  }
161 
162  $self->writeAttribute($to, $value);
163  }
164 
165  return;
166  }
167 
173  public function readAttributes()
174  {
175  return $this->_htmlAttributes;
176  }
177 
184  public function readAttribute($name)
185  {
186  $attributes = $this->readAttributes();
187 
188  if (false === array_key_exists($name, $attributes)) {
189  return null;
190  }
191 
192  return $attributes[$name];
193  }
194 
201  public function attributeExists($name)
202  {
203  return true === array_key_exists($name, $this->readAttributes());
204  }
205 
211  public function readAttributesAsList()
212  {
213  $attributes = $this->readAttributes();
214 
215  foreach ($attributes as $name => &$value) {
216  $value = explode(' ', $value);
217  }
218 
219  return $attributes;
220  }
221 
228  public function readAttributeAsList($name)
229  {
230  return explode(' ', $this->readAttribute($name));
231  }
232 
242  public function readCustomAttributes($set)
243  {
244  $out = [];
245  $set .= '-';
246  $strlen = strlen($set);
247 
248  foreach ($this->readAttributes() as $name => $value) {
249  if ($set === substr($name, 0, $strlen)) {
250  $out[substr($name, $strlen)] = $value;
251  }
252  }
253 
254  return $out;
255  }
256 
263  public function readCustomAttributesAsList($set)
264  {
265  $out = [];
266 
267  foreach ($this->readCustomAttributes($set) as $name => $value) {
268  $out[$name] = explode(' ', $value);
269  }
270 
271  return $out;
272  }
273 
282  public function readAttributesAsString($compute = true)
283  {
284  $out = null;
285 
286  foreach ($this->readAttributes() as $name => $value) {
287  if (null !== $value) {
288  $out .=
289  ' ' . $name . '="' .
290  str_replace(
291  '"',
292  '\"',
293  true === $compute
294  ? $this->computeAttributeValue(
295  $value,
296  isset($this->_htmlAttributesType[$name])
297  ? $this->_htmlAttributesType[$name]
298  : parent::ATTRIBUTE_TYPE_NORMAL,
299  $name
300  )
301  : $value
302  ) .
303  '"';
304  }
305  }
306 
307  return $out;
308  }
309 
317  public function writeAttributes(Array $attributes)
318  {
319  foreach ($attributes as $name => $value) {
320  $this->writeAttribute($name, $value);
321  }
322 
323  return;
324  }
325 
334  public function writeAttribute($name, $value)
335  {
336  $this->_htmlAttributes[$name] = $value;
337 
338  return;
339  }
340 
352  public function writeCustomAttributes($set, Array $pairs)
353  {
354  foreach ($pairs as $attribute => $value) {
355  $this->writeAttribute($set . '-' . $attribute, $value);
356  }
357 
358  return;
359  }
360 
367  public function removeAttribute($name)
368  {
369  unset($this->_htmlAttributes[$name]);
370 
371  return;
372  }
373 
379  public function getName()
380  {
381  return $this->abstract->getName();
382  }
383 }
readAttributesAsString($compute=true)
Definition: Concrete.php:282
writeAttributes(Array $attributes)
Definition: Concrete.php:317
static _mapAttributes($self, $parent, Array &$mapped)
Definition: Concrete.php:126
writeCustomAttributes($set, Array $pairs)
Definition: Concrete.php:352
computeAttributeValue($value, $type=self::ATTRIBUTE_TYPE_UNKNOWN, $name=null)
Definition: Concrete.php:488