Completed
Push — master ( b7ad07...7762ce )
by Hannes
02:04
created

Definition::readAnnotation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester;
6
7
/**
8
 * The data defining an example
9
 */
10
class Definition
11
{
12
    /**
13
     * @var array The set of annotations
14
     */
15
    private $annotations;
16
17
    /**
18
     * @var CodeBlock Example code
19
     */
20
    private $code;
21
22
    public function __construct(array $annotations, CodeBlock $code)
23
    {
24
        $this->annotations = $annotations;
25
        $this->code = $code;
26
    }
27
28
    public function getCodeBlock(): CodeBlock
29
    {
30
        return $this->code;
31
    }
32
33
    public function getAnnotations(): array
34
    {
35
        return $this->annotations;
36
    }
37
38
    /**
39
     * Check if annotation $needle exists
40
     */
41
    public function isAnnotatedWith(string $needle): bool
42
    {
43
        foreach ($this->getAnnotations() as list($name, $args)) {
44
            if (strcasecmp($name, $needle) == 0) {
45
                return true;
46
            }
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * Read the first argument from annotation $needle
54
     */
55
    public function readAnnotation(string $needle): string
56
    {
57
        foreach ($this->getAnnotations() as list($name, $args)) {
58
            if (strcasecmp($name, $needle) == 0) {
59
                return isset($args[0]) ? $args[0] : '';
60
            }
61
        }
62
63
        return '';
64
    }
65
}
66