You are here

abstract class ArrayElement in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Config/Schema/ArrayElement.php \Drupal\Core\Config\Schema\ArrayElement

Defines a generic configuration element that contains multiple properties.

Hierarchy

Expanded class hierarchy of ArrayElement

1 file declares its use of ArrayElement
ViewsConfigUpdater.php in core/modules/views/src/ViewsConfigUpdater.php

File

core/lib/Drupal/Core/Config/Schema/ArrayElement.php, line 10

Namespace

Drupal\Core\Config\Schema
View source
abstract class ArrayElement extends Element implements \IteratorAggregate, TypedConfigInterface, ComplexDataInterface {

  /**
   * Parsed elements.
   */
  protected $elements;

  /**
   * Gets valid configuration data keys.
   *
   * @return array
   *   Array of valid configuration data keys.
   */
  protected function getAllKeys() {
    return is_array($this->value) ? array_keys($this->value) : [];
  }

  /**
   * Builds an array of contained elements.
   *
   * @return \Drupal\Core\TypedData\TypedDataInterface[]
   *   An array of elements contained in this element.
   */
  protected function parse() {
    $elements = [];
    foreach ($this
      ->getAllKeys() as $key) {
      $value = isset($this->value[$key]) ? $this->value[$key] : NULL;
      $definition = $this
        ->getElementDefinition($key);
      $elements[$key] = $this
        ->createElement($definition, $value, $key);
    }
    return $elements;
  }

  /**
   * Gets data definition object for contained element.
   *
   * @param int|string $key
   *   Property name or index of the element.
   *
   * @return \Drupal\Core\TypedData\DataDefinitionInterface
   */
  protected abstract function getElementDefinition($key);

  /**
   * {@inheritdoc}
   */
  public function get($name) {
    $parts = explode('.', $name);
    $root_key = array_shift($parts);
    $elements = $this
      ->getElements();
    if (isset($elements[$root_key])) {
      $element = $elements[$root_key];

      // If $property_name contained a dot recurse into the keys.
      while ($element && ($key = array_shift($parts)) !== NULL) {
        if ($element instanceof TypedConfigInterface) {
          $element = $element
            ->get($key);
        }
        else {
          $element = NULL;
        }
      }
    }
    if (isset($element)) {
      return $element;
    }
    else {
      throw new \InvalidArgumentException("The configuration property {$name} doesn't exist.");
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getElements() {
    if (!isset($this->elements)) {
      $this->elements = $this
        ->parse();
    }
    return $this->elements;
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    return empty($this->value);
  }

  /**
   * {@inheritdoc}
   */
  public function toArray() {
    return isset($this->value) ? $this->value : [];
  }

  /**
   * {@inheritdoc}
   */
  public function onChange($name) {

    // Notify the parent of changes.
    if (isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    return new \ArrayIterator($this
      ->getElements());
  }

  /**
   * Creates a contained typed configuration object.
   *
   * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
   *   The data definition object.
   * @param mixed $value
   *   (optional) The data value. If set, it has to match one of the supported
   *   data type format as documented for the data type classes.
   * @param string $key
   *   The key of the contained element.
   *
   * @return \Drupal\Core\TypedData\TypedDataInterface
   */
  protected function createElement($definition, $value, $key) {
    return $this
      ->getTypedDataManager()
      ->create($definition, $value, $key, $this);
  }

  /**
   * Creates a new data definition object from a type definition array and
   * actual configuration data.
   *
   * @param array $definition
   *   The base type definition array, for which a data definition should be
   *   created.
   * @param $value
   *   The value of the configuration element.
   * @param string $key
   *   The key of the contained element.
   *
   * @return \Drupal\Core\TypedData\DataDefinitionInterface
   */
  protected function buildDataDefinition($definition, $value, $key) {
    return $this
      ->getTypedDataManager()
      ->buildDataDefinition($definition, $value, $key, $this);
  }

  /**
   * Determines if this element allows NULL as a value.
   *
   * @return bool
   *   TRUE if NULL is a valid value, FALSE otherwise.
   */
  public function isNullable() {
    return isset($this->definition['nullable']) && $this->definition['nullable'] == TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function set($property_name, $value, $notify = TRUE) {
    $this->value[$property_name] = $value;

    // Config schema elements do not make use of notifications. Thus, we skip
    // notifying parents.
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getProperties($include_computed = FALSE) {
    $properties = [];
    foreach (array_keys($this->value) as $name) {
      $properties[$name] = $this
        ->get($name);
    }
    return $properties;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArrayElement::$elements protected property Parsed elements.
ArrayElement::buildDataDefinition protected function Creates a new data definition object from a type definition array and actual configuration data.
ArrayElement::createElement protected function Creates a contained typed configuration object.
ArrayElement::get public function Gets a contained typed configuration element. Overrides TypedConfigInterface::get
ArrayElement::getAllKeys protected function Gets valid configuration data keys.
ArrayElement::getElementDefinition abstract protected function Gets data definition object for contained element. 2
ArrayElement::getElements public function Gets an array of contained elements. Overrides TypedConfigInterface::getElements
ArrayElement::getIterator public function
ArrayElement::getProperties public function Gets an array of property objects. Overrides ComplexDataInterface::getProperties
ArrayElement::isEmpty public function Determines whether the data structure is empty. Overrides TypedConfigInterface::isEmpty
ArrayElement::isNullable public function Determines if this element allows NULL as a value.
ArrayElement::onChange public function React to changes to a child property or item. Overrides TraversableTypedDataInterface::onChange
ArrayElement::parse protected function Builds an array of contained elements.
ArrayElement::set public function Sets a property value. Overrides ComplexDataInterface::set
ArrayElement::toArray public function Returns an array of all property values. Overrides TypedConfigInterface::toArray
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
Element::$value protected property The configuration value.
Element::getTypedDataManager public function Gets the typed configuration manager. Overrides TypedDataTrait::getTypedDataManager
Element::setTypedDataManager public function Sets the typed config manager. Overrides TypedDataTrait::setTypedDataManager
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedData::$definition protected property The data definition. 1
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::applyDefaultValue public function Applies the default value. Overrides TypedDataInterface::applyDefaultValue 3
TypedData::createInstance public static function Constructs a TypedData object given its definition and context. Overrides TypedDataInterface::createInstance
TypedData::getConstraints public function Gets a list of validation constraints. Overrides TypedDataInterface::getConstraints 9
TypedData::getDataDefinition public function Gets the data definition. Overrides TypedDataInterface::getDataDefinition
TypedData::getName public function Returns the name of a property or item. Overrides TypedDataInterface::getName
TypedData::getParent public function Returns the parent data structure; i.e. either complex data or a list. Overrides TypedDataInterface::getParent
TypedData::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
TypedData::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
TypedData::getPropertyPath public function Returns the property path of the data. Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Returns the root of the typed data tree. Overrides TypedDataInterface::getRoot
TypedData::getString public function Returns a string representation of the data. Overrides TypedDataInterface::getString 6
TypedData::getValue public function Gets the data value. Overrides TypedDataInterface::getValue 10
TypedData::setContext public function Sets the context of a property or item via a context aware parent. Overrides TypedDataInterface::setContext
TypedData::setValue public function Sets the data value. Overrides TypedDataInterface::setValue 10
TypedData::validate public function Validates the currently set data value. Overrides TypedDataInterface::validate
TypedData::__construct public function Constructs a TypedData object given its definition and context. 3
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.