1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stratadox\Hydration\Mapper\Instruction\Relation; |
6
|
|
|
|
7
|
|
|
use function class_implements; |
8
|
|
|
use function in_array; |
9
|
|
|
use Stratadox\Collection\Alterable; |
10
|
|
|
use Stratadox\Hydration\Hydrates; |
11
|
|
|
use Stratadox\Hydration\Hydrator\ArrayHydrator; |
12
|
|
|
use Stratadox\Hydration\Hydrator\SimpleHydrator; |
13
|
|
|
use Stratadox\Hydration\Hydrator\VariadicConstructor; |
14
|
|
|
use Stratadox\Hydration\Mapper\NoContainerAvailable; |
15
|
|
|
use Stratadox\Hydration\Mapper\NoLoaderAvailable; |
16
|
|
|
use Stratadox\Hydration\Mapping\Property\Relationship\HasManyNested; |
17
|
|
|
use Stratadox\Hydration\Mapping\Property\Relationship\HasManyProxies; |
18
|
|
|
use Stratadox\Hydration\Mapping\Property\Relationship\HasOneProxy; |
19
|
|
|
use Stratadox\Hydration\MapsProperty; |
20
|
|
|
use Stratadox\Hydration\ProducesOwnerUpdaters; |
21
|
|
|
use Stratadox\Hydration\Proxy; |
22
|
|
|
use Stratadox\Hydration\Proxying\AlterableCollectionEntryUpdaterFactory; |
23
|
|
|
use Stratadox\Hydration\Proxying\ArrayEntryUpdaterFactory; |
24
|
|
|
use Stratadox\Hydration\Proxying\PropertyUpdaterFactory; |
25
|
|
|
use Stratadox\Hydration\Proxying\ProxyFactory; |
26
|
|
|
|
27
|
|
|
final class HasMany extends Relationship |
28
|
|
|
{ |
29
|
|
|
public function followFor(string $property) : MapsProperty |
30
|
|
|
{ |
31
|
|
|
if ($this->shouldNest) { |
32
|
|
|
return $this->manyNestedInThe($property); |
33
|
|
|
} |
34
|
|
|
if (!isset($this->loader)) { |
35
|
|
|
throw NoLoaderAvailable::for($this->class); |
36
|
|
|
} |
37
|
|
|
if ($this->implements(Proxy::class, $this->class)) { |
38
|
|
|
return $this->manyProxiesInThe($property); |
39
|
|
|
} |
40
|
|
|
if (!isset($this->container)) { |
41
|
|
|
throw NoContainerAvailable::for($this->class); |
42
|
|
|
} |
43
|
|
|
return $this->oneProxyInThe($property); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function manyNestedInThe(string $property) : MapsProperty |
47
|
|
|
{ |
48
|
|
|
return HasManyNested::inPropertyWithDifferentKey($property, |
49
|
|
|
$this->keyOr($property), |
50
|
|
|
$this->container(), |
51
|
|
|
$this->hydrator() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function manyProxiesInThe(string $property) : MapsProperty |
56
|
|
|
{ |
57
|
|
|
return HasManyProxies::inPropertyWithDifferentKey($property, |
58
|
|
|
$this->keyOr($property), |
59
|
|
|
$this->container(), |
60
|
|
|
ProxyFactory::fromThis( |
61
|
|
|
SimpleHydrator::forThe($this->class), |
62
|
|
|
$this->loader, |
|
|
|
|
63
|
|
|
$this->updaterFactory() |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function oneProxyInThe(string $property) : MapsProperty |
69
|
|
|
{ |
70
|
|
|
return HasOneProxy::inProperty($property, |
71
|
|
|
ProxyFactory::fromThis( |
72
|
|
|
SimpleHydrator::forThe($this->container), |
|
|
|
|
73
|
|
|
$this->loader, |
|
|
|
|
74
|
|
|
new PropertyUpdaterFactory |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function container() : Hydrates |
80
|
|
|
{ |
81
|
|
|
if (isset($this->container)) { |
82
|
|
|
return VariadicConstructor::forThe($this->container); |
83
|
|
|
} |
84
|
|
|
return ArrayHydrator::create(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function updaterFactory() : ProducesOwnerUpdaters |
88
|
|
|
{ |
89
|
|
|
if ($this->implements(Alterable::class, $this->container)) { |
90
|
|
|
return new AlterableCollectionEntryUpdaterFactory; |
91
|
|
|
} |
92
|
|
|
return new ArrayEntryUpdaterFactory; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function implements(string $interface, $class) |
96
|
|
|
{ |
97
|
|
|
return isset($class) && in_array($interface, class_implements($class)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|