|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Doctrine\Hydrator\Strategy; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use Zend\Hydrator\Exception\InvalidArgumentException; |
|
9
|
|
|
use Zend\Hydrator\HydratorInterface; |
|
10
|
|
|
use Zend\Hydrator\Strategy\StrategyInterface; |
|
11
|
|
|
|
|
12
|
|
|
class CollectionStrategy implements StrategyInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var HydratorInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $objectHydrator; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $objectClassName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param HydratorInterface $objectHydrator |
|
25
|
|
|
* @param string $objectClassName |
|
26
|
|
|
* |
|
27
|
|
|
* @throws InvalidArgumentException |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct(HydratorInterface $objectHydrator, $objectClassName) |
|
30
|
|
|
{ |
|
31
|
4 |
|
if (! is_string($objectClassName) || ! class_exists($objectClassName)) { |
|
32
|
|
|
throw new InvalidArgumentException(sprintf( |
|
33
|
|
|
'Object class name needs to the name of an existing class, got "%s" instead.', |
|
34
|
|
|
is_object($objectClassName) ? get_class($objectClassName) : gettype($objectClassName) |
|
35
|
|
|
)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
$this->objectHydrator = $objectHydrator; |
|
39
|
4 |
|
$this->objectClassName = $objectClassName; |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public function extract($value) |
|
46
|
|
|
{ |
|
47
|
4 |
|
if (! ($value instanceof Collection)) { |
|
48
|
|
|
throw new InvalidArgumentException(sprintf( |
|
49
|
|
|
'Value needs to be a Doctrine Collection, got %s instead.', |
|
50
|
|
|
is_object($value) ? get_class($value) : gettype($value) |
|
51
|
|
|
)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return array_map(function ($object) { |
|
55
|
4 |
|
if (! $object instanceof $this->objectClassName) { |
|
56
|
|
|
throw new InvalidArgumentException(sprintf( |
|
57
|
|
|
'Value needs to be an instance of "%s", got "%s" instead.', |
|
58
|
|
|
$this->objectClassName, |
|
59
|
|
|
is_object($object) ? get_class($object) : gettype($object) |
|
60
|
|
|
)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $this->objectHydrator->extract($object); |
|
64
|
4 |
|
}, $value->toArray()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hydrate($value) |
|
71
|
|
|
{ |
|
72
|
|
|
if (! ($value instanceof Collection)) { |
|
73
|
|
|
throw new InvalidArgumentException(sprintf( |
|
74
|
|
|
'Value needs to be a Doctrine Collection, got %s instead.', |
|
75
|
|
|
is_object($value) ? get_class($value) : gettype($value) |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$reflection = new ReflectionClass($this->objectClassName); |
|
80
|
|
|
|
|
81
|
|
|
return array_map(function ($data) use ($reflection) { |
|
82
|
|
|
return $this->objectHydrator->hydrate( |
|
83
|
|
|
$data, |
|
84
|
|
|
$reflection->newInstanceWithoutConstructor() |
|
85
|
|
|
); |
|
86
|
|
|
}, $value); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|