Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
buttonattributes.php
1<?php
2
3namespace Bitrix\UI\Buttons;
4
7
8final class ButtonAttributes implements \ArrayAccess, \IteratorAggregate, \Countable, Arrayable
9{
10 const JSON_OPTIONS_DATA_ATTR = 'data-json-options';
11
13 private $attributes = [];
15 private $dataAttributes = [];
16
17 public function __construct(array $attributes = [])
18 {
19 $this->setAttributes($attributes);
20 }
21
22 public function setAttributes(array $attributes)
23 {
24 list($this->dataAttributes, $this->attributes) = self::splitDataAttributesAndAnother($attributes);
25
26 return $this;
27 }
28
35 public function getAttribute($name, $defaultValue = null)
36 {
37 return isset($this[$name])? $this[$name] : $defaultValue;
38 }
39
46 public function addDataAttribute($name, $value = null)
47 {
48 $this[self::addDataPrefix($name)] = $value;
49
50 return $this;
51 }
52
59 public function getDataAttribute($name, $defaultValue = null)
60 {
61 return $this->getAttribute(self::addDataPrefix($name), $defaultValue);
62 }
63
67 public function getDataAttributes()
68 {
69 return $this->dataAttributes;
70 }
71
72 public function addJsonOption($key, $value)
73 {
74 if (!isset($this[self::JSON_OPTIONS_DATA_ATTR]))
75 {
77 }
78
79 $this[self::JSON_OPTIONS_DATA_ATTR][$key] = $value;
80
81 return $this;
82 }
83
84 public function removeJsonOption($key)
85 {
86 unset($this[self::JSON_OPTIONS_DATA_ATTR][$key]);
87
88 return $this;
89 }
90
91 public function setJsonOptions(array $options)
92 {
93 $this[self::JSON_OPTIONS_DATA_ATTR] = $options;
94
95 return $this;
96 }
97
98 public function getJsonOptions()
99 {
100 return isset($this[self::JSON_OPTIONS_DATA_ATTR])? $this[self::JSON_OPTIONS_DATA_ATTR] : null;
101 }
102
108 public function addClass($className)
109 {
110 if (!isset($this['class']))
111 {
112 $this['class'] = [];
113 }
114
115 if (!in_array($className, $this['class'], true))
116 {
117 $this['class'][] = $className;
118 }
119
120 return $this;
121 }
122
128 public function hasClass($className)
129 {
130 return isset($this['class']) && in_array($className, $this['class'], true);
131 }
132
133 public function setClassList(array $classList)
134 {
135 $this['class'] = $classList;
136
137 return $this;
138 }
139
145 public function removeClass($className)
146 {
147 if (!isset($this['class']))
148 {
149 $this['class'] = [];
150 }
151
152 $index = array_search($className, $this['class'], true);
153 if ($index !== false)
154 {
155 unset($this['class'][$index]);
156 }
157
158 return $this;
159 }
160
168 public function getIterator()
169 {
170 return new \ArrayIterator($this->toArray());
171 }
172
187 public function offsetExists($offset)
188 {
189 $offset = mb_strtolower($offset);
190
191 $asAttribute = isset($this->attributes[$offset]) || array_key_exists($offset, $this->attributes);
192 if ($asAttribute)
193 {
194 return true;
195 }
196
197 if (!self::hasDataPrefix($offset))
198 {
199 return false;
200 }
201
202 $offset = self::deleteDataPrefix($offset);
203
204 return isset($this->dataAttributes[$offset]) || array_key_exists($offset, $this->dataAttributes);
205 }
206
218 public function &offsetGet($offset)
219 {
220 $offset = mb_strtolower($offset);
221 if (isset($this->attributes[$offset]) || array_key_exists($offset, $this->attributes))
222 {
223 return $this->attributes[$offset];
224 }
225
226 if (!self::hasDataPrefix($offset))
227 {
228 return null;
229 }
230
231 $offset = self::deleteDataPrefix($offset);
232 if (isset($this->dataAttributes[$offset]) || array_key_exists($offset, $this->dataAttributes))
233 {
234 return $this->dataAttributes[$offset];
235 }
236
237 return null;
238 }
239
254 public function offsetSet($offset, $value)
255 {
256 if($offset === null)
257 {
258 $this->attributes[] = $value;
259 }
260 else
261 {
262 $offset = mb_strtolower($offset);
263 if (self::hasDataPrefix($offset))
264 {
265 $this->dataAttributes[self::deleteDataPrefix($offset)] = $value;
266 }
267 else
268 {
269 $this->attributes[$offset] = $value;
270 }
271 }
272 }
273
285 public function offsetUnset($offset)
286 {
287 $offset = mb_strtolower($offset);
288 if (isset($this->attributes[$offset]) || array_key_exists($offset, $this->attributes))
289 {
290 unset($this->attributes[$offset]);
291
292 return;
293 }
294
295 if (!self::hasDataPrefix($offset))
296 {
297 return null;
298 }
299
300 $offset = self::deleteDataPrefix($offset);
301 if (isset($this->dataAttributes[$offset]) || array_key_exists($offset, $this->dataAttributes))
302 {
303 unset($this->dataAttributes[$offset]);
304 }
305 }
306
316 public function count()
317 {
318 return count($this->dataAttributes) + count($this->attributes);
319 }
320
324 public function toString()
325 {
326 return (string)$this;
327 }
328
332 public function __toString()
333 {
334 $string = '';
335 foreach ($this as $key => $value)
336 {
337 if (is_int($key))
338 {
339 $string .= "{$value} ";
340 }
341 else
342 {
343 if ($key === 'class')
344 {
345 $value = self::convertClassesToString($value);
346 }
347 elseif ($key === 'style')
348 {
349 $value = self::convertStylesToString($value);
350 }
351 elseif ($key === self::JSON_OPTIONS_DATA_ATTR)
352 {
353 $value = Json::encode($this->getJsonOptions());
354 }
355
356 $value = htmlspecialcharsbx($value);
357 $string .= "{$key}=\"{$value}\" ";
358 }
359 }
360
361 return $string;
362 }
363
364 protected static function convertClassesToString($classes)
365 {
366 if (is_string($classes))
367 {
368 return $classes;
369 }
370
371 return implode(' ', $classes);
372 }
373
374 protected static function convertStylesToString($styles)
375 {
376 if (is_string($styles))
377 {
378 return $styles;
379 }
380
381 $string = '';
382 foreach ($styles as $name => $value)
383 {
384 $string .= "{$name}:{$value};";
385 }
386
387 return $string;
388 }
389
393 public function toArray()
394 {
395 return array_merge(
396 $this->attributes,
397 self::convertDataAttributesToAttributes($this->dataAttributes)
398 );
399 }
400
401 protected static function addDataPrefix($name)
402 {
403 return "data-{$name}";
404 }
405
406 protected static function hasDataPrefix($name)
407 {
408 return is_string($name) && mb_substr($name, 0, 5) === 'data-';
409 }
410
411 protected static function deleteDataPrefix($name)
412 {
413 if (self::hasDataPrefix($name))
414 {
415 return mb_substr($name, 5);
416 }
417
418 return $name;
419 }
420
421 protected static function convertDataAttributesToAttributes(array $dataAttributes)
422 {
423 $attributes = [];
424 foreach ($dataAttributes as $name => $attribute)
425 {
426 $attributes[self::addDataPrefix($name)] = $attribute;
427 }
428
429 return $attributes;
430 }
431
432 protected static function splitDataAttributesAndAnother(array $attributes)
433 {
434 $anotherAttributes = $dataAttributes = [];
435 foreach ($attributes as $name => $attribute)
436 {
437 $name = mb_strtolower($name);
438 if (self::hasDataPrefix($name))
439 {
440 $dataAttributes[mb_substr($name, 5)] = $attribute;
441 }
442 else
443 {
444 $anotherAttributes[$name] = $attribute;
445 }
446 }
447
448 return [$dataAttributes, $anotherAttributes];
449 }
450}
getDataAttribute($name, $defaultValue=null)
static convertDataAttributesToAttributes(array $dataAttributes)
getAttribute($name, $defaultValue=null)
static splitDataAttributesAndAnother(array $attributes)