Overview

Namespaces

  • Charcoal
    • Factory

Classes

  • Charcoal\Factory\AbstractFactory
  • Charcoal\Factory\GenericFactory
  • Charcoal\Factory\GenericResolver
  • Charcoal\Factory\MapFactory
  • Charcoal\Factory\ResolverFactory

Interfaces

  • Charcoal\Factory\FactoryInterface
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\Factory;
  4: 
  5: use \InvalidArgumentException;
  6: 
  7: // Local namespace dependencies
  8: use \Charcoal\Factory\AbstractFactory;
  9: 
 10: /**
 11:  * The Resolver Factory resolves the **class name** by different configurably
 12:  * methods applied to the **type**.
 13:  */
 14: class ResolverFactory extends AbstractFactory
 15: {
 16:     /**
 17:      * @var string $resolverPrefix
 18:      */
 19:     private $resolverPrefix = '';
 20: 
 21:     /**
 22:      * @var string $resolverSuffix
 23:      */
 24:     private $resolverSuffix = '';
 25: 
 26:     /**
 27:      * @var array $resolverCapitals
 28:      */
 29:     private $resolverCapitals;
 30: 
 31:     /**
 32:      * @var array $resolverReplacements
 33:      */
 34:     private $resolverReplacements;
 35: 
 36:     /**
 37:      * @param array $data Factory arguments.
 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:      * @param string $prefix The resolver prefix string.
 73:      * @throws InvalidArgumentException If the prefix argument is not a string.
 74:      * @return ResolverFactory Chainable
 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:      * @return string
 89:      */
 90:     public function resolverPrefix()
 91:     {
 92:         return $this->resolverPrefix;
 93:     }
 94: 
 95:     /**
 96:      * @param string $suffix The resolver suffix string.
 97:      * @throws InvalidArgumentException If the suffix argument is not a string.
 98:      * @return ResolverFactory Chainable
 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:      * @return string
113:      */
114:     public function resolverSuffix()
115:     {
116:         return $this->resolverSuffix;
117:     }
118: 
119:     /**
120:      * @param array $capitals The array of letter to "calitalize-next" (uppercase next letter in the string).
121:      * @return ResolverFactory Chainable
122:      */
123:     public function setResolverCapitals(array $capitals)
124:     {
125:         $this->resolverCapitals = $capitals;
126:         return $this;
127:     }
128: 
129:     /**
130:      * @return array
131:      */
132:     public function resolverCapitals()
133:     {
134:         return $this->resolverCapitals;
135:     }
136: 
137:     /**
138:      * @param array $replacements The array (key=>value) of replacements.
139:      * @return ResolverFactory Chainable
140:      */
141:     public function setResolverReplacements(array $replacements)
142:     {
143:         $this->resolverReplacements = $replacements;
144:         return $this;
145:     }
146: 
147:     /**
148:      * @return array
149:      */
150:     public function resolverReplacements()
151:     {
152:         return $this->resolverReplacements;
153:     }
154: 
155:     /**
156:      * Resolve the class name from the requested type.
157:      *
158:      * @param string $type The "type" of object to resolve (the object ident).
159:      * @throws InvalidArgumentException If the type parameter is not a string.
160:      * @return string The resolved class name (FQN).
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:         // Add prefix + suffix, if applicable
189:         $class = $this->resolverPrefix().$class.$this->resolverSuffix();
190: 
191:         return $class;
192:     }
193: 
194:     /**
195:      * @param string $type The "type" of object to resolve (the object ident).
196:      * @throws InvalidArgumentException If the type parameter is not a string.
197:      * @return boolean
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: 
API documentation generated by ApiGen