예제 #1
0
 /**
  * Gets default constraints for the given data definition.
  *
  * This generates default constraint definitions based on the data definition;
  * e.g. a NotNull constraint is generated if the data is defined as required.
  * Besides that any constraints defined for the data type, i.e. below the
  * 'constraint' key of the type's plugin definition, are taken into account.
  *
  * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
  *   A data definition.
  *
  * @return array
  *   An array of validation constraint definitions, keyed by constraint name.
  *   Each constraint definition can be used for instantiating
  *   \Symfony\Component\Validator\Constraint objects.
  */
 public function getDefaultConstraints(DataDefinitionInterface $definition)
 {
     $constraints = array();
     $type_definition = $this->getDefinition($definition->getDataType());
     // Auto-generate a constraint for data types implementing a primitive
     // interface.
     if (is_subclass_of($type_definition['class'], '\\Drupal\\Core\\TypedData\\PrimitiveInterface')) {
         $constraints['PrimitiveType'] = array();
     }
     // Add in constraints specified by the data type.
     if (isset($type_definition['constraints'])) {
         $constraints += $type_definition['constraints'];
     }
     // Add the NotNull constraint for required data.
     if ($definition->isRequired()) {
         $constraints['NotNull'] = array();
     }
     // Check if the class provides allowed values.
     if (is_subclass_of($definition->getClass(), 'Drupal\\Core\\TypedData\\OptionsProviderInterface')) {
         $constraints['AllowedValues'] = array();
     }
     // Add any constraints about referenced data.
     if ($definition instanceof DataReferenceDefinitionInterface) {
         $constraints += $definition->getTargetDefinition()->getConstraints();
     }
     return $constraints;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function canFilter(DataDefinitionInterface $definition)
 {
     return is_subclass_of($definition->getClass(), DateTimeInterface::class);
 }
예제 #3
0
 /**
  * Checks that the data type of a mapped variable matches the expectation.
  *
  * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
  *   The context definition of the context on the plugin.
  * @param \Drupal\Core\TypedData\DataDefinitionInterface $provided
  *   The data definition of the mapped variable to the context.
  * @param string $context_name
  *   The name of the context on the plugin.
  * @param \Drupal\rules\Engine\IntegrityViolationList $violation_list
  *   The list of violations where new ones will be added.
  */
 protected function checkDataTypeCompatible(ContextDefinitionInterface $context_definition, DataDefinitionInterface $provided, $context_name, IntegrityViolationList $violation_list)
 {
     $expected_class = $context_definition->getDataDefinition()->getClass();
     $provided_class = $provided->getClass();
     $expected_type_problem = NULL;
     if (is_subclass_of($expected_class, PrimitiveInterface::class) && !is_subclass_of($provided_class, PrimitiveInterface::class)) {
         $expected_type_problem = $this->t('primitive');
     } elseif (is_subclass_of($expected_class, ListInterface::class) && !is_subclass_of($provided_class, ListInterface::class)) {
         $expected_type_problem = $this->t('list');
     } elseif (is_subclass_of($expected_class, ComplexDataInterface::class) && !is_subclass_of($provided_class, ComplexDataInterface::class)) {
         $expected_type_problem = $this->t('complex');
     }
     if ($expected_type_problem) {
         $violation = new IntegrityViolation();
         $violation->setMessage($this->t('Expected a @expected_type data type for context %context_name but got a @provided_type data type instead.', ['@expected_type' => $expected_type_problem, '%context_name' => $context_definition->getLabel(), '@provided_type' => $provided->getDataType()]));
         $violation->setContextName($context_name);
         $violation_list->add($violation);
     }
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function getClass() {
   return $this->wrappedProperty->getClass();
 }