Completed
Push — master ( de3e58...cd0c51 )
by Alex
02:03
created

Annotation::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
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 array
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 = json_decode($arguments, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($arguments, true) of type * is incompatible with the declared type array of property $arguments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
	 * @return string
74
	 */
75
76
	public function getArgument($name)
77
	{
78
		return isset($this->arguments[$name]) ? $this->arguments[$name] : null;
79
	}
80
81
	/**
82
	 * @return string
83
	 */
84
85
	public function getArgumentCount()
86
	{
87
		return count($this->arguments);
88
	}
89
90
	/**
91
	 * @return boolean
92
	 */
93
94
	public function hasArgument($name)
95
	{
96
		return isset($this->arguments[$name]);
97
	}
98
99
	/**
100
	 * Overwrite this method to parse and validate the arguments in a
101
	 * new Annotation definition.
102
	 *
103
	 * @return void
104
	 */
105
106
	protected function filter()
107
	{
108
		// void
109
	}
110
111
	public function __toString()
112
	{
113
		if (count($this->arguments) === 1) {
114
			return (string) $this->arguments[0];
115
		}
116
117
		throw new Exceptions\AnnotationException("The annotation cannot be converted to string.");
118
	}
119
120
}
121