1: <?php
2: 3: 4: 5: 6: 7: 8: 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: 25:
26: abstract class TypeFactory extends AbstractTypeFactory
27: {
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: public static function setNumberType($requiredType)
41: {
42: parent::setNumberType($requiredType);
43: RationalTypeFactory::setNumberType($requiredType);
44: ComplexTypeFactory::setNumberType($requiredType);
45: }
46:
47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 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: 89: 90: 91: 92: 93: 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: 112: 113: 114: 115: 116: 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: 136: 137: 138: 139: 140:
141: public static function createString($value)
142: {
143: return new StringType($value);
144: }
145:
146: 147: 148: 149: 150: 151: 152:
153: public static function createBool($value)
154: {
155: return new BoolType($value);
156: }
157:
158: 159: 160: 161: 162: 163: 164:
165: public static function createDigit($value)
166: {
167: return new DigitType($value);
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177: 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: 192: 193: 194: 195: 196: 197: 198:
199: public static function createRational($numerator, $denominator = 1)
200: {
201: if ($numerator instanceof NumericTypeInterface) {
202: return $numerator->asRational();
203: }
204:
205: $denominator = (is_null($denominator) ? 1 : $denominator);
206: return RationalTypeFactory::create($numerator, $denominator);
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216: 217:
218: public static function createWhole($value)
219: {
220: return self::createSuperIntType($value, 'WholeIntType');
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230: 231:
232: public static function createNatural($value)
233: {
234: return self::createSuperIntType($value, 'NaturalIntType');
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244: 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: