1 | <?php |
||
10 | class Collection |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $items = []; |
||
16 | |||
17 | /** |
||
18 | * @var int Max items count, if set 0 - unlimited |
||
19 | */ |
||
20 | protected $maxCount = 0; |
||
21 | |||
22 | /** |
||
23 | * @param CollectionItemInterface $item |
||
24 | * @param mixed $key |
||
25 | * @return void |
||
26 | * @throws ReachedMaxSizeException |
||
27 | * @throws KeyHasUseException |
||
28 | */ |
||
29 | 8 | public function addItem(CollectionItemInterface $item, $key = null) |
|
30 | { |
||
31 | 8 | if ($this->maxCount > 0 && $this->count() >= $this->maxCount) { |
|
32 | throw new ReachedMaxSizeException("Maximum collection items count reached. Max size: {$this->maxCount}"); |
||
33 | } |
||
34 | |||
35 | 8 | if ($key == null) { |
|
36 | 5 | $this->items[] = $item; |
|
37 | 5 | } else { |
|
38 | 3 | if (isset($this->items[$key])) { |
|
39 | throw new KeyHasUseException("Key $key already in use."); |
||
40 | } |
||
41 | 3 | $this->items[$key] = $item; |
|
42 | } |
||
43 | 8 | } |
|
44 | |||
45 | /** |
||
46 | * @param $key |
||
47 | * @throws KeyInvalidException |
||
48 | * @return void |
||
49 | */ |
||
50 | 1 | public function deleteItem($key) |
|
56 | |||
57 | /** |
||
58 | * @param $key |
||
59 | * @return InputMedia |
||
60 | * @return CollectionItemInterface |
||
61 | * @throws KeyInvalidException |
||
62 | */ |
||
63 | 1 | public function getItem($key) |
|
69 | |||
70 | /** |
||
71 | * @return int |
||
72 | */ |
||
73 | 8 | public function count() |
|
77 | |||
78 | /** |
||
79 | * @param bool $inner |
||
80 | * @return array|string |
||
81 | */ |
||
82 | 2 | public function toJson($inner = false) |
|
91 | |||
92 | /** |
||
93 | * @param int $maxCount |
||
94 | * @return void |
||
95 | */ |
||
96 | 1 | public function setMaxCount($maxCount) |
|
100 | |||
101 | /** |
||
102 | * @param $key |
||
103 | * @throws KeyInvalidException |
||
104 | */ |
||
105 | 2 | private function checkItemKey($key) |
|
111 | } |
||
112 |