Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Utility / DrupalAttributes.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Utility\DrupalAttributes.
5  */
6
7 namespace Drupal\bootstrap\Utility;
8
9 use Drupal\Core\Template\Attribute;
10
11 /**
12  * Class for managing multiple types of attributes commonly found in Drupal.
13  *
14  * @ingroup utility
15  *
16  * @see \Drupal\bootstrap\Utility\Attributes
17  * @see \Drupal\bootstrap\Utility\Element
18  * @see \Drupal\bootstrap\Utility\Variables
19  */
20 class DrupalAttributes extends ArrayObject {
21
22   /**
23    * Defines the "attributes" storage type constant.
24    *
25    * @var string
26    */
27   const ATTRIBUTES = 'attributes';
28
29   /**
30    * Defines the "body_attributes" storage type constant.
31    *
32    * @var string
33    */
34   const BODY = 'body_attributes';
35
36   /**
37    * Defines the "content_attributes" storage type constant.
38    *
39    * @var string
40    */
41   const CONTENT = 'content_attributes';
42
43   /**
44    * Defines the "description_attributes" storage type constant.
45    *
46    * @var string
47    */
48   const DESCRIPTION = 'description_attributes';
49
50   /**
51    * Defines the "footer_attributes" storage type constant.
52    *
53    * @var string
54    */
55   const FOOTER = 'footer_attributes';
56
57   /**
58    * Defines the "header_attributes" storage type constant.
59    *
60    * @var string
61    */
62   const HEADER = 'header_attributes';
63
64   /**
65    * Defines the "heading_attributes" storage type constant.
66    *
67    * @var string
68    */
69   const HEADING = 'heading_attributes';
70
71   /**
72    * Defines the "input_group_attributes" storage type constant.
73    *
74    * @var string
75    */
76   const INPUT_GROUP = 'input_group_attributes';
77
78   /**
79    * Defines the "label_attributes" storage type constant.
80    *
81    * @var string
82    */
83   const LABEL = 'label_attributes';
84
85   /**
86    * Defines the "navbar_attributes" storage type constant.
87    *
88    * @var string
89    */
90   const NAVBAR = 'navbar_attributes';
91
92   /**
93    * Defines the "split_button_attributes" storage type constant.
94    *
95    * @var string
96    */
97   const SPLIT_BUTTON = 'split_button_attributes';
98
99   /**
100    * Defines the "title_attributes" storage type constant.
101    *
102    * @var string
103    */
104   const TITLE = 'title_attributes';
105
106   /**
107    * Defines the "wrapper_attributes" storage type constant.
108    *
109    * @var string
110    */
111   const WRAPPER = 'wrapper_attributes';
112
113   /**
114    * Stored attribute instances.
115    *
116    * @var \Drupal\bootstrap\Utility\Attributes[]
117    */
118   protected $attributes = [];
119
120   /**
121    * A prefix to use for retrieving attribute keys from the array.
122    *
123    * @var string
124    */
125   protected $attributePrefix = '';
126
127   /**
128    * Add class(es) to an attributes object.
129    *
130    * This is a wrapper method to retrieve the correct attributes storage object
131    * and then add the class(es) to it.
132    *
133    * @param string|array $class
134    *   An individual class or an array of classes to add.
135    * @param string $type
136    *   (optional) The type of attributes to use for this method.
137    *
138    * @return $this
139    *
140    * @see \Drupal\bootstrap\Utility\Attributes::addClass()
141    */
142   public function addClass($class, $type = DrupalAttributes::ATTRIBUTES) {
143     $this->getAttributes($type)->addClass($class);
144     return $this;
145   }
146
147   /**
148    * Retrieve a specific attribute from an attributes object.
149    *
150    * This is a wrapper method to retrieve the correct attributes storage object
151    * and then retrieve the attribute from it.
152    *
153    * @param string $name
154    *   The specific attribute to retrieve.
155    * @param mixed $default
156    *   (optional) The default value to set if the attribute does not exist.
157    * @param string $type
158    *   (optional) The type of attributes to use for this method.
159    *
160    * @return mixed
161    *   A specific attribute value, passed by reference.
162    *
163    * @see \Drupal\bootstrap\Utility\Attributes::getAttribute()
164    */
165   public function &getAttribute($name, $default = NULL, $type = DrupalAttributes::ATTRIBUTES) {
166     return $this->getAttributes($type)->getAttribute($name, $default);
167   }
168
169   /**
170    * Retrieves a specific attributes object.
171    *
172    * @param string $type
173    *   (optional) The type of attributes to use for this method.
174    *
175    * @return \Drupal\bootstrap\Utility\Attributes
176    *   An attributes object for $type.
177    */
178   public function getAttributes($type = DrupalAttributes::ATTRIBUTES) {
179     if (!isset($this->attributes[$type])) {
180       $attributes = &$this->offsetGet($this->attributePrefix . $type, []);
181       if ($attributes instanceof Attribute) {
182         $attributes = $attributes->toArray();
183       }
184       $this->attributes[$type] = new Attributes($attributes);
185     }
186     return $this->attributes[$type];
187   }
188
189   /**
190    * Retrieves classes from an attributes object.
191    *
192    * This is a wrapper method to retrieve the correct attributes storage object
193    * and then retrieve the set classes from it.
194    *
195    * @param string $type
196    *   (optional) The type of attributes to use for this method.
197    *
198    * @return array
199    *   The classes array, passed by reference.
200    *
201    * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
202    */
203   public function &getClasses($type = DrupalAttributes::ATTRIBUTES) {
204     return $this->getAttributes($type)->getClasses();
205   }
206
207   /**
208    * Indicates whether an attributes object has a specific attribute set.
209    *
210    * This is a wrapper method to retrieve the correct attributes storage object
211    * and then check there if the attribute exists.
212    *
213    * @param string $name
214    *   The attribute to search for.
215    * @param string $type
216    *   (optional) The type of attributes to use for this method.
217    *
218    * @return bool
219    *   TRUE or FALSE
220    *
221    * @see \Drupal\bootstrap\Utility\Attributes::hasAttribute()
222    */
223   public function hasAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
224     return $this->getAttributes($type)->hasAttribute($name);
225   }
226
227   /**
228    * Indicates whether an attributes object has a specific class.
229    *
230    * This is a wrapper method to retrieve the correct attributes storage object
231    * and then check there if a class exists in the attributes object.
232    *
233    * @param string $class
234    *   The class to search for.
235    * @param string $type
236    *   (optional) The type of attributes to use for this method.
237    *
238    * @return bool
239    *   TRUE or FALSE
240    *
241    * @see \Drupal\bootstrap\Utility\Attributes::hasClass()
242    */
243   public function hasClass($class, $type = DrupalAttributes::ATTRIBUTES) {
244     return $this->getAttributes($type)->hasClass($class);
245   }
246
247   /**
248    * Removes an attribute from an attributes object.
249    *
250    * This is a wrapper method to retrieve the correct attributes storage object
251    * and then remove an attribute from it.
252    *
253    * @param string|array $name
254    *   The name of the attribute to remove.
255    * @param string $type
256    *   (optional) The type of attributes to use for this method.
257    *
258    * @return $this
259    *
260    * @see \Drupal\bootstrap\Utility\Attributes::removeAttribute()
261    */
262   public function removeAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
263     $this->getAttributes($type)->removeAttribute($name);
264     return $this;
265   }
266
267   /**
268    * Removes a class from an attributes object.
269    *
270    * This is a wrapper method to retrieve the correct attributes storage object
271    * and then remove the class(es) from it.
272    *
273    * @param string|array $class
274    *   An individual class or an array of classes to remove.
275    * @param string $type
276    *   (optional) The type of attributes to use for this method.
277    *
278    * @return $this
279    *
280    * @see \Drupal\bootstrap\Utility\Attributes::removeClass()
281    */
282   public function removeClass($class, $type = DrupalAttributes::ATTRIBUTES) {
283     $this->getAttributes($type)->removeClass($class);
284     return $this;
285   }
286
287   /**
288    * Replaces a class in an attributes object.
289    *
290    * This is a wrapper method to retrieve the correct attributes storage object
291    * and then replace the class(es) in it.
292    *
293    * @param string $old
294    *   The old class to remove.
295    * @param string $new
296    *   The new class. It will not be added if the $old class does not exist.
297    * @param string $type
298    *   (optional) The type of attributes to use for this method.
299    *
300    * @return $this
301    *
302    * @see \Drupal\bootstrap\Utility\Attributes::replaceClass()
303    */
304   public function replaceClass($old, $new, $type = DrupalAttributes::ATTRIBUTES) {
305     $this->getAttributes($type)->replaceClass($old, $new);
306     return $this;
307   }
308
309   /**
310    * Sets an attribute on an attributes object.
311    *
312    * This is a wrapper method to retrieve the correct attributes storage object
313    * and then set an attribute on it.
314    *
315    * @param string $name
316    *   The name of the attribute to set.
317    * @param mixed $value
318    *   The value of the attribute to set.
319    * @param string $type
320    *   (optional) The type of attributes to use for this method.
321    *
322    * @return $this
323    *
324    * @see \Drupal\bootstrap\Utility\Attributes::setAttribute()
325    */
326   public function setAttribute($name, $value, $type = DrupalAttributes::ATTRIBUTES) {
327     $this->getAttributes($type)->setAttribute($name, $value);
328     return $this;
329   }
330
331   /**
332    * Sets multiple attributes on an attributes object.
333    *
334    * This is a wrapper method to retrieve the correct attributes storage object
335    * and then merge multiple attributes into it.
336    *
337    * @param array $values
338    *   An associative key/value array of attributes to set.
339    * @param string $type
340    *   (optional) The type of attributes to use for this method.
341    *
342    * @return $this
343    *
344    * @see \Drupal\bootstrap\Utility\Attributes::setAttributes()
345    */
346   public function setAttributes(array $values, $type = DrupalAttributes::ATTRIBUTES) {
347     $this->getAttributes($type)->setAttributes($values);
348     return $this;
349   }
350
351
352 }