Annotation::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Codeburner Framework.
5
 *
6
 * @author Alex Rohleder <[email protected]>
7
 * @copyright 2016 Alex Rohleder
8
 * @license http://opensource.org/licenses/MIT
9
 */
10
11
namespace Codeburner\Annotator;
12
13
/**
14
 * The annotation representation.
15
 *
16
 * @author Alex Rohleder <[email protected]>
17
 */
18
19
class Annotation
20
{
21
22
    /**
23
     * The annotation token.
24
     *
25
     * @var string
26
     */
27
28
    protected $name;
29
30
    /**
31
     * Formmated arguments, but not casted.
32
     *
33
     * @var string[]
34
     */
35
36
    protected $arguments;
37
38
    /**
39
     * @param string $name
40
     * @param string $arguments
41
     */
42
43
    public function __construct($name, $arguments)
44
    {
45
        $this->name = $name;
46
47
        if (strpos($arguments, "{") === 0) {
48
               $this->arguments = (array) json_decode($arguments, true);
49
        } else $this->arguments = (array) trim($arguments);
50
51
        $this->filter();
52
    }
53
54
    /**
55
     * @return string
56
     */
57
58
    public function getName()
59
    {
60
        return $this->name;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
67
    public function getArguments()
68
    {
69
        return $this->arguments;
70
    }
71
72
    /**
73
     * @param string $name
74
     * @param string $default = null
75
     * @return string
76
     */
77
78
    public function getArgument($name, $default = null)
79
    {
80
        return isset($this->arguments[$name]) ? $this->arguments[$name] : $default;
81
    }
82
83
    /**
84
     * @return int
85
     */
86
87
    public function getArgumentCount()
88
    {
89
        return count($this->arguments);
90
    }
91
92
    /**
93
     * @param string $name
94
     * @return boolean
95
     */
96
97
    public function hasArgument($name)
98
    {
99
        return isset($this->arguments[$name]);
100
    }
101
102
    /**
103
     * Overwrite this method to parse and validate the arguments in a
104
     * new Annotation definition.
105
     *
106
     * @return void
107
     */
108
109
    protected function filter()
110
    {
111
        // void
112
    }
113
114
    /**
115
     * @return string
116
     */
117
118
    public function __toString()
119
    {
120
        if (count($this->arguments) === 1) {
121
            return (string) $this->arguments[0];
122
        }
123
124
        throw new Exceptions\AnnotationException("The annotation cannot be converted to string.");
125
    }
126
127
}
128