-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ModelToIdPropertyTransformer.php
207 lines (179 loc) · 6.2 KB
/
ModelToIdPropertyTransformer.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
<?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\Model\ModelManagerInterface;
use Sonata\AdminBundle\Util\TraversableToCollection;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Transform object to ID and property label.
*
* @final since sonata-project/admin-bundle 3.52
*
* @author Andrej Hudec <pulzarraider@gmail.com>
*
* @phpstan-template T of object
*/
class ModelToIdPropertyTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
protected $modelManager;
/**
* @var string
*
* @phpstan-var class-string<T>
*/
protected $className;
/**
* @var string
*/
protected $property;
/**
* @var bool
*/
protected $multiple;
/**
* @var callable|null
*/
protected $toStringCallback;
/**
* @param string $className
* @param string $property
* @param bool $multiple
* @param callable|null $toStringCallback
*
* @phpstan-param class-string<T> $className
* @phpstan-param null|callable(object, string): string $toStringCallback
*/
public function __construct(
ModelManagerInterface $modelManager,
$className,
$property,
$multiple = false,
$toStringCallback = null
) {
$this->modelManager = $modelManager;
$this->className = $className;
$this->property = $property;
$this->multiple = $multiple;
$this->toStringCallback = $toStringCallback;
}
/**
* @param mixed $value
*
* @return Collection<int|string, object>|object|null
*
* @phpstan-return Collection<array-key, T>|T|null
*/
public function reverseTransform($value)
{
if (empty($value)) {
if ($this->multiple) {
return new ArrayCollection();
}
return null;
}
if (!$this->multiple) {
return $this->modelManager->find($this->className, $value);
}
if (!\is_array($value)) {
throw new \UnexpectedValueException(sprintf('Value should be array, %s given.', \gettype($value)));
}
foreach ($value as $key => $id) {
if ('_labels' === $key) {
unset($value[$key]);
continue;
}
$value[$key] = (string) $id;
}
$query = $this->modelManager->createQuery($this->className);
$this->modelManager->addIdentifiersToQuery($this->className, $query, $value);
$result = $this->modelManager->executeQuery($query);
return TraversableToCollection::transform($result);
}
/**
* @param object|object[]|null $value
*
* @return array<string|int, string>
*
* @phpstan-param T|T[]|null $value
*
* @phpstan-return array{_labels?: mixed}
*/
public function transform($value)
{
$result = [];
if (!$value) {
return $result;
}
if ($this->multiple) {
$isArray = \is_array($value);
if (!$isArray && substr(\get_class($value), -1 * \strlen($this->className)) === $this->className) {
throw new \InvalidArgumentException(
'A multiple selection must be passed a collection not a single value.'
.' Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true"'
.' is set for many-to-many or one-to-many relations.'
);
}
if ($isArray || ($value instanceof \ArrayAccess)) {
$collection = $value;
} else {
throw new \InvalidArgumentException(
'A multiple selection must be passed a collection not a single value.'
.' Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true"'
.' is set for many-to-many or one-to-many relations.'
);
}
} else {
if (substr(\get_class($value), -1 * \strlen($this->className)) === $this->className) {
$collection = [$value];
} elseif ($value instanceof \ArrayAccess) {
throw new \InvalidArgumentException(
'A single selection must be passed a single value not a collection.'
.' Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true"'
.' is set for many-to-many or one-to-many relations.'
);
} else {
$collection = [$value];
}
}
if (empty($this->property)) {
throw new \RuntimeException('Please define "property" parameter.');
}
foreach ($collection as $model) {
$id = current($this->modelManager->getIdentifierValues($model));
if (null !== $this->toStringCallback) {
if (!\is_callable($this->toStringCallback)) {
throw new \RuntimeException(
'Callback in "to_string_callback" option doesn`t contain callable function.'
);
}
$label = ($this->toStringCallback)($model, $this->property);
} else {
try {
$label = (string) $model;
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'Unable to convert the entity %s to String, entity must have a \'__toString()\' method defined',
ClassUtils::getClass($model)
), 0, $e);
}
}
$result[] = $id;
$result['_labels'][] = $label;
}
return $result;
}
}