-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractResourceList.php
173 lines (145 loc) · 3.85 KB
/
AbstractResourceList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Resource;
use Klipper\Component\Resource\Exception\OutOfBoundsException;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* Abstract Resource list.
*
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
abstract class AbstractResourceList implements \IteratorAggregate, ResourceListInterface
{
protected ?string $status = null;
/**
* @var ResourceInterface[]
*/
protected array $resources = [];
protected ConstraintViolationListInterface $errors;
/**
* @var null|ConstraintViolationListInterface|FormErrorIterator[]
*/
protected $childrenErrors;
/**
* @param ResourceInterface[] $resources The list of resource
* @param ConstraintViolationListInterface $errors The list of errors
*/
public function __construct(
array $resources = [],
?ConstraintViolationListInterface $errors = null
) {
$this->errors = $errors ?? new ConstraintViolationList();
foreach ($resources as $resource) {
$this->add($resource);
}
}
public function getStatus(): string
{
if (null === $this->status) {
$this->refreshStatus();
}
return $this->status;
}
public function getResources(): array
{
return $this->resources;
}
public function add(ResourceInterface $resource): void
{
$this->reset();
$this->resources[] = $resource;
}
public function addAll(ResourceListInterface $otherList): void
{
$this->reset();
foreach ($otherList as $resource) {
$this->resources[] = $resource;
}
}
public function all(): array
{
return $this->resources;
}
/**
* @param mixed $offset
*/
public function get($offset): ResourceInterface
{
if (!isset($this->resources[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
}
return $this->resources[$offset];
}
public function has(int $offset): bool
{
return isset($this->resources[$offset]);
}
public function set(int $offset, ResourceInterface $resource): void
{
$this->reset();
$this->resources[$offset] = $resource;
}
public function remove(int $offset): void
{
$this->reset();
unset($this->resources[$offset]);
}
public function count(): int
{
return \count($this->resources);
}
/**
* @param mixed $offset
*/
public function offsetExists($offset): bool
{
return $this->has($offset);
}
/**
* @param mixed $offset
*/
public function offsetGet($offset): ResourceInterface
{
return $this->get($offset);
}
/**
* @param mixed $offset
* @param mixed $resource
*/
public function offsetSet($offset, $resource): void
{
if (null === $offset) {
$this->add($resource);
} else {
$this->set($offset, $resource);
}
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset): void
{
$this->remove($offset);
}
/**
* Reset the value of status and children errors.
*/
protected function reset(): void
{
$this->status = null;
$this->childrenErrors = null;
}
/**
* Refresh the status of this list.
*/
abstract protected function refreshStatus(): void;
}