1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Factory; |
4
|
|
|
|
5
|
|
|
use \InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
// Local namespace dependencies |
8
|
|
|
use \Charcoal\Factory\AbstractFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class ResolverFactory extends AbstractFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string $resolverPrefix |
17
|
|
|
*/ |
18
|
|
|
private $resolverPrefix = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string $resolverSuffix |
22
|
|
|
*/ |
23
|
|
|
private $resolverSuffix = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array $resolverCapitals |
27
|
|
|
*/ |
28
|
|
|
private $resolverCapitals = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array $resolverReplacements |
32
|
|
|
*/ |
33
|
|
|
private $resolverReplacements = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $prefix The resolver prefix string. |
37
|
|
|
* @throws InvalidArgumentException If the prefix argument is not a string. |
38
|
|
|
* @return ResolverFactory Chainable |
39
|
|
|
*/ |
40
|
|
|
public function setResolverPrefix($prefix) |
41
|
|
|
{ |
42
|
|
|
if (!is_string($prefix)) { |
43
|
|
|
throw new InvalidArgumentException( |
44
|
|
|
'Prefix must be a string' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
$this->resolverPrefix = $prefix; |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function resolverPrefix() |
55
|
|
|
{ |
56
|
|
|
return $this->resolverPrefix; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $suffix The resolver suffix string. |
61
|
|
|
* @throws InvalidArgumentException If the suffix argument is not a string. |
62
|
|
|
* @return ResolverFactory Chainable |
63
|
|
|
*/ |
64
|
|
|
public function setResolverSuffix($suffix) |
65
|
|
|
{ |
66
|
|
|
if (!is_string($suffix)) { |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
'Prefix must be a string' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
$this->resolverSuffix = $suffix; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function resolverSuffix() |
79
|
|
|
{ |
80
|
|
|
return $this->resolverSuffix; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $capitals The array of letter to "calitalize-next" (uppercase next letter in the string). |
85
|
|
|
* @return ResolverFactory Chainable |
86
|
|
|
*/ |
87
|
|
|
public function setResolverCapitals(array $capitals) |
88
|
|
|
{ |
89
|
|
|
$this->resolverCapitals = $capitals; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function resolverCapitals() |
97
|
|
|
{ |
98
|
|
|
if ($this->resolverCapitals === null) { |
99
|
|
|
return [ |
100
|
|
|
'-', |
101
|
|
|
'\\', |
102
|
|
|
'/', |
103
|
|
|
'.', |
104
|
|
|
'_' |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
return $this->resolverCapitals; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $replacements The array (key=>value) of replacements. |
112
|
|
|
* @return ResolverFactory Chainable |
113
|
|
|
*/ |
114
|
|
|
public function setResolverReplacements(array $replacements) |
115
|
|
|
{ |
116
|
|
|
$this->resolverReplacements = $replacements; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function resolverReplacements() |
124
|
|
|
{ |
125
|
|
|
if ($this->resolverReplacements === null) { |
126
|
|
|
return [ |
127
|
|
|
'-'=>'', |
128
|
|
|
'/'=>'\\', |
129
|
|
|
'.'=>'_' |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
return $this->resolverReplacements; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Resolve the class name from the requested type. |
137
|
|
|
* |
138
|
|
|
* @param string $type The "type" of object to resolve (the object ident). |
139
|
|
|
* @throws InvalidArgumentException If the type parameter is not a string. |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function resolve($type) |
143
|
|
|
{ |
144
|
|
|
if (!is_string($type)) { |
145
|
|
|
throw new InvalidArgumentException( |
146
|
|
|
'Can not resolve class ident: type must be a string' |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$capitalize_next = function(&$i) { |
151
|
|
|
$i = ucfirst($i); |
152
|
|
|
}; |
153
|
|
|
|
154
|
|
|
$capitals = $this->resolverCapitals(); |
155
|
|
|
foreach ($capitals as $cap) { |
156
|
|
|
$expl = explode($cap, $type); |
157
|
|
|
array_walk($expl, $capitalize_next); |
158
|
|
|
$type = implode($cap, $expl); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$replacements = $this->resolverReplacements(); |
162
|
|
|
foreach ($replacements as $rep => $target) { |
163
|
|
|
$type = str_replace($rep, $target, $type); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$class = '\\'.trim($type, '\\'); |
167
|
|
|
|
168
|
|
|
// Add prefix + suffix, if applicable |
169
|
|
|
$class = $this->resolverPrefix().$class.$this->resolverSuffix(); |
170
|
|
|
|
171
|
|
|
return $class; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $type The "type" of object to resolve (the object ident). |
176
|
|
|
* @throws InvalidArgumentException If the type parameter is not a string. |
177
|
|
|
* @return boolean |
178
|
|
|
*/ |
179
|
|
|
public function isResolvable($type) |
180
|
|
|
{ |
181
|
|
|
if (!is_string($type)) { |
182
|
|
|
throw new InvalidArgumentException( |
183
|
|
|
'Can not check resolvable: type must be a string' |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$class_name = $this->resolve($type); |
188
|
|
|
return class_exists($class_name); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|