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