public function DateRecurRulePartConstraintValidator::validate in Recurring Dates Field 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Validation/Constraint/DateRecurRulePartConstraintValidator.php \Drupal\date_recur\Plugin\Validation\Constraint\DateRecurRulePartConstraintValidator::validate()
- 3.0.x src/Plugin/Validation/Constraint/DateRecurRulePartConstraintValidator.php \Drupal\date_recur\Plugin\Validation\Constraint\DateRecurRulePartConstraintValidator::validate()
- 3.1.x src/Plugin/Validation/Constraint/DateRecurRulePartConstraintValidator.php \Drupal\date_recur\Plugin\Validation\Constraint\DateRecurRulePartConstraintValidator::validate()
File
- src/
Plugin/ Validation/ Constraint/ DateRecurRulePartConstraintValidator.php, line 36
Class
- DateRecurRulePartConstraintValidator
- Validates the DateRecurRulePartConstraint constraint.
Namespace
Drupal\date_recur\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) : void {
assert($value instanceof DateRecurItem);
assert($constraint instanceof DateRecurRulePartConstraint);
/** @var \Drupal\date_recur\Plugin\Field\FieldType\DateRecurFieldItemList $fieldList */
$fieldList = $value
->getParent();
$grid = $fieldList
->getPartGrid();
// Validator do not apply to field values without RRULE.
if (empty($value->rrule)) {
return;
}
// Catch exceptions thrown by invalid rules.
try {
// Use a fake start time as there may be an empty or invalid start date.
$helper = DateRecurHelper::create($value->rrule, new \DateTime());
} catch (\Exception $e) {
// Invalid RRULE's are handled by DateRecurRruleConstraint.
return;
}
foreach ($helper
->getRules() as $rule) {
/** @var \Drupal\date_recur\DateRecurRuleInterface $rule */
$frequency = $rule
->getFrequency();
// Check if a frequency is supported.
if (!$grid
->isFrequencyAllowed($frequency)) {
$frequencyLabels = $this
->getFrequencyLabels();
$frequencyLabel = $frequencyLabels[$frequency] ?? $frequency;
$this->context
->addViolation($constraint->disallowedFrequency, [
'%frequency' => $frequencyLabel,
]);
// If the frequency isn't supported then dont continue validating its
// parts as it creates redundant violations.
continue;
}
$parts = $rule
->getParts();
unset($parts['FREQ']);
foreach (array_keys($parts) as $part) {
try {
// Check if a part is supported.
if (!$grid
->isPartAllowed($frequency, $part)) {
$partLabels = $this
->getPartLabels();
$partLabel = $partLabels[$part] ?? $part;
$this->context
->addViolation($constraint->disallowedPart, [
'%part' => $partLabel,
]);
}
} catch (DateRecurRulePartIncompatible $e) {
// If a part is incompatible, add a violation.
$frequencyLabels = $this
->getFrequencyLabels();
$frequencyLabel = $frequencyLabels[$frequency] ?? $frequency;
$partLabels = $this
->getPartLabels();
$partLabel = $partLabels[$part] ?? $part;
$this->context
->addViolation($constraint->incompatiblePart, [
'%frequency' => $frequencyLabel,
'%part' => $partLabel,
]);
}
}
}
}