1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
/**
|
3
|
|
|
* Created by Ruslan Molodyko.
|
4
|
|
|
* Date: 11.09.2016
|
5
|
|
|
* Time: 14:47
|
6
|
|
|
*/
|
7
|
|
|
namespace samsonframework\container\definition\parameter;
|
8
|
|
|
|
9
|
|
|
use samsonframework\container\definition\AbstractDefinition;
|
10
|
|
|
use samsonframework\container\definition\parameter\exception\ParameterAlreadyExistsException;
|
11
|
|
|
use samsonframework\container\definition\parameter\exception\ParameterNotFoundException;
|
12
|
|
|
use samsonframework\container\definition\reference\ReferenceInterface;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Class ParameterBuilder
|
16
|
|
|
*
|
17
|
|
|
* @author Ruslan Molodyko <[email protected]>
|
18
|
|
|
*/
|
19
|
|
|
class ParameterBuilder extends AbstractDefinition implements ParameterBuilderInterface
|
20
|
|
|
{
|
21
|
|
|
/** @var array */
|
22
|
|
|
protected $parameterCollection = [];
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* Define parameter
|
26
|
|
|
*
|
27
|
|
|
* @param string $name
|
28
|
|
|
* @param ReferenceInterface $reference
|
29
|
|
|
* @return ParameterBuilderInterface
|
30
|
|
|
* @throws ParameterAlreadyExistsException
|
31
|
|
|
*/
|
32
|
13 |
|
public function defineParameter(string $name, ReferenceInterface $reference): ParameterBuilderInterface
|
33
|
|
|
{
|
34
|
13 |
|
return $this->add($name, $reference);
|
35
|
|
|
}
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Change parameter
|
39
|
|
|
*
|
40
|
|
|
* @param string $name
|
41
|
|
|
* @param ReferenceInterface $reference
|
42
|
|
|
* @return ParameterBuilder
|
43
|
|
|
* @throws ParameterNotFoundException
|
44
|
|
|
*/
|
45
|
2 |
View Code Duplication |
public function changeParameter(string $name, ReferenceInterface $reference): ParameterBuilder
|
|
|
|
|
46
|
|
|
{
|
47
|
2 |
|
if (!$this->has($name)) {
|
48
|
1 |
|
throw new ParameterNotFoundException(sprintf('Parameter with name "%s" not found', $name));
|
49
|
|
|
}
|
50
|
1 |
|
$this->parameterCollection[$name] = $reference;
|
51
|
|
|
|
52
|
1 |
|
return $this;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Add parameter to collection
|
57
|
|
|
*
|
58
|
|
|
* @param string $name
|
59
|
|
|
* @param ReferenceInterface $reference
|
60
|
|
|
* @return ParameterBuilder
|
61
|
|
|
* @throws ParameterAlreadyExistsException
|
62
|
|
|
*/
|
63
|
15 |
View Code Duplication |
public function add(string $name, ReferenceInterface $reference): ParameterBuilder
|
|
|
|
|
64
|
|
|
{
|
65
|
15 |
|
if ($this->has($name)) {
|
66
|
2 |
|
throw new ParameterAlreadyExistsException(sprintf('Parameter with name "%s" already defined', $name));
|
67
|
|
|
}
|
68
|
15 |
|
$this->parameterCollection[$name] = $reference;
|
69
|
|
|
|
70
|
15 |
|
return $this;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Has parameter in collection
|
75
|
|
|
*
|
76
|
|
|
* @param string $name
|
77
|
|
|
* @return bool
|
78
|
|
|
*/
|
79
|
15 |
|
public function has(string $name): bool
|
80
|
|
|
{
|
81
|
15 |
|
return array_key_exists($name, $this->parameterCollection);
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* Remove parameter
|
86
|
|
|
*
|
87
|
|
|
* @param string $name
|
88
|
|
|
* @throws ParameterNotFoundException
|
89
|
|
|
*/
|
90
|
2 |
View Code Duplication |
public function remove(string $name)
|
|
|
|
|
91
|
|
|
{
|
92
|
2 |
|
if (!$this->has($name)) {
|
93
|
1 |
|
throw new ParameterNotFoundException(sprintf('Parameter with name "%s" not found', $name));
|
94
|
|
|
}
|
95
|
2 |
|
unset($this->parameterCollection[$name]);
|
96
|
2 |
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* Get parameter
|
100
|
|
|
*
|
101
|
|
|
* @param string $name
|
102
|
|
|
* @return ReferenceInterface
|
103
|
|
|
* @throws ParameterNotFoundException
|
104
|
|
|
*/
|
105
|
5 |
View Code Duplication |
public function get(string $name): ReferenceInterface
|
|
|
|
|
106
|
|
|
{
|
107
|
5 |
|
if (!$this->has($name)) {
|
108
|
1 |
|
throw new ParameterNotFoundException(sprintf('Parameter with name "%s" not found', $name));
|
109
|
|
|
}
|
110
|
4 |
|
return $this->parameterCollection[$name];
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* Get parameter collection
|
115
|
|
|
*
|
116
|
|
|
* @return ReferenceInterface[]
|
117
|
|
|
*/
|
118
|
9 |
|
public function getParameterCollection(): array
|
119
|
|
|
{
|
120
|
9 |
|
return $this->parameterCollection;
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* Set parent definition
|
125
|
|
|
*
|
126
|
|
|
* @param AbstractDefinition $definition
|
127
|
|
|
*/
|
128
|
18 |
|
public function setParentDefinition(AbstractDefinition $definition)
|
129
|
|
|
{
|
130
|
18 |
|
$this->parentDefinition = $definition;
|
131
|
18 |
|
}
|
132
|
|
|
}
|
133
|
|
|
|