DefinitionList::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Definition;
5
6
use Habemus\Definition\Tag\Taggable;
7
use Habemus\Utility\Lists\KeyValueList;
8
use IteratorAggregate;
9
10
/**
11
 * @method Definition get($id):
12
 */
13
class DefinitionList implements IteratorAggregate
14
{
15
    use KeyValueList {
16
        set as private;
17
    }
18
19
    public function add(Definition $definition)
20
    {
21
        $this->set($definition->getIdentity(), $definition);
22
    }
23
24
    public function hasTag(string $tag): bool
25
    {
26
        foreach ($this->elements as $definition) {
27
            if ($definition instanceof Taggable && $definition->hasTag($tag)) {
28
                return true;
29
            }
30
        }
31
32
        return false;
33
    }
34
35
    /**
36
     * @param string $tag
37
     * @return Definition[]|Taggable[]
38
     */
39
    public function getTagged(string $tag): array
40
    {
41
        $tagged = [];
42
        foreach ($this->elements as $definition) {
43
            if ($definition instanceof Taggable && $definition->hasTag($tag)) {
44
                $tagged[] = $definition;
45
            }
46
        }
47
48
        return $tagged;
49
    }
50
}
51