b6032989c5db664fabd1f34b303709f9bc004fd2
[yaffs-website] / web / themes / contrib / bootstrap / src / Utility / Attributes.php
1 <?php
2
3 namespace Drupal\bootstrap\Utility;
4
5 /**
6  * Class to help modify attributes.
7  *
8  * @ingroup utility
9  */
10 class Attributes extends ArrayObject {
11
12   /**
13    * {@inheritdoc}
14    */
15   public function __construct(array &$array = []) {
16     $this->array = &$array;
17   }
18
19   /**
20    * Add class(es) to the array.
21    *
22    * @param string|array $class
23    *   An individual class or an array of classes to add.
24    *
25    * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
26    */
27   public function addClass($class) {
28     $classes = &$this->getClasses();
29     $classes = array_unique(array_merge($classes, (array) $class));
30   }
31
32   /**
33    * Retrieve a specific attribute from the array.
34    *
35    * @param string $name
36    *   The specific attribute to retrieve.
37    * @param mixed $default
38    *   (optional) The default value to set if the attribute does not exist.
39    *
40    * @return mixed
41    *   A specific attribute value, passed by reference.
42    *
43    * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
44    */
45   public function &getAttribute($name, $default = NULL) {
46     return $this->offsetGet($name, $default);
47   }
48
49   /**
50    * Retrieves classes from the array.
51    *
52    * @return array
53    *   The classes array, passed by reference.
54    *
55    * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
56    */
57   public function &getClasses() {
58     $classes = &$this->offsetGet('class', []);
59     $classes = array_unique($classes);
60     return $classes;
61   }
62
63   /**
64    * Indicates whether a specific attribute is set.
65    *
66    * @param string $name
67    *   The attribute to search for.
68    *
69    * @return bool
70    *   TRUE or FALSE
71    *
72    * @see \Drupal\bootstrap\Utility\ArrayObject::offsetExists()
73    */
74   public function hasAttribute($name) {
75     return $this->offsetExists($name);
76   }
77
78   /**
79    * Indicates whether a class is present in the array.
80    *
81    * @param string|array $class
82    *   The class or array of classes to search for.
83    * @param bool $all
84    *   Flag determining to check if all classes are present.
85    *
86    * @return bool
87    *   TRUE or FALSE
88    *
89    * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
90    */
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;
95   }
96
97   /**
98    * Removes an attribute from the array.
99    *
100    * @param string|array $name
101    *   The name of the attribute to remove.
102    *
103    * @see \Drupal\bootstrap\Utility\ArrayObject::offsetUnset()
104    */
105   public function removeAttribute($name) {
106     $this->offsetUnset($name);
107   }
108
109   /**
110    * Removes a class from the attributes array.
111    *
112    * @param string|array $class
113    *   An individual class or an array of classes to remove.
114    *
115    * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
116    */
117   public function removeClass($class) {
118     $classes = &$this->getClasses();
119     $classes = array_values(array_diff($classes, (array) $class));
120   }
121
122   /**
123    * Replaces a class in the attributes array.
124    *
125    * @param string $old
126    *   The old class to remove.
127    * @param string $new
128    *   The new class. It will not be added if the $old class does not exist.
129    *
130    * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
131    */
132   public function replaceClass($old, $new) {
133     $classes = &$this->getClasses();
134     $key = array_search($old, $classes);
135     if ($key !== FALSE) {
136       $classes[$key] = $new;
137     }
138   }
139
140   /**
141    * Sets an attribute on the array.
142    *
143    * @param string $name
144    *   The name of the attribute to set.
145    * @param mixed $value
146    *   The value of the attribute to set.
147    *
148    * @see \Drupal\bootstrap\Utility\ArrayObject::offsetSet()
149    */
150   public function setAttribute($name, $value) {
151     // Handle class attribute differently.
152     if ($name === 'class') {
153       $this->removeAttribute('class');
154       $this->addClass($value);
155     }
156     else {
157       $this->offsetSet($name, $value);
158     }
159   }
160
161   /**
162    * Sets multiple attributes on the array.
163    *
164    * @param array $values
165    *   An associative key/value array of attributes to set.
166    *
167    * @see \Drupal\bootstrap\Utility\ArrayObject::merge()
168    */
169   public function setAttributes(array $values) {
170     // Handle class attribute differently.
171     $classes = isset($values['class']) ? $values['class'] : [];
172     unset($values['class']);
173     if ($classes) {
174       $this->addClass($classes);
175     }
176
177     // Merge the reset of the attributes.
178     $this->merge($values);
179   }
180
181 }