Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Template;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * A class that defines a type of Attribute that can be added to as an array.
|
Chris@0
|
9 *
|
Chris@0
|
10 * To use with Attribute, the array must be specified.
|
Chris@0
|
11 * Correct:
|
Chris@0
|
12 * @code
|
Chris@0
|
13 * $attributes = new Attribute();
|
Chris@0
|
14 * $attributes['class'] = array();
|
Chris@0
|
15 * $attributes['class'][] = 'cat';
|
Chris@0
|
16 * @endcode
|
Chris@0
|
17 * Incorrect:
|
Chris@0
|
18 * @code
|
Chris@0
|
19 * $attributes = new Attribute();
|
Chris@0
|
20 * $attributes['class'][] = 'cat';
|
Chris@0
|
21 * @endcode
|
Chris@0
|
22 *
|
Chris@0
|
23 * @see \Drupal\Core\Template\Attribute
|
Chris@0
|
24 */
|
Chris@0
|
25 class AttributeArray extends AttributeValueBase implements \ArrayAccess, \IteratorAggregate {
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Ensures empty array as a result of array_filter will not print '$name=""'.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @see \Drupal\Core\Template\AttributeArray::__toString()
|
Chris@0
|
31 * @see \Drupal\Core\Template\AttributeValueBase::render()
|
Chris@0
|
32 */
|
Chris@0
|
33 const RENDER_EMPTY_ATTRIBUTE = FALSE;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function offsetGet($offset) {
|
Chris@0
|
39 return $this->value[$offset];
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function offsetSet($offset, $value) {
|
Chris@0
|
46 if (isset($offset)) {
|
Chris@0
|
47 $this->value[$offset] = $value;
|
Chris@0
|
48 }
|
Chris@0
|
49 else {
|
Chris@0
|
50 $this->value[] = $value;
|
Chris@0
|
51 }
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public function offsetUnset($offset) {
|
Chris@0
|
58 unset($this->value[$offset]);
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function offsetExists($offset) {
|
Chris@0
|
65 return isset($this->value[$offset]);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Implements the magic __toString() method.
|
Chris@0
|
70 */
|
Chris@0
|
71 public function __toString() {
|
Chris@0
|
72 // Filter out any empty values before printing.
|
Chris@0
|
73 $this->value = array_unique(array_filter($this->value));
|
Chris@0
|
74 return Html::escape(implode(' ', $this->value));
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * {@inheritdoc}
|
Chris@0
|
79 */
|
Chris@0
|
80 public function getIterator() {
|
Chris@0
|
81 return new \ArrayIterator($this->value);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Exchange the array for another one.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @see ArrayObject::exchangeArray
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param array $input
|
Chris@0
|
90 * The array input to replace the internal value.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return array
|
Chris@0
|
93 * The old array value.
|
Chris@0
|
94 */
|
Chris@0
|
95 public function exchangeArray($input) {
|
Chris@0
|
96 $old = $this->value;
|
Chris@0
|
97 $this->value = $input;
|
Chris@0
|
98 return $old;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|