1 | <?php |
||
20 | class UniqueObject extends ObjectExists |
||
21 | { |
||
22 | /** |
||
23 | * Error constants |
||
24 | */ |
||
25 | public const ERROR_OBJECT_NOT_UNIQUE = 'objectNotUnique'; |
||
26 | |||
27 | // phpcs:disable Generic.Files.LineLength |
||
28 | /** @var mixed[] */ |
||
29 | protected $messageTemplates = [self::ERROR_OBJECT_NOT_UNIQUE => "There is already another object matching '%value%'"]; |
||
30 | // phpcs:enable Generic.Files.LineLength |
||
31 | |||
32 | /** @var ObjectManager */ |
||
33 | protected $objectManager; |
||
34 | |||
35 | /** @var bool */ |
||
36 | protected $useContext; |
||
37 | |||
38 | /*** |
||
39 | * Constructor |
||
40 | * |
||
41 | * @param mixed[] $options required keys are `object_repository`, which must be an instance of |
||
42 | * Doctrine\Persistence\ObjectRepository, `object_manager`, which |
||
43 | * must be an instance of Doctrine\Persistence\ObjectManager, |
||
44 | * and `fields`, with either a string or an array of strings representing |
||
45 | * the fields to be matched by the validator. |
||
46 | * |
||
47 | * @throws Exception\InvalidArgumentException |
||
48 | */ |
||
49 | 12 | public function __construct(array $options) |
|
76 | |||
77 | /** |
||
78 | * Returns false if there is another object with the same field values but other identifiers. |
||
79 | * |
||
80 | * @param mixed $value |
||
81 | * @param mixed $context |
||
82 | */ |
||
83 | 10 | public function isValid($value, $context = null) : bool |
|
84 | { |
||
85 | 10 | if (! $this->useContext) { |
|
86 | 5 | $context = (array) $value; |
|
87 | } |
||
88 | |||
89 | 10 | $cleanedValue = $this->cleanSearchValue($value); |
|
90 | 10 | $match = $this->objectRepository->findOneBy($cleanedValue); |
|
91 | |||
92 | 10 | if (! is_object($match)) { |
|
93 | 2 | return true; |
|
94 | } |
||
95 | |||
96 | 8 | $expectedIdentifiers = $this->getExpectedIdentifiers($context); |
|
97 | 5 | $foundIdentifiers = $this->getFoundIdentifiers($match); |
|
98 | |||
99 | 5 | if (count(array_diff_assoc($expectedIdentifiers, $foundIdentifiers)) === 0) { |
|
100 | 3 | return true; |
|
101 | } |
||
102 | |||
103 | 2 | $this->error(self::ERROR_OBJECT_NOT_UNIQUE, $value); |
|
104 | |||
105 | 2 | return false; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Gets the identifiers from the matched object. |
||
110 | * |
||
111 | * @return mixed[] |
||
112 | * |
||
113 | * @throws Exception\RuntimeException |
||
114 | */ |
||
115 | 5 | protected function getFoundIdentifiers(object $match) : array |
|
121 | |||
122 | /** |
||
123 | * Gets the identifiers from the context. |
||
124 | * |
||
125 | * @param mixed[]|object $context |
||
126 | * |
||
127 | * @return mixed[] |
||
128 | * |
||
129 | * @throws Exception\RuntimeException |
||
130 | */ |
||
131 | 8 | protected function getExpectedIdentifiers($context = null) : array |
|
158 | |||
159 | /** |
||
160 | * @return mixed[] the names of the identifiers |
||
161 | */ |
||
162 | 6 | protected function getIdentifiers() : array |
|
168 | } |
||
169 |