Collection::__construct()   A
last analyzed

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