-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ModelToIdPropertyTransformer.php
177 lines (151 loc) · 5.16 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
<?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\Collection;
use Doctrine\Common\Util\ClassUtils;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Transform object to ID and property label.
*
* @author Andrej Hudec <pulzarraider@gmail.com>
*
* @template T of object
*/
final class ModelToIdPropertyTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
private $modelManager;
/**
* @var class-string<T>
*/
private $className;
/**
* @var string
*/
private $property;
/**
* @var bool
*/
private $multiple;
/**
* @var callable|null
*/
private $toStringCallback;
/**
* @param class-string<T> $className
*/
public function __construct(
ModelManagerInterface $modelManager,
string $className,
string $property,
bool $multiple = false,
?callable $toStringCallback = null
) {
$this->modelManager = $modelManager;
$this->className = $className;
$this->property = $property;
$this->multiple = $multiple;
$this->toStringCallback = $toStringCallback;
}
/**
* @throws \UnexpectedValueException
*
* @return Collection<array-key, T>|T|null
*/
public function reverseTransform($value)
{
$collection = $this->modelManager->getModelCollectionInstance($this->className);
if (empty($value)) {
if ($this->multiple) {
return $collection;
}
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) {
continue;
}
$collection[] = $this->modelManager->find($this->className, $id);
}
return $collection;
}
/**
* @throws \InvalidArgumentException
*/
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 ('' === $this->property) {
throw new \RuntimeException('Please define "property" parameter.');
}
foreach ($collection as $model) {
$id = current($this->modelManager->getIdentifierValues($model));
if (null !== $this->toStringCallback) {
$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;
}
}