|
17 | 17 |
|
18 | 18 | namespace TinCan; |
19 | 19 |
|
20 | | -class Version |
| 20 | +use InvalidArgumentException; |
| 21 | + |
| 22 | +/** |
| 23 | + * TinCan API Version |
| 24 | + * |
| 25 | + * @internal implemented similar to a type-safe enum |
| 26 | + * @package TinCan |
| 27 | + */ |
| 28 | +final class Version |
21 | 29 | { |
22 | | - private static $_supported = array( |
23 | | - "1.0.1", |
24 | | - "1.0.0" |
25 | | - //, "0.95" |
26 | | - ); |
| 30 | + /**#@+ |
| 31 | + * Value constants |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + const V101 = "1.0.1"; |
| 36 | + const V100 = "1.0.0"; |
| 37 | + const V095 = "0.95"; |
| 38 | + /**#@- */ |
| 39 | + |
| 40 | + /** @var array string => bool */ |
| 41 | + private static $supported = [ |
| 42 | + self::V101 => true, |
| 43 | + self::V100 => true, |
| 44 | + self::V095 => false |
| 45 | + ]; |
| 46 | + |
| 47 | + /** @var self[] */ |
| 48 | + private static $instances = []; |
| 49 | + |
| 50 | + /** @var string */ |
| 51 | + private $value; |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructor |
| 55 | + * |
| 56 | + * @param string $aValue a version value |
| 57 | + * @throws InvalidArgumentException when the value is not recognized |
| 58 | + */ |
| 59 | + private function __construct($aValue) { |
| 60 | + if (!isset(static::$supported[$aValue])) { |
| 61 | + throw new InvalidArgumentException("Invalid version [$aValue]"); |
| 62 | + } |
| 63 | + $this->value = $aValue; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Does the value match? |
| 68 | + * |
| 69 | + * @param string $aValue a value to check |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | + public function hasValue($aValue) { |
| 73 | + return $this->value === (string) $aValue; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Is the value contained in a list of versions? |
| 78 | + * |
| 79 | + * @param string[] $aValueList a list of values to check |
| 80 | + * @return bool |
| 81 | + */ |
| 82 | + public function hasAnyValue(array $aValueList) { |
| 83 | + return in_array($this->value, $aValueList); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Is this the latest version? |
| 88 | + * |
| 89 | + * @return bool |
| 90 | + */ |
| 91 | + public function isLatest() { |
| 92 | + return $this->value === static::latest(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Is this a supported version? |
| 97 | + * |
| 98 | + * @return bool |
| 99 | + */ |
| 100 | + public function isSupported() { |
| 101 | + return static::$supported[$this->value]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Convert the object to a string |
| 106 | + * |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + public function __toString() { |
| 110 | + return $this->value; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Factory constructor |
| 115 | + * |
| 116 | + * @example $version = Version::V101(); |
| 117 | + * @param string $aValue the called method as a version value |
| 118 | + * @param array $arguments unused arguments passed to the method |
| 119 | + * @return self |
| 120 | + */ |
| 121 | + public static function __callStatic($aValue, array $arguments = []) { |
| 122 | + $aValue = trim(preg_replace("#v(\d)(95|\d)(\d)?#i", '$1.$2.$3', $aValue), "."); |
| 123 | + if (!isset(static::$instances[$aValue])) { |
| 124 | + static::$instances[$aValue] = new static($aValue); |
| 125 | + } |
| 126 | + return static::$instances[$aValue]; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Convert a string into a Version instance |
| 131 | + * |
| 132 | + * @param string $aValue a version value |
| 133 | + * @return self |
| 134 | + */ |
| 135 | + public static function fromString($aValue) { |
| 136 | + $aValue = str_replace(".", "", $aValue); |
| 137 | + return static::{"V$aValue"}(); |
| 138 | + } |
27 | 139 |
|
| 140 | + /** |
| 141 | + * List all supported versions |
| 142 | + * |
| 143 | + * @return string[] |
| 144 | + */ |
28 | 145 | public static function supported() { |
29 | | - return self::$_supported; |
| 146 | + return array_keys(array_filter(static::$supported, function($supported) { |
| 147 | + return $supported === true; |
| 148 | + })); |
30 | 149 | } |
31 | 150 |
|
| 151 | + /** |
| 152 | + * Retrieve the most recent version |
| 153 | + * |
| 154 | + * @return string |
| 155 | + */ |
32 | 156 | public static function latest() { |
33 | | - return self::$_supported[0]; |
| 157 | + return array_keys(static::$supported)[0]; |
34 | 158 | } |
35 | 159 | } |
0 commit comments