CollectionItem::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Billogram\Model\Item;
6
7
use Billogram\Exception\InvalidArgumentException;
8
use Billogram\Model\CreatableFromArray;
9
10
/**
11
 * @author Ibrahim Hizeoui <[email protected]>
12
 */
13
class CollectionItem implements CreatableFromArray
14
{
15
    /**
16
     * @var Item[]
17
     */
18
    private $items;
19
20
    /**
21
     * @param Item[] $items
22
     */
23 1
    private function __construct(array $items)
24
    {
25 1
        foreach ($items as $item) {
26 1
            if (!$item instanceof Item) {
27 1
                throw new InvalidArgumentException('All items must be an instance of '.Item::class);
28
            }
29
        }
30 1
        $this->items = $items;
31 1
    }
32
33 1
    public static function createFromArray(array $data)
34
    {
35 1
        $items = [];
36 1
        if (isset($data['data'])) {
37 1
            foreach ($data['data'] as $item) {
38 1
                $items[] = Item::createFromArray($item);
39
            }
40
        }
41
42 1
        return new self($items);
43
    }
44
45
    /**
46
     * @return Item[]
47
     */
48
    public function getItems()
49
    {
50
        return $this->items;
51
    }
52
}
53