1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\EntityState\Internal; |
4
|
|
|
|
5
|
|
|
use function is_object; |
6
|
|
|
use Stratadox\IdentityMap\MapsObjectsByIdentity as Map; |
7
|
|
|
use Stratadox\IdentityMap\NoSuchObject; |
8
|
|
|
|
9
|
|
|
final class ExtractionRequest |
10
|
|
|
{ |
11
|
|
|
private $value; |
12
|
|
|
private $owner; |
13
|
|
|
private $name; |
14
|
|
|
private $map; |
15
|
|
|
private $visited; |
16
|
|
|
|
17
|
|
|
private function __construct( |
18
|
|
|
$value, |
19
|
|
|
object $owner, |
20
|
|
|
Name $name, |
21
|
|
|
Map $map, |
22
|
|
|
Visited $visited |
23
|
|
|
) { |
24
|
|
|
$this->value = $value; |
25
|
|
|
$this->owner = $owner; |
26
|
|
|
$this->name = $name; |
27
|
|
|
$this->map = $map; |
28
|
|
|
$this->visited = $visited; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function for($entity, Map $identityMap): self |
32
|
|
|
{ |
33
|
|
|
return new self( |
34
|
|
|
$entity, |
35
|
|
|
$entity, |
36
|
|
|
Name::start(), |
37
|
|
|
$identityMap, |
38
|
|
|
Visited::noneYet() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function value() |
43
|
|
|
{ |
44
|
|
|
return $this->value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isTheOwner(): bool |
48
|
|
|
{ |
49
|
|
|
return $this->value === $this->owner; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function objectName(): Name |
53
|
|
|
{ |
54
|
|
|
return $this->name->for($this->value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function nameForCounting(): Name |
58
|
|
|
{ |
59
|
|
|
return $this->name->toCount($this->value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function name(): Name |
63
|
|
|
{ |
64
|
|
|
return $this->name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function pointsToAnotherEntity(): bool |
68
|
|
|
{ |
69
|
|
|
return is_object($this->value) |
70
|
|
|
&& $this->map->hasThe($this->value) |
71
|
|
|
&& !$this->isTheOwner(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @throws NoSuchObject */ |
75
|
|
|
public function otherEntityId(): string |
76
|
|
|
{ |
77
|
|
|
return $this->map->idOf($this->value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isRecursive(): bool |
81
|
|
|
{ |
82
|
|
|
return $this->visited->already($this->value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function visitedName(): string |
86
|
|
|
{ |
87
|
|
|
return $this->visited->name($this->value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function withVisitation(): self |
91
|
|
|
{ |
92
|
|
|
return new self( |
93
|
|
|
$this->value, |
94
|
|
|
$this->owner, |
95
|
|
|
$this->name, |
96
|
|
|
$this->map, |
97
|
|
|
$this->visited->add($this->value, $this->name) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function forCollectionItem( |
102
|
|
|
iterable $collection, |
103
|
|
|
string $key, |
104
|
|
|
$value |
105
|
|
|
): self { |
106
|
|
|
return new self( |
107
|
|
|
$value, |
108
|
|
|
$this->owner, |
109
|
|
|
$this->name->forItem($collection, $key), |
110
|
|
|
$this->map, |
111
|
|
|
$this->visited |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function forProperty(ReflectionProperty $property): self |
116
|
|
|
{ |
117
|
|
|
$name = $this->isTheOwner() ? |
118
|
|
|
$this->name : |
119
|
|
|
$this->name->for($this->value); |
120
|
|
|
|
121
|
|
|
return new self( |
122
|
|
|
$property->getValue($this->value), |
123
|
|
|
$this->owner, |
124
|
|
|
$name->forReflected($property), |
125
|
|
|
$this->map, |
126
|
|
|
$this->visited |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|