1: <?php
2: /**
3: * Strong-Type
4: *
5: * @author Ashley Kitson
6: * @copyright Ashley Kitson, 2015, UK
7: * @license GPL V3+ See LICENSE.md
8: */
9:
10: namespace chippyash\Type;
11:
12: /**
13: * Abstract base type for factories
14: * Handles gmp/native type determination
15: */
16: abstract class AbstractTypeFactory
17: {
18: /**@+
19: * Numeric base types
20: */
21: const TYPE_DEFAULT = 'auto';
22: const TYPE_NATIVE = 'native';
23: const TYPE_GMP = 'gmp';
24: /**@-*/
25:
26: /**
27: * Client requested numeric base type support
28: * @var string
29: */
30: protected static $supportType = self::TYPE_DEFAULT;
31:
32: /**
33: * Numeric base types we can support
34: * @var array
35: */
36: protected static $validTypes = array(self::TYPE_DEFAULT, self::TYPE_GMP, self::TYPE_NATIVE);
37:
38: /**
39: * The actual base type we are going to return
40: * @var string
41: */
42: protected static $requiredType = null;
43:
44: /**
45: * Set the required number type to return
46: * By default this is self::TYPE_DEFAULT which is 'auto', meaning that
47: * the factory will determine if GMP is installed and use that else use
48: * PHP native types
49: *
50: * @param string $requiredType
51: *
52: * @return void
53: *
54: * @throws \InvalidArgumentException
55: */
56: public static function setNumberType($requiredType)
57: {
58: if (!in_array($requiredType, self::$validTypes)) {
59: throw new \InvalidArgumentException("{$requiredType} is not a supported number type");
60: }
61: if ($requiredType == self::TYPE_GMP && !extension_loaded('gmp')) {
62: throw new \InvalidArgumentException('GMP not supported');
63: }
64: self::$supportType = $requiredType;
65: }
66:
67: /**
68: * Get the required type base to return
69: *
70: * @return string
71: */
72: protected static function getRequiredType()
73: {
74: if (self::$requiredType != null) {
75: return self::$requiredType;
76: }
77:
78: if (self::$supportType == self::TYPE_DEFAULT) {
79: if (extension_loaded('gmp')) {
80: self::$requiredType = self::TYPE_GMP;
81: } else {
82: self::$requiredType = self::TYPE_NATIVE;
83: }
84: } else {
85: self::$requiredType = self::$supportType;
86: }
87:
88: return self::$requiredType;
89: }
90:
91: }
92: