AbstractCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the ddd-common.
5
 *
6
 * Copyright 2021 Evgenii Dudal <[email protected]>.
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 * @package ddd-common
11
 */
12
13
namespace RobotE13\DDD\Entities\Collection;
14
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * Базовый класс для реализации типизированных коллекций
19
 *
20
 * Индексами коллекции будут являться порядковые номера добавленных элементов.
21
 * @method bool exist($index) {@see Collection::exist($index)}
22
 * @method mixed get($index) {@see Collection::get($index)}
23
 * @method void remove($index) {@see Collection::remove($index)}
24
 * @author Evgenii Dudal <[email protected]>
25
 */
26
abstract class AbstractCollection implements Collection
27
{
28
29
    use CollectionTrait;
30
31 2
    public function __construct(array $items = [])
32
    {
33 2
        Assert::allIsInstanceOf($items, $this->getItemClass());
34 2
        $this->items = $items;
35 2
    }
36
37
    /**
38
     *
39
     * @param mixed $item
40
     * @param bool $overwrite
41
     * @return void
42
     */
43 1
    public function add($item): void
44
    {
45 1
        Assert::isInstanceOf($item, $this->getItemClass());
46 1
        if (in_array($item, $this->items))
47
        {
48 1
            throw new \Webmozart\Assert\InvalidArgumentException($this->getItemName() . ' already exist.');
49
        }
50
        $this->items[] = $item;
51
    }
52
53
}
54