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