-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathModelsToArrayTransformer.php
135 lines (117 loc) · 3.59 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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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 Sonata\AdminBundle\Util\TraversableToCollection;
use Sonata\Doctrine\Adapter\AdapterInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-template T of object
* @phpstan-implements DataTransformerInterface<\Traversable<T>, array<int|string>>
*/
final class ModelsToArrayTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
* @phpstan-var ModelManagerInterface<T>
*/
private $modelManager;
/**
* @var string
*
* @phpstan-var class-string<T>
*/
private $class;
/**
* @phpstan-param ModelManagerInterface<T> $modelManager
* @phpstan-param class-string<T> $class
*/
public function __construct(ModelManagerInterface $modelManager, string $class)
{
$this->modelManager = $modelManager;
$this->class = $class;
}
/**
* @param \Traversable<object>|null $value
*
* @return string[]
*
* @phpstan-param \Traversable<T>|null $value
*/
public function transform($value): array
{
if (null === $value) {
return [];
}
$array = [];
foreach ($value as $model) {
$array[] = implode(AdapterInterface::ID_SEPARATOR, $this->getIdentifierValues($model));
}
return $array;
}
/**
* @param array<int|string>|null $value
*
* @throws UnexpectedTypeException
*
* @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');
}
if ([] === $value) {
$result = $value;
} else {
$query = $this->modelManager->createQuery($this->class);
$this->modelManager->addIdentifiersToQuery($this->class, $query, $value);
$result = $this->modelManager->executeQuery($query);
}
$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;
}
/**
* @return array<int|string>
*
* @phpstan-param T $model
*/
private function getIdentifierValues(object $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);
}
}
}