1 | <?php declare(strict_types = 1); |
||
10 | abstract class Container extends PrettyJsonSerializable implements \Countable, \ArrayAccess, \IteratorAggregate |
||
11 | { |
||
12 | protected $members = []; |
||
13 | protected $closed = true; |
||
14 | |||
15 | /** |
||
16 | * Check whether value can be added as member. |
||
17 | * |
||
18 | * @throws InvalidArgumentException if value is no JSKOS Resource |
||
19 | */ |
||
20 | protected static function checkMember($value) |
||
21 | { |
||
22 | if ($value instanceof Resource) { |
||
23 | return $value; |
||
24 | } else { |
||
25 | throw new InvalidArgumentException(get_called_class() . ' may only contain JSKOS Resources'); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Check whether an equal member alredy exists in this Container. |
||
31 | */ |
||
32 | abstract public function contains($member): bool; |
||
33 | |||
34 | /** |
||
35 | * Create a new container, possibly from an array. |
||
36 | */ |
||
37 | public function __construct(array $members = []) |
||
47 | |||
48 | /** |
||
49 | * Apply a function to each member of this container. |
||
50 | * @param $callback callable |
||
51 | */ |
||
52 | public function map(callable $callback): array |
||
62 | |||
63 | /** |
||
64 | * Return whether this container is empty (no members and closed). |
||
65 | */ |
||
66 | public function isEmpty(): bool |
||
70 | |||
71 | /** |
||
72 | * Return whether this container is closed. |
||
73 | */ |
||
74 | public function isClosed(): bool |
||
78 | |||
79 | /** |
||
80 | * Set whether this container is closed (no unknown members) or not. |
||
81 | * @param Boolean $closed |
||
82 | */ |
||
83 | public function setClosed(bool $closed = true) |
||
87 | |||
88 | # implements Countable |
||
89 | |||
90 | /** |
||
91 | * Return the number of known values in this container (closed or not). |
||
92 | */ |
||
93 | public function count(): int |
||
97 | |||
98 | # implements ArrayAccess: |
||
99 | |||
100 | /** |
||
101 | * Return whether an object exists at the given offset. |
||
102 | * @param mixed $offset |
||
103 | */ |
||
104 | public function offsetExists($offset) |
||
108 | |||
109 | /** |
||
110 | * Return an object at the given offset. |
||
111 | * @param mixed $offset |
||
112 | */ |
||
113 | public function offsetGet($offset) |
||
117 | |||
118 | /** |
||
119 | * Set an object at the given offset. |
||
120 | * @param mixed $offset |
||
121 | * @param mixed $object |
||
122 | */ |
||
123 | public function offsetSet($offset, $object) |
||
137 | |||
138 | /** |
||
139 | * Append an object at the end. |
||
140 | */ |
||
141 | public function append($object) |
||
149 | |||
150 | /** |
||
151 | * @throws \BadMethodCallException |
||
152 | */ |
||
153 | public function offsetUnset($key) |
||
157 | |||
158 | # implements IteratorAggregate: |
||
159 | |||
160 | /** |
||
161 | * Return an iterator to iterate all members of the set. |
||
162 | */ |
||
163 | public function getIterator() |
||
167 | |||
168 | # extends PrettyJsonSerializable: |
||
169 | |||
170 | public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null) |
||
181 | } |
||
182 |