1 | <?php |
||
15 | class MapFactory extends AbstractFactory |
||
16 | { |
||
17 | /** |
||
18 | * The class map array holds available types, in `[$type => $className]` format. |
||
19 | * @var array $map |
||
20 | */ |
||
21 | private $map = []; |
||
22 | |||
23 | public function __construct($data=null) |
||
31 | |||
32 | /** |
||
33 | * Add a class name to the available types _map_. |
||
34 | * |
||
35 | * @param string $type The type (class ident). |
||
36 | * @param string $className The FQN of the class. |
||
37 | * @throws InvalidArgumentException If the $type parameter is not a striing or the $className class does not exist. |
||
38 | * @return FactoryInterface Chainable |
||
39 | */ |
||
40 | public function addClass($type, $className) |
||
56 | |||
57 | /** |
||
58 | * Add multiple types, in a an array of `type` => `className`. |
||
59 | * |
||
60 | * @param array $map The map (key=>classname) to use. |
||
61 | * @return FactoryInterface Chainable |
||
62 | */ |
||
63 | public function setMap(array $map) |
||
72 | |||
73 | /** |
||
74 | * Get the map of all types in `[$type => $class]` format. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function map() |
||
82 | |||
83 | /** |
||
84 | * The "Map Factory" implements `AbstractFactory`'s `resolve()` abstract method |
||
85 | * by fetching the class ident from the `map` member array. |
||
86 | * |
||
87 | * If the object's `type` is not defined in the class map, an exception will be thrown. |
||
88 | * |
||
89 | * @param string $type The "type" of object to resolve (the object ident). |
||
90 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
91 | * @return string The resolved class name (FQN). |
||
92 | */ |
||
93 | public function resolve($type) |
||
109 | |||
110 | /** |
||
111 | * The "Map Factory" implements `AbstractFactory`'s `is_resolvable()` abstract method |
||
112 | * by ensuring the class ident is defined in the class map and is a validd class. |
||
113 | * |
||
114 | * @param string $type The "type" of object to resolve (the object ident). |
||
115 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
116 | * @return boolean |
||
117 | */ |
||
118 | public function isResolvable($type) |
||
134 | } |
||
135 |