-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ModelsToArrayTransformer.php
243 lines (208 loc) · 6.11 KB
/
ModelsToArrayTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Form\DataTransformer;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Util\ClassUtils;
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Util\TraversableToCollection;
use Sonata\Doctrine\Adapter\AdapterInterface;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* @final since sonata-project/admin-bundle 3.52
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* @phpstan-template T of object
*/
class ModelsToArrayTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
protected $modelManager;
/**
* @var string
*
* @phpstan-var class-string<T>
*/
protected $class;
/**
* @var ModelChoiceLoader|LazyChoiceList
*
* @deprecated since sonata-project/admin-bundle 3.12, to be removed in 4.0
* NEXT_MAJOR: remove this property
*/
protected $choiceList;
/**
* @param LazyChoiceList|ModelChoiceLoader $choiceList
* @param ModelManagerInterface $modelManager
* @param string $class
*
* @phpstan-param class-string<T> $class
*
* @throws RuntimeException
*/
public function __construct($choiceList, $modelManager, $class = null)
{
/*
NEXT_MAJOR: Remove condition , magic methods, legacyConstructor() method, $choiceList property and argument
__construct() signature should be : public function __construct(ModelManager $modelManager, string $class)
*/
$args = \func_get_args();
if (3 === \func_num_args()) {
$this->legacyConstructor($args);
} else {
$this->modelManager = $args[0];
$this->class = $args[1];
}
}
/**
* @internal
*/
public function __get($name)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
return $this->$name;
}
/**
* @internal
*/
public function __set($name, $value)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
$this->$name = $value;
}
/**
* @internal
*/
public function __isset($name)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
return isset($this->$name);
}
/**
* @internal
*/
public function __unset($name)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
unset($this->$name);
}
/**
* @param object[]|null $value
*
* @return string[]
*
* @phpstan-param T[]|null $value
*/
public function transform($value)
{
if (null === $value) {
return [];
}
$array = [];
foreach ($value as $model) {
$id = implode(AdapterInterface::ID_SEPARATOR, $this->getIdentifierValues($model));
$array[] = $id;
}
return $array;
}
/**
* @return Collection<int|string, object>|null
*
* @phpstan-return Collection<array-key, T>|null
*/
public function reverseTransform($value)
{
if (null === $value) {
return null;
}
if (!\is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}
$value = array_map('strval', $value);
$query = $this->modelManager->createQuery($this->class);
$this->modelManager->addIdentifiersToQuery($this->class, $query, $value);
$result = $this->modelManager->executeQuery($query);
/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = TraversableToCollection::transform($result);
$diffCount = \count($value) - $collection->count();
if (0 !== $diffCount) {
throw new TransformationFailedException(sprintf(
'%u keys could not be found in the provided values: "%s".',
$diffCount,
implode('", "', $value)
));
}
return $collection;
}
/**
* Simulates the old constructor for BC.
*
* @throws RuntimeException
*/
private function legacyConstructor(array $args): void
{
$choiceList = $args[0];
if (!$choiceList instanceof ModelChoiceLoader
&& !$choiceList instanceof LazyChoiceList) {
throw new RuntimeException(
'First param passed to ModelsToArrayTransformer'
.' should be instance of ModelChoiceLoader or LazyChoiceList'
);
}
$this->choiceList = $choiceList;
$this->modelManager = $args[1];
$this->class = $args[2];
}
/**
* @param object $model
*
* @return mixed[]
*
* @phpstan-param T $model
*/
private function getIdentifierValues($model): array
{
try {
return $this->modelManager->getIdentifierValues($model);
} catch (\Exception $e) {
throw new \InvalidArgumentException(sprintf(
'Unable to retrieve the identifier values for entity %s',
ClassUtils::getClass($model)
), 0, $e);
}
}
/**
* @internal
*/
private function triggerDeprecation(): void
{
@trigger_error(sprintf(
'Using the "%s::$choiceList" property is deprecated since version 3.12 and will be removed in 4.0.',
__CLASS__
), E_USER_DEPRECATED);
}
}