1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Factory; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class GenericResolver |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string $prefix |
12
|
|
|
*/ |
13
|
|
|
private $prefix = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string $suffix |
17
|
|
|
*/ |
18
|
|
|
private $suffix = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array $capitals |
22
|
|
|
*/ |
23
|
|
|
private $capitals; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array $replacements |
27
|
|
|
*/ |
28
|
|
|
private $replacements; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $data Optional class dependencies. Will use default values if none are provided. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $data = null) |
34
|
|
|
{ |
35
|
|
|
if (!isset($data['prefix'])) { |
36
|
|
|
$data['prefix'] = ''; |
37
|
|
|
} |
38
|
|
|
if (!isset($data['suffix'])) { |
39
|
|
|
$data['suffix'] = ''; |
40
|
|
|
} |
41
|
|
View Code Duplication |
if (!isset($data['capitals'])) { |
|
|
|
|
42
|
|
|
$data['capitals'] = [ |
43
|
|
|
'-', |
44
|
|
|
'\\', |
45
|
|
|
'/', |
46
|
|
|
'.', |
47
|
|
|
'_' |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
View Code Duplication |
if (!isset($data['replacements'])) { |
|
|
|
|
51
|
|
|
$data['replacements'] = [ |
52
|
|
|
'-'=>'', |
53
|
|
|
'/'=>'\\', |
54
|
|
|
'.'=>'_' |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
$this->prefix = $data['prefix']; |
58
|
|
|
$this->suffix = $data['suffix']; |
59
|
|
|
$this->capitals = $data['capitals']; |
60
|
|
|
$this->replacements = $data['replacements']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Resolver needs to be callable |
65
|
|
|
* |
66
|
|
|
* @param string $type The "type" of object to resolve (the object ident). |
67
|
|
|
* @return string The resolved class name (FQN). |
68
|
|
|
*/ |
69
|
|
|
public function __invoke($type) |
70
|
|
|
{ |
71
|
|
|
return $this->resolve($type); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Resolve the class name from the requested type. |
76
|
|
|
* |
77
|
|
|
* @param string $type The "type" of object to resolve (the object ident). |
78
|
|
|
* @throws InvalidArgumentException If the type parameter is not a string. |
79
|
|
|
* @return string The resolved class name (FQN). |
80
|
|
|
*/ |
81
|
|
|
public function resolve($type) |
82
|
|
|
{ |
83
|
|
|
if (!is_string($type)) { |
84
|
|
|
throw new InvalidArgumentException( |
85
|
|
|
'Can not resolve class ident: type must be a string' |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Normalize requested type with prefix / suffix, if applicable. |
90
|
|
|
$type = $this->prefix.$type.$this->suffix; |
91
|
|
|
|
92
|
|
|
$capitalizeNext = function(&$i) { |
93
|
|
|
$i = ucfirst($i); |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$capitals = $this->capitals; |
97
|
|
View Code Duplication |
foreach ($capitals as $cap) { |
|
|
|
|
98
|
|
|
$expl = explode($cap, $type); |
99
|
|
|
array_walk($expl, $capitalizeNext); |
100
|
|
|
$type = implode($cap, $expl); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$replacements = $this->replacements; |
104
|
|
|
foreach ($replacements as $rep => $target) { |
105
|
|
|
$type = str_replace($rep, $target, $type); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$class = '\\'.trim($type, '\\'); |
109
|
|
|
|
110
|
|
|
return $class; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.