3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Copyright 2014 Rustici Software Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ namespace TinCan; use InvalidArgumentException; /** * TinCan API Version * * @internal implemented similar to a type-safe enum * @package TinCan */ final class Version { /**#@+ * Value constants * * @var string */ const V101 = "1.0.1"; const V100 = "1.0.0"; const V095 = "0.95"; /**#@- */ /** @var array string => bool */ private static $supported = [ self::V101 => true, self::V100 => true, self::V095 => false ]; /** @var self[] */ private static $instances = []; /** @var string */ private $value; /** * Constructor * * @param string $aValue a version value * @throws InvalidArgumentException when the value is not recognized */ private function __construct($aValue) { if (!isset(static::$supported[$aValue])) { throw new InvalidArgumentException("Invalid version [$aValue]"); } $this->value = $aValue; } /** * Does the value match? * * @param string $aValue a value to check * @return bool */ public function hasValue($aValue) { return $this->value === (string) $aValue; } /** * Is the value contained in a list of versions? * * @param string[] $aValueList a list of values to check * @return bool */ public function hasAnyValue(array $aValueList) { return in_array($this->value, $aValueList); } /** * Is this the latest version? * * @return bool */ public function isLatest() { return $this === static::latest(); } /** * Is this a supported version? * * @return bool */ public function isSupported() { return static::$supported[$this->value]; } /** * Convert the object to a string * * @return string */ public function __toString() { return $this->value; } /** * Factory constructor * * @example $version = Version::V101(); * @param string $aValue the called method as a version value * @param array $arguments unused argments passed to the method * @return self */ public static function __callStatic($aValue, array $arguments = []) { $aValue = trim(preg_replace("#v(\d)(95|\d)(\d)?#i", '$1.$2.$3', $aValue), "."); if (!isset(static::$instances[$aValue])) { static::$instances[$aValue] = new static($aValue); } return static::$instances[$aValue]; } /** * Convert a string into a Version instance * * @param string $aValue a version value * @return self */ public static function fromString($aValue) { $aValue = str_replace(".", "", $aValue); return static::{"V$aValue"}(); } /** * List all supported versions * * @array string[] */ public static function supported() { return array_keys(array_filter(static::$supported, function($supported) { return $supported === true; })); } /** * Retrieve the most recent version * * @return string */ public static function latest() { return array_flip(static::$supported)[0]; } } var_dump(Version::fromString("1.0.0"), Version::supported(), Version::latest());
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZMhER on line 157 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZMhER on line 157 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZMhER on line 157 Warning: Undefined array key 0 in /in/ZMhER on line 157 object(TinCan\Version)#1 (1) { ["value":"TinCan\Version":private]=> string(5) "1.0.0" } array(2) { [0]=> string(5) "1.0.1" [1]=> string(5) "1.0.0" } NULL
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZMhER on line 157 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZMhER on line 157 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZMhER on line 157 Notice: Undefined offset: 0 in /in/ZMhER on line 157 object(TinCan\Version)#1 (1) { ["value":"TinCan\Version":private]=> string(5) "1.0.0" } array(2) { [0]=> string(5) "1.0.1" [1]=> string(5) "1.0.0" } NULL
Output for 7.3.32 - 7.3.33
Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZMhER on line 157 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZMhER on line 157 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZMhER on line 157 object(TinCan\Version)#1 (1) { ["value":"TinCan\Version":private]=> string(5) "1.0.0" } array(2) { [0]=> string(5) "1.0.1" [1]=> string(5) "1.0.0" } NULL
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/ZMhER on line 41
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/ZMhER on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/ZMhER on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/ZMhER on line 18
Process exited with code 255.

preferences:
197.64 ms | 401 KiB | 323 Q