file Attributes.php

Namespace

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

Classes

Name Description
Attributes Class to help modify attributes.