|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the InMemoryList package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace InMemoryList\Domain\Model; |
|
12
|
|
|
|
|
13
|
|
|
use InMemoryList\Domain\Helper\ListElementConsistencyChecker; |
|
14
|
|
|
use InMemoryList\Domain\Model\Exceptions\ListElementDuplicateKeyException; |
|
15
|
|
|
use InMemoryList\Domain\Model\Exceptions\ListElementKeyDoesNotExistException; |
|
16
|
|
|
use InMemoryList\Domain\Model\Exceptions\ListElementNotConsistentException; |
|
17
|
|
|
|
|
18
|
|
|
class ListCollection implements \Countable |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $elements; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ListCollectionUuid |
|
27
|
|
|
*/ |
|
28
|
|
|
private $uuid; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $headers; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* IMListElementCollection constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ListCollectionUuid $uuid |
|
39
|
|
|
* @param array $elements |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(ListCollectionUuid $uuid, array $elements = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->setUuid($uuid); |
|
44
|
|
|
$this->setElements($elements); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param ListCollectionUuid $uuid |
|
49
|
|
|
*/ |
|
50
|
|
|
private function setUuid(ListCollectionUuid $uuid) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->uuid = $uuid; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return ListCollectionUuid |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getUuid() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->uuid; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $elements |
|
65
|
|
|
*/ |
|
66
|
|
|
private function setElements($elements) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->elements = $elements; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param ListElementUuid $uuid |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function hasElement(ListElementUuid $uuid) |
|
77
|
|
|
{ |
|
78
|
|
|
return isset($this->elements[$uuid->getUuid()]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ListElement $element |
|
83
|
|
|
* |
|
84
|
|
|
* @throws ListElementDuplicateKeyException |
|
85
|
|
|
* @throws ListElementNotConsistentException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function addElement(ListElement $element) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->hasElement($element->getUuid())) { |
|
90
|
|
|
throw new ListElementDuplicateKeyException('Key '.$element->getUuid()->getUuid().' already in use.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!ListElementConsistencyChecker::isConsistent($element, $this->elements)) { |
|
94
|
|
|
throw new ListElementNotConsistentException('Element '.$element->getUuid()->getUuid().' is not consistent with list data.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->elements[$element->getUuid()->getUuid()] = $element; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param ListElement $element |
|
102
|
|
|
* |
|
103
|
|
|
* @throws ListElementKeyDoesNotExistException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function deleteElement(ListElement $element) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->hasElement($element->getUuid())) { |
|
108
|
|
|
throw new ListElementKeyDoesNotExistException('Invalid key '.$element->getUuid()->getUuid()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
unset($this->elements[$element->getUuid()->getUuid()]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param ListElementUuid $uuid |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
* |
|
119
|
|
|
* @throws ListElementKeyDoesNotExistException |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getElement(ListElementUuid $uuid) |
|
122
|
|
|
{ |
|
123
|
|
|
if (!$this->hasElement($uuid)) { |
|
124
|
|
|
throw new ListElementKeyDoesNotExistException('Invalid key '.$uuid->getUuid()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->elements[$uuid->getUuid()]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getElements() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->elements; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $headers |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setHeaders($headers) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->headers = $headers; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getHeaders() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->headers; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return int |
|
156
|
|
|
*/ |
|
157
|
|
|
public function count() |
|
158
|
|
|
{ |
|
159
|
|
|
return count($this->elements); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|