Passed
Pull Request — master (#1691)
by Tarmo
12:26 queued 09:10
created

EntityReferenceExists::getTargets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    public const ENTITY_REFERENCE_EXISTS_ERROR = '64888b5e-bded-449b-82ed-0cc1f73df14d';
32
    public const MESSAGE_SINGLE = 'Invalid id value "{{ id }}" given for entity "{{ entity }}".';
33
    public const MESSAGE_MULTIPLE = 'Invalid id values "{{ id }}" given for entity "{{ entity }}".';
34
35
    public string $entityClass = '';
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @var array<string, string>
41
     */
42
    protected static $errorNames = [
43
        self::ENTITY_REFERENCE_EXISTS_ERROR => 'ENTITY_REFERENCE_EXISTS_ERROR',
44
    ];
45
46
    /**
47
     * EntityReferenceExists constructor.
48
     *
49
     * @param array<string, string> $options
50
     */
51 12
    public function __construct(
52
        ?string $entityClass = null,
53
        array $options = [],
54
        array $groups = [],
55
        mixed $payload = null,
56
    ) {
57 12
        $this->entityClass = $entityClass ?? $options['entityClass'] ?? '';
58
59 12
        parent::__construct($options, $groups, $payload);
60
    }
61
}
62