Overview

Namespaces

  • chippyash
    • Type
      • Exceptions
      • Interfaces
      • Number
        • Complex
        • Rational
      • String

Classes

  • AbstractMultiValueType
  • AbstractType
  • AbstractTypeFactory
  • BoolType
  • TypeFactory
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Hard type support
  4:  * For when you absolutely want to know what you are getting
  5:  *
  6:  * @author Ashley Kitson <akitson@zf4.biz>
  7:  * @copyright Ashley Kitson, UK, 2014
  8:  * @licence GPL V3 or later : http://www.gnu.org/licenses/gpl.html
  9:  *
 10:  */
 11: namespace chippyash\Type;
 12: 
 13: use chippyash\Type\Exceptions\InvalidTypeException;
 14: use chippyash\Type\String\StringType;
 15: use chippyash\Type\String\DigitType;
 16: use chippyash\Type\Number\IntType;
 17: use chippyash\Type\Number\GMPIntType;
 18: use chippyash\Type\Number\FloatType;
 19: use chippyash\Type\Number\Complex\ComplexTypeFactory;
 20: use chippyash\Type\Number\Rational\RationalTypeFactory;
 21: use chippyash\Type\Interfaces\NumericTypeInterface;
 22: 
 23: /**
 24:  * Static Factory for creating types
 25:  */
 26: abstract class TypeFactory extends AbstractTypeFactory
 27: {
 28:     /**
 29:      * Set the required number type to return
 30:      * By default this is self::TYPE_DEFAULT  which is 'auto', meaning that
 31:      * the factory will determine if GMP is installed and use that else use
 32:      * PHP native types
 33:      *
 34:      * @param string $requiredType
 35:      *
 36:      * @return void
 37:      *
 38:      * @throws \InvalidArgumentException
 39:      */
 40:     public static function setNumberType($requiredType)
 41:     {
 42:         parent::setNumberType($requiredType);
 43:         RationalTypeFactory::setNumberType($requiredType);
 44:         ComplexTypeFactory::setNumberType($requiredType);
 45:     }
 46: 
 47:     /**
 48:      * Generic type factory
 49:      *
 50:      * @param string $type
 51:      * @param mixed $value
 52:      * @param mixed $extra required for some types
 53:      *
 54:      * @return \chippyash\Type\AbstractType
 55:      *
 56:      * @throws InvalidTypeException
 57:      */
 58:     public static function create($type, $value, $extra = null)
 59:     {
 60:         switch (strtolower($type)) {
 61:             case 'int':
 62:             case 'integer':
 63:                 return self::createInt($value);
 64:             case 'float':
 65:             case 'double':
 66:                 return self::createFloat($value);
 67:             case 'string':
 68:                 return self::createString($value);
 69:             case 'bool':
 70:             case 'boolean':
 71:                 return self::createBool($value);
 72:             case 'digit':
 73:                 return self::createDigit($value);
 74:             case 'natural':
 75:                 return self::createNatural($value);
 76:             case 'whole':
 77:                 return self::createWhole($value);
 78:             case 'complex':
 79:                 return self::createComplex($value, $extra);
 80:             case 'rational':
 81:                 return self::createRational($value, $extra);
 82:             default:
 83:                 throw new InvalidTypeException(strtolower($type));
 84:         }
 85:     }
 86: 
 87:     /**
 88:      * Create an IntType
 89:      *
 90:      * @param mixed $value
 91:      * @return \chippyash\Type\Number\IntType
 92:      *
 93:      * @throws \InvalidArgumentException
 94:      */
 95:     public static function createInt($value)
 96:     {
 97:         if ($value instanceof NumericTypeInterface) {
 98:             return $value->asIntType();
 99:         }
100:         if (!is_numeric($value)) {
101:             throw new \InvalidArgumentException("'{$value}' is no valid numeric for IntType");
102:         }
103:         if (self::getRequiredType() == self::TYPE_GMP) {
104:             return new GMPIntType($value);
105:         } else {
106:             return new IntType($value);
107:         }
108:     }
109: 
110:     /**
111:      * Create a FloatType
112:      *
113:      * @param mixed $value
114:      * @return \chippyash\Type\Number\FloatType|\chippyash\Type\Number\Rational\GMPRationalType
115:      *
116:      * @throws \InvalidArgumentException
117:      */
118:     public static function createFloat($value)
119:     {
120:         if ($value instanceof NumericTypeInterface) {
121:             return $value->asFloatType();
122:         }
123:         if (!is_numeric($value)) {
124:             throw new \InvalidArgumentException("'{$value}' is no valid numeric for FloatType");
125:         }
126:         
127:         if (self::getRequiredType() == self::TYPE_GMP) {
128:             return RationalTypeFactory::create($value);
129:         } else {
130:             return new FloatType($value);
131:         }
132:     }
133: 
134:     /**
135:      * Create a StringType
136:      *
137:      * @param mixed $value
138:      *
139:      * @return \chippyash\Type\String\StringType
140:      */
141:     public static function createString($value)
142:     {
143:         return new StringType($value);
144:     }
145: 
146:     /**
147:      * Create a BoolType
148:      *
149:      * @param mixed $value
150:      *
151:      * @return \chippyash\Type\BoolType
152:      */
153:     public static function createBool($value)
154:     {
155:         return new BoolType($value);
156:     }
157: 
158:     /**
159:      * Create a DigitType
160:      *
161:      * @param mixed $value
162:      *
163:      * @return \chippyash\Type\String\DigitType
164:      */
165:     public static function createDigit($value)
166:     {
167:         return new DigitType($value);
168:     }
169: 
170:     /**
171:      * Create a Complex number
172:      * If imaginary part is null, a complex equivalent real number is created r+0i
173:      *
174:      * @param int|float|string|NumericTypeInterface $realPart
175:      * @param int|float|string|NumericTypeInterface|null $imaginaryPart
176:      *
177:      * @return \chippyash\Type\Number\Complex\ComplexType
178:      */
179:     public static function createComplex($realPart, $imaginaryPart = null)
180:     {
181:         if ($realPart instanceof NumericTypeInterface) {
182:             return $realPart->asComplex();
183:         }
184:         if (!is_string($realPart) && is_null($imaginaryPart)) {
185:             return ComplexTypeFactory::create($realPart, 0);
186:         }
187:         return ComplexTypeFactory::create($realPart, $imaginaryPart);
188:     }
189: 
190:     /**
191:      * Create a Rational number
192:      * @see RationalTypeFactory::create
193:      *
194:      * @param int|string|float $numerator
195:      * @param int $denominator
196:      *
197:      * @return \chippyash\Type\Number\Rational\RationalType
198:      */
199:     public static function createRational($numerator, $denominator = 1)
200:     {
201:         if ($numerator instanceof NumericTypeInterface) {
202:             return $numerator->asRational();
203:         }
204:         //check because the create() method can pass in a null
205:         $denominator = (is_null($denominator) ? 1 : $denominator);
206:         return RationalTypeFactory::create($numerator, $denominator);
207:     }
208: 
209:     /**
210:      * Create a whole number
211:      *
212:      * @param mixed $value
213:      *
214:      * @return \chippyash\Type\Number\WholeIntType|\chippyash\Type\Number\GMPIntType
215:      *
216:      * @throws \InvalidArgumentException
217:      */
218:     public static function createWhole($value)
219:     {
220:         return self::createSuperIntType($value, 'WholeIntType');
221:     }
222: 
223:     /**
224:      * Create a Natural number
225:      *
226:      * @param mixed $value
227:      *
228:      * @return \chippyash\Type\Number\NaturalIntType|\chippyash\Type\Number\GMPIntType
229:      *
230:      * @throws \InvalidArgumentException
231:      */
232:     public static function createNatural($value)
233:     {
234:         return self::createSuperIntType($value, 'NaturalIntType');
235:     }
236: 
237:     /**
238:      * Create a super int type (whole, natural)
239:      *
240:      * @param mixed $value
241:      *
242:      * @param string $typeClassName
243:      *
244:      * @return GMPIntType
245:      */
246:     protected static function createSuperIntType($value, $typeClassName)
247:     {
248:         if ($value instanceof NumericTypeInterface) {
249:             $value = $value->asIntType()->get();
250:         }
251:         if (!is_numeric($value)) {
252:             throw new \InvalidArgumentException("'{$value}' is no valid numeric for {$typeClassName}");
253:         }
254: 
255:         if (self::getRequiredType() == self::TYPE_GMP) {
256:             return new GMPIntType($value);
257:         } else {
258:             $nsp = __NAMESPACE__;
259:             $className = "{$nsp}\\Number\\{$typeClassName}";
260:             return new $className($value);
261:         }
262:     }
263: }
264: 
Chippyash Strong Types API documentation generated by ApiGen 2.8.0