4 * Contains \Drupal\bootstrap\Utility\Attributes.
7 namespace Drupal\bootstrap\Utility;
10 * Class to help modify attributes.
14 class Attributes extends ArrayObject {
19 public function __construct(array &$array = []) {
20 $this->array = &$array;
24 * Add class(es) to the array.
26 * @param string|array $class
27 * An individual class or an array of classes to add.
29 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
31 public function addClass($class) {
32 $classes = &$this->getClasses();
33 $classes = array_unique(array_merge($classes, (array) $class));
37 * Retrieve a specific attribute from the array.
40 * The specific attribute to retrieve.
41 * @param mixed $default
42 * (optional) The default value to set if the attribute does not exist.
45 * A specific attribute value, passed by reference.
47 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
49 public function &getAttribute($name, $default = NULL) {
50 return $this->offsetGet($name, $default);
54 * Retrieves classes from the array.
57 * The classes array, passed by reference.
59 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
61 public function &getClasses() {
62 $classes = &$this->offsetGet('class', []);
63 $classes = array_unique($classes);
68 * Indicates whether a specific attribute is set.
71 * The attribute to search for.
76 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetExists()
78 public function hasAttribute($name) {
79 return $this->offsetExists($name);
83 * Indicates whether a class is present in the array.
85 * @param string|array $class
86 * The class or array of classes to search for.
88 * Flag determining to check if all classes are present.
93 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
95 public function hasClass($class, $all = FALSE) {
96 $classes = (array) $class;
97 $result = array_intersect($classes, $this->getClasses());
98 return $all ? $result && count($classes) === count($result) : !!$result;
102 * Removes an attribute from the array.
104 * @param string|array $name
105 * The name of the attribute to remove.
107 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetUnset()
109 public function removeAttribute($name) {
110 $this->offsetUnset($name);
114 * Removes a class from the attributes array.
116 * @param string|array $class
117 * An individual class or an array of classes to remove.
119 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
121 public function removeClass($class) {
122 $classes = &$this->getClasses();
123 $classes = array_values(array_diff($classes, (array) $class));
127 * Replaces a class in the attributes array.
130 * The old class to remove.
132 * The new class. It will not be added if the $old class does not exist.
134 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
136 public function replaceClass($old, $new) {
137 $classes = &$this->getClasses();
138 $key = array_search($old, $classes);
139 if ($key !== FALSE) {
140 $classes[$key] = $new;
145 * Sets an attribute on the array.
147 * @param string $name
148 * The name of the attribute to set.
149 * @param mixed $value
150 * The value of the attribute to set.
152 * @see \Drupal\bootstrap\Utility\ArrayObject::offsetSet()
154 public function setAttribute($name, $value) {
155 $this->offsetSet($name, $value);
159 * Sets multiple attributes on the array.
161 * @param array $values
162 * An associative key/value array of attributes to set.
164 * @see \Drupal\bootstrap\Utility\ArrayObject::merge()
166 public function setAttributes(array $values) {
167 $this->merge($values);