You are here

public function PhpDateIntervalConstraintValidator::validate in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Validation/Constraint/PhpDateIntervalConstraintValidator.php \Drupal\duration_field\Plugin\Validation\Constraint\PhpDateIntervalConstraintValidator::validate()

File

src/Plugin/Validation/Constraint/PhpDateIntervalConstraintValidator.php, line 15

Class

PhpDateIntervalConstraintValidator
Validates the php_date_interval constraint.

Namespace

Drupal\duration_field\Plugin\Validation\Constraint

Code

public function validate($items, Constraint $constraint) {
  if (is_array($items)) {
    foreach ($items as $item) {
      if (!$this
        ->isPhpDateIntervalObject($item) && !$this
        ->isIso8601String($item)) {

        // The value is not an PHP Date Interval, so a violation, aka error,
        // is applied.
        $this->context
          ->addViolation($constraint->notDateInterval, [
          '%value' => (string) $item,
        ]);
      }
    }
  }
  else {
    if (!$this
      ->isPhpDateIntervalObject($items) && !$this
      ->isIso8601String($items)) {

      // The value is not an PHP Date Interval, so a violation, aka error,
      // is applied.
      $this->context
        ->addViolation($constraint->notDateInterval, [
        '%value' => (string) $items,
      ]);
    }
  }
}