PhpDateIntervalConstraintValidator.php in Duration Field 8.2
File
src/Plugin/Validation/Constraint/PhpDateIntervalConstraintValidator.php
View source
<?php
namespace Drupal\duration_field\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
class PhpDateIntervalConstraintValidator extends Iso8601StringConstraintValidatorBase {
public function validate($items, Constraint $constraint) {
if (is_array($items)) {
foreach ($items as $item) {
if (!$this
->isPhpDateIntervalObject($item) && !$this
->isIso8601String($item)) {
$this->context
->addViolation($constraint->notDateInterval, [
'%value' => (string) $item,
]);
}
}
}
else {
if (!$this
->isPhpDateIntervalObject($items) && !$this
->isIso8601String($items)) {
$this->context
->addViolation($constraint->notDateInterval, [
'%value' => (string) $items,
]);
}
}
}
protected function isPhpDateIntervalObject($value) {
return is_object($value) && is_a($value, 'DateInterval');
}
}