Total Complexity | 7 |
Total Lines | 54 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
10 | class DataClassReader implements ReaderInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var ValidatorInterface |
||
14 | */ |
||
15 | private $validator; |
||
16 | |||
17 | /** |
||
18 | * @param ValidatorInterface $validator |
||
19 | */ |
||
20 | 6 | public function __construct(ValidatorInterface $validator) |
|
21 | { |
||
22 | 6 | $this->validator = $validator; |
|
23 | 6 | } |
|
24 | |||
25 | /** |
||
26 | * @inheritdoc |
||
27 | */ |
||
28 | 6 | public function read(FormInterface $form): array |
|
29 | { |
||
30 | 6 | $config = $form->getRoot()->getConfig(); |
|
31 | 6 | if (null === $config->getDataClass()) { |
|
32 | 1 | return []; |
|
33 | } |
||
34 | |||
35 | try { |
||
36 | 5 | $metadata = $this->validator->getMetadataFor($config->getDataClass()); |
|
37 | 1 | } catch (NoSuchMetadataException $exception) { |
|
38 | 1 | return []; |
|
39 | } |
||
40 | |||
41 | 4 | if (!$metadata instanceof ClassMetadata) { |
|
42 | 1 | return []; |
|
43 | } |
||
44 | |||
45 | 3 | $constraints = []; |
|
46 | 3 | foreach ($metadata->getPropertyMetadata($form->getName()) as $propertyMetadatum) { |
|
47 | 2 | $constraints = array_merge($constraints, $propertyMetadatum->findConstraints($metadata->getDefaultGroup())); |
|
48 | } |
||
49 | |||
50 | 3 | return $constraints; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * DataClassReader priority should be greater than FormTypeReader priority |
||
55 | * so that FormType constraints override entity constraints. |
||
56 | * |
||
57 | * @inheritdoc |
||
58 | * |
||
59 | * @codeCoverageIgnore |
||
60 | */ |
||
61 | public function getPriority(): int |
||
64 | } |
||
65 | } |
||
66 |