Collection::find()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Leaditin\Annotations;
6
7
use Leaditin\Annotations\Exception\CollectionException;
8
9
/**
10
 * Class Collection
11
 *
12
 * @package Leaditin\Annotations
13
 * @author Igor Vuckovic <[email protected]>
14
 */
15
class Collection implements \IteratorAggregate, \Countable
16
{
17
    /** @var array|Annotation[] */
18
    protected $annotations;
19
20
    /**
21
     * @param Annotation[] ...$annotations
22
     */
23 22
    public function __construct(Annotation ...$annotations)
24
    {
25 22
        $this->annotations = $annotations;
26 22
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function getIterator() : \ArrayIterator
32
    {
33 1
        return new \ArrayIterator($this->annotations);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function count() : int
40
    {
41 6
        return count($this->annotations);
42
    }
43
44
    /**
45
     * @return Annotation[]
46
     */
47 1
    public function getAll() : array
48
    {
49 1
        return $this->annotations;
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return bool
55
     */
56 7
    public function has(string $name) : bool
57
    {
58 7
        foreach ($this->annotations as $annotation) {
59 7
            if ($annotation->getName() === $name) {
60 7
                return true;
61
            }
62
        }
63
64 1
        return false;
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return Annotation
70
     * @throws CollectionException
71
     */
72 6
    public function findOne(string $name) : Annotation
73
    {
74 6
        foreach ($this->annotations as $annotation) {
75 6
            if ($annotation->getName() === $name) {
76 6
                return $annotation;
77
            }
78
        }
79
80 1
        throw CollectionException::undefinedAnnotation($name);
81
    }
82
83
    /**
84
     * @param string $name
85
     * @return Collection|Annotation[]
86
     */
87 1
    public function find(string $name) : Collection
88
    {
89 1
        $matched = [];
90
91 1
        foreach ($this->annotations as $annotation) {
92 1
            if ($annotation->getName() === $name) {
93 1
                $matched[] = $annotation;
94
            }
95
        }
96
97 1
        return new static(...$matched);
98
    }
99
}
100