Skip to content

Commit

Permalink
update entity exists validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Yarysh committed May 1, 2024
1 parent 2bfb06b commit 934e1e6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Service/Repository/FindableByIdInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* FindableByIdInterface.
*
* @deprecated
*/
interface FindableByIdInterface
{
Expand Down
2 changes: 2 additions & 0 deletions Service/Repository/GettableOneByIdInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* Gettable One By Id Interface.
*
* @deprecated
*/
interface GettableOneByIdInterface
{
Expand Down
2 changes: 2 additions & 0 deletions Service/Repository/RepositoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* Repository Service.
*
* @deprecated
*/
class RepositoryService
{
Expand Down
14 changes: 9 additions & 5 deletions Validator/Constraints/Entity/EntityExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ class EntityExists extends Constraint

public string $message = 'entity_does_not_exist';

/** @var class-string<object> */
public string $class;
public string $property;

/**
* @param string $class
* @param mixed $options
* @param array|null $groups
* @param mixed $payload
* @param class-string<object> $class
* @param mixed $options
* @param array|null $groups
* @param mixed $payload
* @param string $property
*/
public function __construct(string $class, mixed $options = null, array $groups = null, mixed $payload = null)
public function __construct(string $class, mixed $options = null, array $groups = null, mixed $payload = null, string $property = 'id')
{
parent::__construct($options, $groups, $payload);

Expand All @@ -52,5 +55,6 @@ public function __construct(string $class, mixed $options = null, array $groups
}

$this->class = $class;
$this->property = $property;
}
}
19 changes: 9 additions & 10 deletions Validator/Constraints/Entity/EntityExistsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace StfalconStudio\ApiBundle\Validator\Constraints\Entity;

use StfalconStudio\ApiBundle\Exception\Validator\UnexpectedConstraintException;
use StfalconStudio\ApiBundle\Service\Repository\RepositoryService;
use StfalconStudio\ApiBundle\Traits\EntityManagerTrait;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

Expand All @@ -22,12 +22,7 @@
*/
class EntityExistsValidator extends ConstraintValidator
{
/**
* @param RepositoryService $repositoryService
*/
public function __construct(private readonly RepositoryService $repositoryService)
{
}
use EntityManagerTrait;

/**
* @param mixed $value
Expand All @@ -41,15 +36,19 @@ public function validate(mixed $value, Constraint|EntityExists $constraint): voi
throw new UnexpectedConstraintException($constraint, EntityExists::class);
}

if (!\is_string($value)) {
if (!(\is_int($value) || \is_string($value))) {
return;
}

if (!$this->repositoryService->findEntityById($value, $constraint->class) instanceof $constraint->class) {
$repository = $this->em->getRepository($constraint->class);

if (!$repository->findOneBy([$constraint->property => $value]) instanceof $constraint->class) {
$this->context
->buildViolation($constraint->message)
->setCode(EntityExists::ENTITY_DOES_NOT_EXIST)
->setParameter('%id%', $value)
->setParameter('%property%', $constraint->property)
->setParameter('%value%', (string) $value)
->setParameter('%class%', $constraint->class)
->addViolation()
;
}
Expand Down

0 comments on commit 934e1e6

Please sign in to comment.