54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\PlanBundle\Validator\Constraints;
|
|
|
|
use MauticPlugin\PlanBundle\Entity\Task;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
|
|
|
class ValidTaskParentValidator extends ConstraintValidator
|
|
{
|
|
public function validate(mixed $value, Constraint $constraint): void
|
|
{
|
|
if (!$constraint instanceof ValidTaskParent) {
|
|
throw new UnexpectedTypeException($constraint, ValidTaskParent::class);
|
|
}
|
|
|
|
if (null === $value) {
|
|
return;
|
|
}
|
|
|
|
if (!$value instanceof Task) {
|
|
throw new UnexpectedValueException($value, Task::class);
|
|
}
|
|
|
|
$parent = $value->getParent();
|
|
|
|
if (null === $parent) {
|
|
return;
|
|
}
|
|
|
|
// The parent's ancestors include the task itself: the task is its own parent or a sub-task's child.
|
|
if ($parent->hasAncestorOrSelf($value)) {
|
|
$this->context->buildViolation($constraint->loopMessage)
|
|
->atPath('parent')
|
|
->addViolation();
|
|
|
|
return;
|
|
}
|
|
|
|
$plan = $value->getPlan();
|
|
$parentPlan = $parent->getPlan();
|
|
|
|
if (null !== $plan && null !== $parentPlan && $plan->getId() !== $parentPlan->getId()) {
|
|
$this->context->buildViolation($constraint->otherPlanMessage)
|
|
->atPath('parent')
|
|
->addViolation();
|
|
}
|
|
}
|
|
}
|