You are here

class PhpDateIntervalConstraintValidator 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

Validates the php_date_interval constraint.

Hierarchy

Expanded class hierarchy of PhpDateIntervalConstraintValidator

File

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

Namespace

Drupal\duration_field\Plugin\Validation\Constraint
View source
class PhpDateIntervalConstraintValidator extends Iso8601StringConstraintValidatorBase {

  /**
   * {@inheritdoc}
   */
  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,
        ]);
      }
    }
  }

  /**
   * Test if the given value is a valid ISO 8601 duration string.
   *
   * @param mixed $value
   *   The item to check as an ISO 8601 duration string.
   *
   * @return bool
   *   TRUE if the value is a valid ISO 8601 Duration string. Otherwise FALSE.
   */
  protected function isPhpDateIntervalObject($value) {
    return is_object($value) && is_a($value, 'DateInterval');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Iso8601StringConstraintValidatorBase::isIso8601String protected function Test if a string is a valid ISO 8601 duration string.
PhpDateIntervalConstraintValidator::isPhpDateIntervalObject protected function Test if the given value is a valid ISO 8601 duration string.
PhpDateIntervalConstraintValidator::validate public function