|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TelegramBot\Api\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use TelegramBot\Api\Types\InputMedia\InputMedia; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Collection |
|
9
|
|
|
*/ |
|
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() + 1 >= $this->maxCount) { |
|
32
|
1 |
|
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) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->checkItemKey($key); |
|
53
|
|
|
|
|
54
|
1 |
|
unset($this->items[$key]); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $key |
|
59
|
|
|
* @return InputMedia |
|
60
|
|
|
* @return CollectionItemInterface |
|
61
|
|
|
* @throws KeyInvalidException |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function getItem($key) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->checkItemKey($key); |
|
66
|
|
|
|
|
67
|
1 |
|
return $this->items[$key]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
8 |
|
public function count() |
|
74
|
|
|
{ |
|
75
|
8 |
|
return count($this->items); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param bool $inner |
|
80
|
|
|
* @return array|string |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function toJson($inner = false) |
|
83
|
|
|
{ |
|
84
|
2 |
|
$output = []; |
|
85
|
2 |
|
foreach ($this->items as $item) { |
|
86
|
2 |
|
$output[] = $item->toJson(true); |
|
87
|
2 |
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return $inner === false ? json_encode($output) : $output; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param int $maxCount |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function setMaxCount($maxCount) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->maxCount = $maxCount; |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $key |
|
103
|
|
|
* @throws KeyInvalidException |
|
104
|
|
|
*/ |
|
105
|
2 |
|
private function checkItemKey($key) |
|
106
|
|
|
{ |
|
107
|
2 |
|
if (!isset($this->items[$key])) { |
|
108
|
|
|
throw new KeyInvalidException("Invalid key $key."); |
|
109
|
|
|
} |
|
110
|
2 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|