Passed
Push — master ( 16b303...27cbee )
by Alexander
02:59 queued 47s
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 extends \ArrayObject
11
{
12
    /**
13
     * @var int Max items count, if set 0 - unlimited
14
     */
15
    protected $maxCount = 0;
16
17
    /**
18
     * @param CollectionItemInterface[] $items
19
     */
20
    public function __construct(array $items = [])
21
    {
22
        parent::__construct($items);
23
    }
24
25
    /**
26
     * @param CollectionItemInterface $item
27
     * @param mixed $key
28
     * @return void
29 8
     * @throws ReachedMaxSizeException
30
     * @throws KeyHasUseException
31 8
     */
32 1
    public function addItem(CollectionItemInterface $item, $key = null)
33
    {
34
        if ($this->maxCount > 0 && $this->count() + 1 > $this->maxCount) {
35 8
            throw new ReachedMaxSizeException("Maximum collection items count reached. Max size: {$this->maxCount}");
36 5
        }
37 5
38 3
        if ($key == null) {
39
            $this->append($item);
40
        } else {
41 3
            if ($this->offsetExists($key)) {
42
                throw new KeyHasUseException("Key $key already in use.");
43 8
            }
44
            $this->offsetSet($key, $item);
45
        }
46
    }
47
48
    /**
49
     * @param $key
50 1
     * @throws KeyInvalidException
51
     * @return void
52 1
     */
53
    public function deleteItem($key)
54 1
    {
55 1
        $this->checkItemKey($key);
56
57
        $this->offsetUnset($key);
58
    }
59
60
    /**
61
     * @param $key
62
     * @return InputMedia
63 1
     * @return CollectionItemInterface
64
     * @throws KeyInvalidException
65 1
     */
66
    public function getItem($key)
67 1
    {
68
        $this->checkItemKey($key);
69
70
        return $this->offsetGet($key);
71
    }
72
73 8
    /**
74
     * @param bool $inner
75 8
     * @return array|string
76
     */
77
    public function toJson($inner = false)
78
    {
79
        $output = [];
80
        foreach ($this as $item) {
81
            $output[] = $item->toJson(true);
82 2
        }
83
84 2
        return $inner === false ? json_encode($output) : $output;
85 2
    }
86 2
87 2
    /**
88
     * @param int $maxCount
89 2
     * @return void
90
     */
91
    public function setMaxCount($maxCount)
92
    {
93
        $this->maxCount = $maxCount;
94
    }
95
96 1
    /**
97
     * @param $key
98 1
     * @throws KeyInvalidException
99 1
     */
100
    private function checkItemKey($key)
101
    {
102
        if (!$this->offsetExists($key)) {
103
            throw new KeyInvalidException("Invalid key $key.");
104
        }
105 2
    }
106
}
107