You are here

class Attributes in Express 8

Class to help modify attributes.

Hierarchy

Expanded class hierarchy of Attributes

File

themes/contrib/bootstrap/src/Utility/Attributes.php, line 14
Contains \Drupal\bootstrap\Utility\Attributes.

Namespace

Drupal\bootstrap\Utility
View source
class Attributes extends ArrayObject {

  /**
   * {@inheritdoc}
   */
  public function __construct(array &$array = []) {
    $this->array =& $array;
  }

  /**
   * Add class(es) to the array.
   *
   * @param string|array $class
   *   An individual class or an array of classes to add.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function addClass($class) {
    $classes =& $this
      ->getClasses();
    $classes = array_unique(array_merge($classes, (array) $class));
  }

  /**
   * Retrieve a specific attribute from the array.
   *
   * @param string $name
   *   The specific attribute to retrieve.
   * @param mixed $default
   *   (optional) The default value to set if the attribute does not exist.
   *
   * @return mixed
   *   A specific attribute value, passed by reference.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
   */
  public function &getAttribute($name, $default = NULL) {
    return $this
      ->offsetGet($name, $default);
  }

  /**
   * Retrieves classes from the array.
   *
   * @return array
   *   The classes array, passed by reference.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
   */
  public function &getClasses() {
    $classes =& $this
      ->offsetGet('class', []);
    $classes = array_unique($classes);
    return $classes;
  }

  /**
   * Indicates whether a specific attribute is set.
   *
   * @param string $name
   *   The attribute to search for.
   *
   * @return bool
   *   TRUE or FALSE
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetExists()
   */
  public function hasAttribute($name) {
    return $this
      ->offsetExists($name);
  }

  /**
   * Indicates whether a class is present in the array.
   *
   * @param string|array $class
   *   The class or array of classes to search for.
   * @param bool $all
   *   Flag determining to check if all classes are present.
   *
   * @return bool
   *   TRUE or FALSE
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function hasClass($class, $all = FALSE) {
    $classes = (array) $class;
    $result = array_intersect($classes, $this
      ->getClasses());
    return $all ? $result && count($classes) === count($result) : !!$result;
  }

  /**
   * Removes an attribute from the array.
   *
   * @param string|array $name
   *   The name of the attribute to remove.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetUnset()
   */
  public function removeAttribute($name) {
    $this
      ->offsetUnset($name);
  }

  /**
   * Removes a class from the attributes array.
   *
   * @param string|array $class
   *   An individual class or an array of classes to remove.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function removeClass($class) {
    $classes =& $this
      ->getClasses();
    $classes = array_values(array_diff($classes, (array) $class));
  }

  /**
   * Replaces a class in the attributes array.
   *
   * @param string $old
   *   The old class to remove.
   * @param string $new
   *   The new class. It will not be added if the $old class does not exist.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function replaceClass($old, $new) {
    $classes =& $this
      ->getClasses();
    $key = array_search($old, $classes);
    if ($key !== FALSE) {
      $classes[$key] = $new;
    }
  }

  /**
   * Sets an attribute on the array.
   *
   * @param string $name
   *   The name of the attribute to set.
   * @param mixed $value
   *   The value of the attribute to set.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetSet()
   */
  public function setAttribute($name, $value) {
    $this
      ->offsetSet($name, $value);
  }

  /**
   * Sets multiple attributes on the array.
   *
   * @param array $values
   *   An associative key/value array of attributes to set.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::merge()
   */
  public function setAttributes(array $values) {
    $this
      ->merge($values);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArrayObject::$array protected property The array.
ArrayObject::addAttachments public function Adds attachments. Overrides AttachmentsInterface::addAttachments
ArrayObject::addCacheableDependency public function Adds a dependency on an object: merges its cacheability metadata. Overrides RefinableCacheableDependencyInterface::addCacheableDependency
ArrayObject::addCacheContexts public function Adds cache contexts. Overrides RefinableCacheableDependencyInterface::addCacheContexts
ArrayObject::addCacheTags public function Adds cache tags. Overrides RefinableCacheableDependencyInterface::addCacheTags
ArrayObject::append public function Appends the value.
ArrayObject::asort public function Sort the entries by value.
ArrayObject::bubbleObject public function Merges an object's cacheable metadata into the variables array.
ArrayObject::bubbleRenderArray public function Merges a render array's cacheable metadata into the variables array.
ArrayObject::count public function Get the number of public properties in the ArrayObject.
ArrayObject::exchangeArray public function Exchange the array for another one. 1
ArrayObject::getArrayCopy public function Creates a copy of the ArrayObject.
ArrayObject::getAttachments public function Gets attachments. Overrides AttachmentsInterface::getAttachments
ArrayObject::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
ArrayObject::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
ArrayObject::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
ArrayObject::getIterator public function Creates a new iterator from an ArrayObject instance.
ArrayObject::ksort public function Sort the entries by key.
ArrayObject::merge public function Merges multiple values into the array.
ArrayObject::mergeCacheMaxAge public function Merges the maximum age (in seconds) with the existing maximum age. Overrides RefinableCacheableDependencyInterface::mergeCacheMaxAge
ArrayObject::natcasesort public function Sort an array using a case insensitive "natural order" algorithm.
ArrayObject::natsort public function Sort entries using a "natural order" algorithm.
ArrayObject::offsetExists public function Returns whether the requested key exists.
ArrayObject::offsetGet public function Returns the value at the specified key.
ArrayObject::offsetSet public function Sets the value at the specified key to value.
ArrayObject::offsetUnset public function Unsets the value at the specified key.
ArrayObject::serialize public function Serialize an ArrayObject.
ArrayObject::setAttachments public function Sets attachments. Overrides AttachmentsInterface::setAttachments
ArrayObject::uasort public function Sort entries with a user-defined function and maintain key association.
ArrayObject::uksort public function Sort the entries by keys using a user-defined comparison function.
ArrayObject::unserialize public function Unserialize an ArrayObject.
ArrayObject::__get public function Returns the value at the specified key by reference. 1
ArrayObject::__isset public function Returns whether the requested key exists. 1
ArrayObject::__set public function Sets the value at the specified key to value. 1
ArrayObject::__unset public function Unsets the value at the specified key. 1
Attributes::addClass public function Add class(es) to the array.
Attributes::getAttribute public function Retrieve a specific attribute from the array.
Attributes::getClasses public function Retrieves classes from the array.
Attributes::hasAttribute public function Indicates whether a specific attribute is set.
Attributes::hasClass public function Indicates whether a class is present in the array.
Attributes::removeAttribute public function Removes an attribute from the array.
Attributes::removeClass public function Removes a class from the attributes array.
Attributes::replaceClass public function Replaces a class in the attributes array.
Attributes::setAttribute public function Sets an attribute on the array.
Attributes::setAttributes public function Sets multiple attributes on the array.
Attributes::__construct public function Array object constructor. Overrides ArrayObject::__construct