You are here

public static function ContextDefinition::createFromArray in Typed Data API enhancements 8

Creates a definition object from an exported array of values.

Parameters

array $values: The array of values, as returned by toArray().

Return value

static The created definition.

Throws

\Drupal\Component\Plugin\Exception\ContextException If the required classes are not implemented.

1 call to ContextDefinition::createFromArray()
ContextDefinition::__construct in src/Context/Annotation/ContextDefinition.php
Constructs a new context definition object.

File

src/Context/ContextDefinition.php, line 73

Class

ContextDefinition
Extends the core context definition class with useful methods.

Namespace

Drupal\typed_data\Context

Code

public static function createFromArray(array $values) {
  if (isset($values['class']) && !in_array(ContextDefinitionInterface::class, class_implements($values['class']))) {
    throw new ContextException('ContextDefinition class must implement ' . ContextDefinitionInterface::class . '.');
  }

  // Default to Typed Data context definition class.
  $values['class'] = isset($values['class']) ? $values['class'] : ContextDefinition::class;
  if (!isset($values['value'])) {
    $values['value'] = 'any';
  }
  $definition = $values['class']::create($values['value']);
  foreach (array_intersect_key(static::$nameMap, $values) as $key => $name) {
    $definition->{$name} = $values[$key];
  }
  return $definition;
}