1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Stratadox\EntityState; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Stratadox\ImmutableCollection\ImmutableCollection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Collection of property representations. |
11
|
|
|
* |
12
|
|
|
* @author Stratadox |
13
|
|
|
*/ |
14
|
|
|
final class PropertyStates extends ImmutableCollection implements ListsPropertyStates |
15
|
|
|
{ |
16
|
|
|
private function __construct(RepresentsProperty ...$properties) |
17
|
|
|
{ |
18
|
|
|
parent::__construct(...$properties); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Packs a number of properties in a collection. |
23
|
|
|
* |
24
|
|
|
* @param RepresentsProperty ...$properties |
25
|
|
|
* @return ListsPropertyStates |
26
|
|
|
*/ |
27
|
|
|
public static function list(RepresentsProperty ...$properties): ListsPropertyStates |
28
|
|
|
{ |
29
|
|
|
return new self(...$properties); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @inheritdoc */ |
33
|
|
|
public function current(): RepresentsProperty |
34
|
|
|
{ |
35
|
|
|
return parent::current(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @inheritdoc */ |
39
|
|
|
public function offsetGet($position): RepresentsProperty |
40
|
|
|
{ |
41
|
|
|
return parent::offsetGet($position); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @inheritdoc */ |
45
|
|
|
public function contains(RepresentsProperty $property): bool |
46
|
|
|
{ |
47
|
|
|
foreach ($this as $candidate) { |
48
|
|
|
if ($candidate->isSameAs($property)) { |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @inheritdoc */ |
56
|
|
|
public function areDifferentFrom(ListsPropertyStates $otherProperties): bool |
57
|
|
|
{ |
58
|
|
|
foreach ($otherProperties as $property) { |
59
|
|
|
if (!$this->contains($property)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @inheritdoc */ |
67
|
|
|
public function merge(ListsPropertyStates $newProperties): ListsPropertyStates |
68
|
|
|
{ |
69
|
|
|
$properties = $this->items(); |
70
|
|
|
foreach ($newProperties as $theNewProperty) { |
71
|
|
|
$properties[$this->positionOf($theNewProperty)] = $theNewProperty; |
72
|
|
|
} |
73
|
|
|
return PropertyStates::list(...$properties); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @inheritdoc */ |
77
|
|
|
public function hasOneNamed(string $propertyName): bool |
78
|
|
|
{ |
79
|
|
|
foreach ($this as $property) { |
80
|
|
|
if ($property->name() === $propertyName) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @inheritdoc */ |
88
|
|
|
public function theOneNamed(string $propertyName): RepresentsProperty |
89
|
|
|
{ |
90
|
|
|
foreach ($this as $property) { |
91
|
|
|
if ($property->name() === $propertyName) { |
92
|
|
|
return $property; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
throw new InvalidArgumentException("No such property: `$propertyName`"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function positionOf(RepresentsProperty $theNewProperty): int |
99
|
|
|
{ |
100
|
|
|
foreach ($this as $i => $property) { |
101
|
|
|
if ($property->name() === $theNewProperty->name()) { |
102
|
|
|
return $i; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return $this->count(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|