Completed
Push — master ( c13142...c0435c )
by Tobias
02:34
created

CollectionItem::createFromArray()   A

Complexity

Conditions 3
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 11
Code Lines 6

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
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 1
    private function __construct(array $items)
21
    {
22 1
        foreach ($items as $item) {
23 1
            if (!$item instanceof Item) {
24 1
                throw new InvalidArgumentException('All items must be an instance of '.Item::class);
25
            }
26
        }
27 1
        $this->items = $items;
28 1
    }
29
30 1
    public static function createFromArray(array $data)
31
    {
32 1
        $items = [];
33 1
        if (isset($data['data'])) {
34 1
            foreach ($data['data'] as $item) {
35 1
                $items[] = Item::createFromArray($item);
36
            }
37
        }
38
39 1
        return new self($items);
40
    }
41
42
    /**
43
     * @return Items[]
44
     */
45
    public function getItems()
46
    {
47
        return $this->items;
48
    }
49
}
50