mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
|
|
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ResourceDuplicateCheck;
|
|
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
|
|
|
|
class ResourceDuplicateCheckValidator extends ConstraintValidator
|
|
{
|
|
|
|
private PersonRender $personRender;
|
|
private ThirdPartyRender $thirdpartyRender;
|
|
|
|
public function __construct(PersonRender $personRender, ThirdPartyRender $thirdPartyRender)
|
|
{
|
|
$this->personRender = $personRender;
|
|
$this->thirdpartyRender = $thirdPartyRender;
|
|
}
|
|
|
|
public function validate($resources, Constraint $constraint)
|
|
{
|
|
if (!$constraint instanceof ResourceDuplicateCheck) {
|
|
throw new UnexpectedTypeException($constraint, ParticipationOverlap::class);
|
|
}
|
|
|
|
if (!$resources instanceof Collection) {
|
|
throw new UnexpectedTypeException($resources, Collection::class);
|
|
}
|
|
|
|
$resourceList = [];
|
|
|
|
foreach ($resources as $resource) {
|
|
$id = ($resource->getResource() instanceof Person ? 'p' :
|
|
't').$resource->getResource()->getId();
|
|
|
|
if (\in_array($id, $resourceList, true)) {
|
|
$this->context->buildViolation($constraint->message)
|
|
->setParameter('{{ name }}', $resource->getResource() instanceof Person ? $this->personRender->renderString($resource->getResource(), []) :
|
|
$this->thirdpartyRender->renderString($resource->getResource(), []))
|
|
->addViolation();
|
|
}
|
|
|
|
$resourceList[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |