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