1 | <?php |
||
31 | abstract class AbstractBinding implements Binding |
||
32 | { |
||
33 | /** |
||
34 | * @var Uuid |
||
35 | */ |
||
36 | private $uuid; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $typeName; |
||
42 | |||
43 | /** |
||
44 | * @var BindingType|null |
||
45 | */ |
||
46 | private $type; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $userParameterValues = array(); |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $parameterValues = array(); |
||
57 | |||
58 | /** |
||
59 | * Creates a new binding. |
||
60 | * |
||
61 | * You can pass parameters that have been defined for the type. If you pass |
||
62 | * unknown parameters, or if a required parameter is missing, an exception |
||
63 | * is thrown. |
||
64 | * |
||
65 | * All parameters that you do not set here will receive the default values |
||
66 | * set for the parameter. |
||
67 | * |
||
68 | * @param string $typeName The name of the type to bind against. |
||
69 | * @param array $parameterValues The values of the parameters defined |
||
70 | * for the type. |
||
71 | * @param Uuid|null $uuid The UUID of the binding. A new one is |
||
72 | * generated if none is passed. |
||
73 | * |
||
74 | * @throws NoSuchParameterException If an invalid parameter was passed. |
||
75 | * @throws MissingParameterException If a required parameter was not passed. |
||
76 | */ |
||
77 | 159 | public function __construct($typeName, array $parameterValues = array(), Uuid $uuid = null) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 130 | public function initialize(BindingType $type) |
|
93 | { |
||
94 | 130 | if ($this->typeName !== $type->getName()) { |
|
95 | 2 | throw new InvalidArgumentException(sprintf( |
|
96 | 2 | 'The passed type "%s" does not match the configured type "%s".', |
|
97 | 2 | $type->getName(), |
|
98 | 2 | $this->typeName |
|
99 | )); |
||
100 | } |
||
101 | |||
102 | 128 | if (!$type->acceptsBinding(get_class($this))) { |
|
103 | 7 | throw BindingNotAcceptedException::forBindingClass($type->getName(), get_class($this)); |
|
104 | } |
||
105 | |||
106 | // Merge default parameter values of the type |
||
107 | 121 | $this->assertParameterValuesValid($this->userParameterValues, $type); |
|
108 | |||
109 | 117 | $this->type = $type; |
|
110 | 117 | $this->parameterValues = array_replace($type->getParameterValues(), $this->userParameterValues); |
|
111 | |||
112 | 117 | ksort($this->parameterValues); |
|
113 | 117 | } |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 2 | public function isInitialized() |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 119 | public function getUuid() |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 122 | public function getTypeName() |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 4 | public function getType() |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 10 | public function getParameterValues($includeDefault = true) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function hasParameterValues($includeDefault = true) |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 32 | public function getParameterValue($parameterName, $includeDefault = true) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 10 | public function hasParameterValue($parameterName, $includeDefault = true) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 93 | public function serialize() |
|
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | 40 | public function unserialize($serialized) |
|
222 | |||
223 | 93 | protected function preSerialize(array &$data) |
|
229 | |||
230 | 40 | protected function postUnserialize(array &$data) |
|
237 | |||
238 | 121 | private function assertParameterValuesValid(array $parameterValues, BindingType $type) |
|
254 | } |
||
255 |