1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: root |
5
|
|
|
* Date: 02.08.16 |
6
|
|
|
* Time: 0:46. |
7
|
|
|
*/ |
8
|
|
|
namespace samsonframework\container\definition; |
9
|
|
|
|
10
|
|
|
use samsonframework\container\definition\exception\ParameterDefinitionAlreadyExistsException; |
11
|
|
|
use samsonframework\container\definition\exception\ParameterDefinitionNotFoundException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MethodDefinition |
15
|
|
|
* |
16
|
|
|
* @package samsonframework\container\definition |
17
|
|
|
*/ |
18
|
|
|
class MethodDefinition extends AbstractDefinition implements MethodBuilderInterface |
19
|
|
|
{ |
20
|
|
|
/** @var string Method name */ |
21
|
|
|
protected $methodName; |
22
|
|
|
/** @var ParameterDefinition[] Collection of parameter collection */ |
23
|
|
|
protected $parametersCollection = []; |
24
|
|
|
/** @var int Method modifiers */ |
25
|
|
|
protected $modifiers = 0; |
26
|
|
|
/** @var bool Flag that method is public */ |
27
|
|
|
protected $isPublic = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Define arguments |
31
|
|
|
* |
32
|
|
|
* @param string $parameterName |
33
|
|
|
* @return ParameterBuilderInterface |
34
|
|
|
* @throws ParameterDefinitionAlreadyExistsException |
35
|
|
|
*/ |
36
|
13 |
|
public function defineParameter($parameterName): ParameterBuilderInterface |
37
|
|
|
{ |
38
|
13 |
|
if (array_key_exists($parameterName, $this->parametersCollection)) { |
39
|
1 |
|
throw new ParameterDefinitionAlreadyExistsException(); |
40
|
|
|
} |
41
|
|
|
|
42
|
13 |
|
$parameter = new ParameterDefinition($this); |
43
|
13 |
|
$parameter->setParameterName($parameterName); |
44
|
|
|
|
45
|
13 |
|
$this->parametersCollection[$parameterName] = $parameter; |
46
|
|
|
|
47
|
13 |
|
return $parameter; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
2 |
|
public function getMethodName(): string |
54
|
|
|
{ |
55
|
2 |
|
return $this->methodName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return boolean |
60
|
|
|
*/ |
61
|
3 |
|
public function isPublic(): bool |
62
|
|
|
{ |
63
|
3 |
|
return $this->isPublic; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param boolean $isPublic |
68
|
|
|
* @return MethodDefinition |
69
|
|
|
*/ |
70
|
4 |
|
public function setIsPublic(bool $isPublic): MethodDefinition |
71
|
|
|
{ |
72
|
4 |
|
$this->isPublic = $isPublic; |
73
|
|
|
|
74
|
4 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $methodName |
79
|
|
|
* @return MethodDefinition |
80
|
|
|
*/ |
81
|
16 |
|
public function setMethodName(string $methodName): MethodDefinition |
82
|
|
|
{ |
83
|
16 |
|
$this->methodName = $methodName; |
84
|
|
|
|
85
|
16 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
1 |
|
public function getModifiers(): int |
92
|
|
|
{ |
93
|
1 |
|
return $this->modifiers; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $modifiers |
98
|
|
|
* @return MethodDefinition |
99
|
|
|
*/ |
100
|
4 |
|
public function setModifiers(int $modifiers): MethodDefinition |
101
|
|
|
{ |
102
|
4 |
|
$this->modifiers = $modifiers; |
103
|
|
|
|
104
|
4 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return ParameterDefinition[] |
109
|
|
|
*/ |
110
|
4 |
|
public function getParametersCollection(): array |
111
|
|
|
{ |
112
|
4 |
|
return $this->parametersCollection; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Has parameter definition |
117
|
|
|
* |
118
|
|
|
* @param string $parameterName |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
4 |
|
public function hasParameter(string $parameterName): bool |
122
|
|
|
{ |
123
|
4 |
|
return array_key_exists($parameterName, $this->parametersCollection); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get property definition |
128
|
|
|
* |
129
|
|
|
* @param $parameterName |
130
|
|
|
* @return ParameterDefinition |
131
|
|
|
* @throws ParameterDefinitionNotFoundException |
132
|
|
|
*/ |
133
|
4 |
|
public function getParameter($parameterName): ParameterDefinition |
134
|
|
|
{ |
135
|
4 |
|
if (!$this->hasParameter($parameterName)) { |
136
|
|
|
throw new ParameterDefinitionNotFoundException(); |
137
|
|
|
} |
138
|
4 |
|
return $this->parametersCollection[$parameterName]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get existing or define new parameter |
143
|
|
|
* |
144
|
|
|
* @param string $parameterName |
145
|
|
|
* @return ParameterDefinition |
146
|
|
|
* @throws ParameterDefinitionNotFoundException |
147
|
|
|
* @throws ParameterDefinitionAlreadyExistsException |
148
|
|
|
*/ |
149
|
4 |
|
public function setupParameter(string $parameterName): ParameterDefinition |
150
|
|
|
{ |
151
|
|
|
// Get existing parameter |
152
|
4 |
|
if ($this->hasParameter($parameterName)) { |
153
|
4 |
|
return $this->getParameter($parameterName); |
154
|
|
|
} else { // Or define new parameter |
155
|
1 |
|
return $this->defineParameter($parameterName); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|