function EntityContextDefinition::createFromArray

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.

File

src/Context/EntityContextDefinition.php, line 83

Class

EntityContextDefinition
Extends the core entity context definition class with useful methods.

Namespace

Drupal\rules\Context

Code

public static function createFromArray(array $values) {
    if (isset($values['class']) && !in_array(ContextDefinitionInterface::class, class_implements($values['class']))) {
        throw new ContextException('EntityContextDefinition class must implement ' . ContextDefinitionInterface::class . '.');
    }
    // Default to Rules context definition class.
    $values['class'] = $values['class'] ?? EntityContextDefinition::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;
}