Feature: add cc users in workflow: add constraint validation on EntityWorkflowStep

This commit is contained in:
nobohan 2023-03-29 12:09:12 +02:00
parent a8c2750ac8
commit 8c37afa3a9
3 changed files with 72 additions and 0 deletions

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Entity\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Validator\Constraints\Entity\WorkflowStepUsers;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
@ -24,6 +25,7 @@ use function in_array;
/**
* @ORM\Entity
* @ORM\Table("chill_main_workflow_entity_step")
* @WorkflowStepUsers()
*/
class EntityWorkflowStep
{

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Validator\Constraints\Entity;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class WorkflowStepUsers extends Constraint
{
public string $message = 'The user in cc cannot be a dest user in the same workflow step';
public function getTargets()
{
return [self::CLASS_CONSTRAINT];
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Validator\Constraints\Entity;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* @Annotation
*/
class WorkflowStepUsersValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$value instanceof EntityWorkflowStep) {
throw new UnexpectedTypeException($value, EntityWorkflowStep::class);
}
if (!$constraint instanceof WorkflowStepUsers) {
throw new UnexpectedTypeException($constraint, WorkflowStepUsers::class);
}
foreach($value->getCcUser() as $u) {
if ($value->getCcUser()->contains($u)) {
$this->context
->buildViolation($constraint->message)
->atPath('ccUsers')
->addViolation();
}
}
}
}