CollectionTrait::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 0
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
 * @author Evgenii Dudal <[email protected]>
20
 */
21
trait CollectionTrait
22
{
23
24
    protected static $collectionItemName = 'Item';
25
    private $items = [];
26
27
    public function exist($index): bool
28
    {
29
        return key_exists($index, $this->items);
30
    }
31
32
    /**
33
     *
34
     * @param mixed $key
35
     * @return mixed
36
     * @throws \InvalidArgumentException
37
     */
38
    public function get($key)
39
    {
40
        Assert::keyExists($this->items, $key, static::getItemName() . " with key `{$key}` not present in collection");
41
        return clone $this->items[$key];
42
    }
43
44 2
    public function remove($key): void
45
    {
46 2
        Assert::keyExists($this->items, $key, static::getItemName() . " with key `{$key}` not present in collection.");
47
        unset($this->items[$key]);
48
    }
49
50 2
    public function toArray()
51
    {
52 2
        return $this->items;
53
    }
54
55 2
    public function count(): int
56
    {
57 2
        return count($this->items);
58
    }
59
60 2
    public function getIterator(): \Traversable
61
    {
62 2
        yield from $this->items;
63 2
    }
64
65
    public static function getItemName(): string
66
    {
67
        return 'Item';
68
    }
69
70
}
71