AbstractHashableCollection::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
 * содержащихся в добавляемом объекте. {@see HashableIndex::resolveIndexOf($item)}
22
 * @author Evgenii Dudal <[email protected]>
23
 */
24
abstract class AbstractHashableCollection implements Collection, HashableIndex
25
{
26
    use CollectionTrait;
27
28
//    const COLLECTION_ITEM_NAME = 'Item';
29
30 2
    public function __construct(array $items = [])
31
    {
32 2
        foreach ($items as $item)
33
        {
34 2
            $this->add($item);
35
        }
36 2
    }
37
38 2
    public function add($item): void
39
    {
40 2
        $index = $this->resolveIndexOf($item);
41 2
        Assert::keyNotExists($this->items, $index, $this->getItemName() . ' already exist.');
42 2
        $this->items[$index] = $item;
43 2
        return;
44
    }
45
46
}
47