1 | <?php |
||
29 | abstract class AbstractContainer implements Container |
||
30 | { |
||
31 | /** |
||
32 | * @var Container The parent container |
||
33 | */ |
||
34 | protected $parent; |
||
35 | /** |
||
36 | * @var string[] with string keys |
||
37 | */ |
||
38 | protected $types = []; |
||
39 | /** |
||
40 | * @var string[] the list of PHP native types |
||
41 | */ |
||
42 | protected static $primitives = ['array', 'bool', 'float', 'int', 'resource', 'string']; |
||
43 | |||
44 | /** |
||
45 | * Creates a new AbstractContainer. |
||
46 | * |
||
47 | * @param string[] $types with string keys |
||
48 | * @param \Caridea\Container\Container $parent The parent container |
||
49 | */ |
||
50 | protected function __construct(array $types, Container $parent = null) |
||
55 | |||
56 | /** |
||
57 | * Whether this container or its parent contains a component with the given name. |
||
58 | * |
||
59 | * @param string $name The component name |
||
60 | * @return bool |
||
61 | */ |
||
62 | public function contains(string $name): bool |
||
67 | |||
68 | /** |
||
69 | * Whether this container or its parent contains a component with the given type. |
||
70 | * |
||
71 | * @param string $type The name of a class or one of PHP's language types |
||
72 | * (i.e. bool, int, float, string, array, resource) |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function containsType(string $type): bool |
||
88 | |||
89 | /** |
||
90 | * Gets a component by name. |
||
91 | * |
||
92 | * If this container doesn't have a value for that name, it will delegate to |
||
93 | * its parent. |
||
94 | * |
||
95 | * @param string $name The component name |
||
96 | * @return mixed The component or null if the name isn't registered |
||
97 | */ |
||
98 | public function get(string $name) |
||
103 | |||
104 | /** |
||
105 | * Gets the components in the contanier for the given type. |
||
106 | * |
||
107 | * The parent container is called first, then any values of this container |
||
108 | * are appended to the array. Values in this container supercede any with |
||
109 | * duplicate names in the parent. |
||
110 | * |
||
111 | * @param string $type The name of a class or one of PHP's language types |
||
112 | * (i.e. bool, int, float, string, array, resource) |
||
113 | * @return array keys are component names, values are components themselves |
||
114 | */ |
||
115 | public function getByType(string $type): array |
||
126 | |||
127 | /** |
||
128 | * Gets the first compenent found by type. |
||
129 | * |
||
130 | * If this container doesn't have a value of the type, it will delegate to |
||
131 | * its parent. |
||
132 | * |
||
133 | * @param string $type The name of a class or one of PHP's language types |
||
134 | * (i.e. bool, int, float, string, array, resource) |
||
135 | * @return mixed The component or null if one isn't registered |
||
136 | */ |
||
137 | public function getFirst(string $type) |
||
147 | |||
148 | /** |
||
149 | * Retrieves the value |
||
150 | * |
||
151 | * @param string $name The value name |
||
152 | */ |
||
153 | abstract protected function doGet(string $name); |
||
154 | |||
155 | /** |
||
156 | * Gets all registered component names (excluding any in the parent container). |
||
157 | * |
||
158 | * @return array of strings |
||
159 | */ |
||
160 | public function getNames(): array |
||
164 | |||
165 | /** |
||
166 | * Gets the parent container. |
||
167 | * |
||
168 | * @return Container |
||
169 | */ |
||
170 | public function getParent() |
||
174 | |||
175 | /** |
||
176 | * Gets the type of component with the given name. |
||
177 | * |
||
178 | * If this container doesn't have a value for that name, it will delegate to |
||
179 | * its parent. |
||
180 | * |
||
181 | * @param string $name The component name |
||
182 | * @return string The component type, either a class name or one of PHP's language types |
||
183 | * (i.e. bool, int, float, string, array, resource) |
||
184 | */ |
||
185 | public function getType(string $name) |
||
190 | |||
191 | /** |
||
192 | * Gets a component by name and ensures its type. |
||
193 | * |
||
194 | * If this container doesn't have a value for that name, it will delegate to |
||
195 | * its parent. |
||
196 | * |
||
197 | * If the value isn't an instance of the type provided, an exception is |
||
198 | * thrown, including when the value is `null`. |
||
199 | * |
||
200 | * @param string $name The component name |
||
201 | * @param string $type The expected type |
||
202 | * @return mixed The type-checked component |
||
203 | * @throws \UnexpectedValueException if the `$type` doesn't match the value |
||
204 | */ |
||
205 | public function named(string $name, string $type) |
||
219 | } |
||
220 |