3 namespace Drupal\bootstrap\Utility;
6 * Class to help modify attributes.
10 class Attributes extends ArrayObject {
15 public function __construct(array &$array = []) {
16 $this->array = &$array;
20 * Add class(es) to the array.
22 * @param string|array $class
23 * An individual class or an array of classes to add.
25 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
27 public function addClass($class) {
28 $classes = &$this->getClasses();
29 $classes = array_unique(array_merge($classes, (array) $class));
33 * Retrieve a specific attribute from the array.
36 * The specific attribute to retrieve.
37 * @param mixed $default
38 * (optional) The default value to set if the attribute does not exist.
41 * A specific attribute value, passed by reference.
43 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
45 public function &getAttribute($name, $default = NULL) {
46 return $this->offsetGet($name, $default);
50 * Retrieves classes from the array.
53 * The classes array, passed by reference.
55 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
57 public function &getClasses() {
58 $classes = &$this->offsetGet('class', []);
59 $classes = array_unique($classes);
64 * Indicates whether a specific attribute is set.
67 * The attribute to search for.
72 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetExists()
74 public function hasAttribute($name) {
75 return $this->offsetExists($name);
79 * Indicates whether a class is present in the array.
81 * @param string|array $class
82 * The class or array of classes to search for.
84 * Flag determining to check if all classes are present.
89 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
91 public function hasClass($class, $all = FALSE) {
92 $classes = (array) $class;
93 $result = array_intersect($classes, $this->getClasses());
94 return $all ? $result && count($classes) === count($result) : !!$result;
98 * Removes an attribute from the array.
100 * @param string|array $name
101 * The name of the attribute to remove.
103 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetUnset()
105 public function removeAttribute($name) {
106 $this->offsetUnset($name);
110 * Removes a class from the attributes array.
112 * @param string|array $class
113 * An individual class or an array of classes to remove.
115 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
117 public function removeClass($class) {
118 $classes = &$this->getClasses();
119 $classes = array_values(array_diff($classes, (array) $class));
123 * Replaces a class in the attributes array.
126 * The old class to remove.
128 * The new class. It will not be added if the $old class does not exist.
130 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
132 public function replaceClass($old, $new) {
133 $classes = &$this->getClasses();
134 $key = array_search($old, $classes);
135 if ($key !== FALSE) {
136 $classes[$key] = $new;
141 * Sets an attribute on the array.
143 * @param string $name
144 * The name of the attribute to set.
145 * @param mixed $value
146 * The value of the attribute to set.
148 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetSet()
150 public function setAttribute($name, $value) {
151 // Handle class attribute differently.
152 if ($name === 'class') {
153 $this->removeAttribute('class');
154 $this->addClass($value);
157 $this->offsetSet($name, $value);
162 * Sets multiple attributes on the array.
164 * @param array $values
165 * An associative key/value array of attributes to set.
167 * @see \Drupal\bootstrap\Utility\ArrayObject::merge()
169 public function setAttributes(array $values) {
170 // Handle class attribute differently.
171 $classes = isset($values['class']) ? $values['class'] : [];
172 unset($values['class']);
174 $this->addClass($classes);
177 // Merge the reset of the attributes.
178 $this->merge($values);