tarlepp /
symfony-flex-backend
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | /** |
||
| 4 | * /src/Validator/Constraints/EntityReferenceExists.php |
||
| 5 | * |
||
| 6 | * @author TLe, Tarmo Leppänen <[email protected]> |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace App\Validator\Constraints; |
||
| 10 | |||
| 11 | use Attribute; |
||
| 12 | use Symfony\Component\Validator\Constraint; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class EntityReferenceExists |
||
| 16 | * |
||
| 17 | * Usage example; |
||
| 18 | * #[App\Validator\Constraints\EntityReferenceExists(SomeEntityClass::class)] |
||
| 19 | * |
||
| 20 | * Just add that to your property as an annotation and you're good to go. |
||
| 21 | * |
||
| 22 | * @Annotation |
||
| 23 | * @Target({"PROPERTY"}) |
||
| 24 | * |
||
| 25 | * @package App\Validator\Constraints |
||
| 26 | * @author TLe, Tarmo Leppänen <[email protected]> |
||
| 27 | */ |
||
| 28 | #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
||
| 29 | class EntityReferenceExists extends Constraint |
||
| 30 | { |
||
| 31 | final public const ENTITY_REFERENCE_EXISTS_ERROR = '64888b5e-bded-449b-82ed-0cc1f73df14d'; |
||
| 32 | final public const MESSAGE_SINGLE = 'Invalid id value "{{ id }}" given for entity "{{ entity }}".'; |
||
| 33 | final public const MESSAGE_MULTIPLE = 'Invalid id values "{{ id }}" given for entity "{{ entity }}".'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * {@inheritdoc} |
||
| 37 | * |
||
| 38 | * @psalm-var array<string, string> |
||
| 39 | */ |
||
| 40 | protected const ERROR_NAMES = [ |
||
| 41 | self::ENTITY_REFERENCE_EXISTS_ERROR => 'ENTITY_REFERENCE_EXISTS_ERROR', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | public string $entityClass = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * EntityReferenceExists constructor. |
||
| 48 | * |
||
| 49 | * @inheritDoc |
||
| 50 | * |
||
| 51 | * @param array<string, string> $options |
||
| 52 | * @param array<array-key, string> $groups |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 53 | */ |
||
| 54 | 11 | public function __construct( |
|
| 55 | ?string $entityClass = null, |
||
| 56 | array $options = [], |
||
| 57 | array $groups = [], |
||
| 58 | mixed $payload = null, |
||
| 59 | ) { |
||
| 60 | 11 | $this->entityClass = $entityClass ?? $options['entityClass'] ?? ''; |
|
| 61 | |||
| 62 | 11 | parent::__construct($options, $groups, $payload); |
|
| 63 | } |
||
| 64 | } |
||
| 65 |