ContextDefinition.php in Drupal 10
File
core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
View source
<?php
namespace Drupal\Core\Plugin\Context;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\TypedData\TypedDataTrait;
class ContextDefinition implements ContextDefinitionInterface {
use DependencySerializationTrait {
__sleep as traitSleep;
}
use TypedDataTrait;
protected $dataType;
protected $label;
protected $description;
protected $isMultiple = FALSE;
protected $isRequired = TRUE;
protected $defaultValue;
protected $constraints = [];
public static function create($data_type = 'any') {
if (strpos($data_type, 'entity:') === 0) {
return new EntityContextDefinition($data_type);
}
return new static($data_type);
}
public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
$this->dataType = $data_type;
$this->label = $label;
$this->isRequired = $required;
$this->isMultiple = $multiple;
$this->description = $description;
$this->defaultValue = $default_value;
assert(strpos($data_type, 'entity:') !== 0 || $this instanceof EntityContextDefinition);
}
public function getDataType() {
return $this->dataType;
}
public function setDataType($data_type) {
$this->dataType = $data_type;
return $this;
}
public function getLabel() {
return $this->label;
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
return $this;
}
public function isMultiple() {
return $this->isMultiple;
}
public function setMultiple($multiple = TRUE) {
$this->isMultiple = $multiple;
return $this;
}
public function isRequired() {
return $this->isRequired;
}
public function setRequired($required = TRUE) {
$this->isRequired = $required;
return $this;
}
public function getDefaultValue() {
return $this->defaultValue;
}
public function setDefaultValue($default_value) {
$this->defaultValue = $default_value;
return $this;
}
public function getConstraints() {
return $this->constraints;
}
public function getConstraint($constraint_name) {
$constraints = $this
->getConstraints();
return $constraints[$constraint_name] ?? NULL;
}
public function setConstraints(array $constraints) {
$this->constraints = $constraints;
return $this;
}
public function addConstraint($constraint_name, $options = NULL) {
$this->constraints[$constraint_name] = $options;
return $this;
}
public function getDataDefinition() {
if ($this
->isMultiple()) {
$definition = $this
->getTypedDataManager()
->createListDataDefinition($this
->getDataType());
}
else {
$definition = $this
->getTypedDataManager()
->createDataDefinition($this
->getDataType());
}
if (!$definition) {
throw new \Exception("The data type '{$this->getDataType()}' is invalid");
}
$definition
->setLabel($this
->getLabel())
->setDescription($this
->getDescription())
->setRequired($this
->isRequired());
$constraints = $definition
->getConstraints() + $this
->getConstraints();
$definition
->setConstraints($constraints);
return $definition;
}
protected function dataTypeMatches(ContextInterface $context) {
$this_type = $this
->getDataType();
$that_type = $context
->getContextDefinition()
->getDataType();
return $this_type === 'any' || $this_type === $that_type || strpos($that_type, "{$this_type}:") === 0;
}
public function isSatisfiedBy(ContextInterface $context) {
$definition = $context
->getContextDefinition();
if (!$this
->dataTypeMatches($context)) {
return FALSE;
}
if ($context
->hasContextValue()) {
$values = [
$context
->getContextData(),
];
}
elseif ($definition instanceof self) {
$values = $definition
->getSampleValues();
}
else {
$values = [];
}
$validator = $this
->getTypedDataManager()
->getValidator();
foreach ($values as $value) {
$constraints = array_values($this
->getConstraintObjects());
$violations = $validator
->validate($value, $constraints);
foreach ($violations as $delta => $violation) {
if (!in_array($violation
->getConstraint(), $constraints)) {
$violations
->remove($delta);
}
}
if (!$violations
->count()) {
return TRUE;
}
}
return FALSE;
}
protected function getSampleValues() {
(yield $this
->getTypedDataManager()
->create($this
->getDataDefinition()));
}
protected function getConstraintObjects() {
$constraint_definitions = $this
->getConstraints();
$validation_constraint_manager = $this
->getTypedDataManager()
->getValidationConstraintManager();
$constraints = [];
foreach ($constraint_definitions as $constraint_name => $constraint_definition) {
$constraints[$constraint_name] = $validation_constraint_manager
->create($constraint_name, $constraint_definition);
}
return $constraints;
}
}