@@ -17,77 +17,77 @@ |
||
| 17 | 17 | class EmailType extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - const PATTERN = '\A[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * List of formats recognized by this class |
|
| 24 | - * @var string[] |
|
| 25 | - */ |
|
| 26 | - private static $formats = array('email'); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Construct and set up the regular expression for this type |
|
| 30 | - * |
|
| 31 | - * @param AbstractObject $parent |
|
| 32 | - * @param string $definition |
|
| 33 | - */ |
|
| 34 | - public function __construct(AbstractObject $parent, $definition) |
|
| 35 | - { |
|
| 36 | - $this->pattern = self::PATTERN; |
|
| 37 | - |
|
| 38 | - parent::__construct($parent, $definition); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public static function getFormats() |
|
| 42 | - { |
|
| 43 | - return self::$formats; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function setFormats(array $formats) |
|
| 47 | - { |
|
| 48 | - self::$formats = $formats; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Parse a type definition string, assuming it belongs to this type |
|
| 53 | - * |
|
| 54 | - * @param string $definition |
|
| 55 | - * @throws Exception |
|
| 56 | - */ |
|
| 57 | - protected function parseDefinition($definition) |
|
| 58 | - { |
|
| 59 | - $definition = self::trim($definition); |
|
| 60 | - |
|
| 61 | - $match = []; |
|
| 62 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | - throw new Exception("Unparseable email definition: '{$definition}'"); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | - throw new Exception("Not an email: '{$definition}'"); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Check (and optionally reformat) a default value |
|
| 75 | - * |
|
| 76 | - * @param string $value |
|
| 77 | - * @return string |
|
| 78 | - * @throws Exception |
|
| 79 | - */ |
|
| 80 | - protected function validateDefault($value) |
|
| 81 | - { |
|
| 82 | - if (empty($value)) { |
|
| 83 | - throw new Exception("Empty email default"); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { |
|
| 87 | - throw new Exception("Invalid email default value: '{$value}'"); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $value; |
|
| 91 | - } |
|
| 20 | + const PATTERN = '\A[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * List of formats recognized by this class |
|
| 24 | + * @var string[] |
|
| 25 | + */ |
|
| 26 | + private static $formats = array('email'); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Construct and set up the regular expression for this type |
|
| 30 | + * |
|
| 31 | + * @param AbstractObject $parent |
|
| 32 | + * @param string $definition |
|
| 33 | + */ |
|
| 34 | + public function __construct(AbstractObject $parent, $definition) |
|
| 35 | + { |
|
| 36 | + $this->pattern = self::PATTERN; |
|
| 37 | + |
|
| 38 | + parent::__construct($parent, $definition); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public static function getFormats() |
|
| 42 | + { |
|
| 43 | + return self::$formats; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function setFormats(array $formats) |
|
| 47 | + { |
|
| 48 | + self::$formats = $formats; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Parse a type definition string, assuming it belongs to this type |
|
| 53 | + * |
|
| 54 | + * @param string $definition |
|
| 55 | + * @throws Exception |
|
| 56 | + */ |
|
| 57 | + protected function parseDefinition($definition) |
|
| 58 | + { |
|
| 59 | + $definition = self::trim($definition); |
|
| 60 | + |
|
| 61 | + $match = []; |
|
| 62 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | + throw new Exception("Unparseable email definition: '{$definition}'"); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | + throw new Exception("Not an email: '{$definition}'"); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Check (and optionally reformat) a default value |
|
| 75 | + * |
|
| 76 | + * @param string $value |
|
| 77 | + * @return string |
|
| 78 | + * @throws Exception |
|
| 79 | + */ |
|
| 80 | + protected function validateDefault($value) |
|
| 81 | + { |
|
| 82 | + if (empty($value)) { |
|
| 83 | + throw new Exception("Empty email default"); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { |
|
| 87 | + throw new Exception("Invalid email default value: '{$value}'"); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $value; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -17,77 +17,77 @@ |
||
| 17 | 17 | class MacType extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * List of formats recognized by this class |
|
| 24 | - * @var string[] |
|
| 25 | - */ |
|
| 26 | - private static $formats = array('mac'); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Construct and set up the regular expression for this type |
|
| 30 | - * |
|
| 31 | - * @param AbstractObject $parent |
|
| 32 | - * @param string $definition |
|
| 33 | - */ |
|
| 34 | - public function __construct(AbstractObject $parent, $definition) |
|
| 35 | - { |
|
| 36 | - $this->pattern = self::PATTERN; |
|
| 37 | - |
|
| 38 | - parent::__construct($parent, $definition); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public static function getFormats() |
|
| 42 | - { |
|
| 43 | - return self::$formats; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function setFormats(array $formats) |
|
| 47 | - { |
|
| 48 | - self::$formats = $formats; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Parse a type definition string, assuming it belongs to this type |
|
| 53 | - * |
|
| 54 | - * @param string $definition |
|
| 55 | - * @throws Exception |
|
| 56 | - */ |
|
| 57 | - protected function parseDefinition($definition) |
|
| 58 | - { |
|
| 59 | - $definition = self::trim($definition); |
|
| 60 | - |
|
| 61 | - $match = []; |
|
| 62 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | - throw new Exception("Unparseable MAC definition: '{$definition}'"); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | - throw new Exception("Not a MAC: '{$definition}'"); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Check (and optionally reformat) a default value |
|
| 75 | - * |
|
| 76 | - * @param string $value |
|
| 77 | - * @return string |
|
| 78 | - * @throws Exception |
|
| 79 | - */ |
|
| 80 | - protected function validateDefault($value) |
|
| 81 | - { |
|
| 82 | - if (empty($value)) { |
|
| 83 | - throw new Exception("Empty MAC default"); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) { |
|
| 87 | - throw new Exception("Invalid MAC default value: '{$value}'"); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $value; |
|
| 91 | - } |
|
| 20 | + const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * List of formats recognized by this class |
|
| 24 | + * @var string[] |
|
| 25 | + */ |
|
| 26 | + private static $formats = array('mac'); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Construct and set up the regular expression for this type |
|
| 30 | + * |
|
| 31 | + * @param AbstractObject $parent |
|
| 32 | + * @param string $definition |
|
| 33 | + */ |
|
| 34 | + public function __construct(AbstractObject $parent, $definition) |
|
| 35 | + { |
|
| 36 | + $this->pattern = self::PATTERN; |
|
| 37 | + |
|
| 38 | + parent::__construct($parent, $definition); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public static function getFormats() |
|
| 42 | + { |
|
| 43 | + return self::$formats; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function setFormats(array $formats) |
|
| 47 | + { |
|
| 48 | + self::$formats = $formats; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Parse a type definition string, assuming it belongs to this type |
|
| 53 | + * |
|
| 54 | + * @param string $definition |
|
| 55 | + * @throws Exception |
|
| 56 | + */ |
|
| 57 | + protected function parseDefinition($definition) |
|
| 58 | + { |
|
| 59 | + $definition = self::trim($definition); |
|
| 60 | + |
|
| 61 | + $match = []; |
|
| 62 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | + throw new Exception("Unparseable MAC definition: '{$definition}'"); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | + throw new Exception("Not a MAC: '{$definition}'"); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Check (and optionally reformat) a default value |
|
| 75 | + * |
|
| 76 | + * @param string $value |
|
| 77 | + * @return string |
|
| 78 | + * @throws Exception |
|
| 79 | + */ |
|
| 80 | + protected function validateDefault($value) |
|
| 81 | + { |
|
| 82 | + if (empty($value)) { |
|
| 83 | + throw new Exception("Empty MAC default"); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) { |
|
| 87 | + throw new Exception("Invalid MAC default value: '{$value}'"); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $value; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -17,77 +17,77 @@ |
||
| 17 | 17 | class Ipv6Type extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - const PATTERN = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * List of formats recognized by this class |
|
| 24 | - * @var string[] |
|
| 25 | - */ |
|
| 26 | - private static $formats = array('ipv6'); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Construct and set up the regular expression for this type |
|
| 30 | - * |
|
| 31 | - * @param AbstractObject $parent |
|
| 32 | - * @param string $definition |
|
| 33 | - */ |
|
| 34 | - public function __construct(AbstractObject $parent, $definition) |
|
| 35 | - { |
|
| 36 | - $this->pattern = self::PATTERN; |
|
| 37 | - |
|
| 38 | - parent::__construct($parent, $definition); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public static function getFormats() |
|
| 42 | - { |
|
| 43 | - return self::$formats; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function setFormats(array $formats) |
|
| 47 | - { |
|
| 48 | - self::$formats = $formats; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Parse a type definition string, assuming it belongs to this type |
|
| 53 | - * |
|
| 54 | - * @param string $definition |
|
| 55 | - * @throws Exception |
|
| 56 | - */ |
|
| 57 | - protected function parseDefinition($definition) |
|
| 58 | - { |
|
| 59 | - $definition = self::trim($definition); |
|
| 60 | - |
|
| 61 | - $match = []; |
|
| 62 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | - throw new Exception("Unparseable IPv6 definition: '{$definition}'"); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | - throw new Exception("Not an IPv6: '{$definition}'"); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Check (and optionally reformat) a default value |
|
| 75 | - * |
|
| 76 | - * @param string $value |
|
| 77 | - * @return string |
|
| 78 | - * @throws Exception |
|
| 79 | - */ |
|
| 80 | - protected function validateDefault($value) |
|
| 81 | - { |
|
| 82 | - if (empty($value)) { |
|
| 83 | - throw new Exception("Empty IPv6 default"); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
|
| 87 | - throw new Exception("Invalid IPv6 default value: '{$value}'"); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $value; |
|
| 91 | - } |
|
| 20 | + const PATTERN = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * List of formats recognized by this class |
|
| 24 | + * @var string[] |
|
| 25 | + */ |
|
| 26 | + private static $formats = array('ipv6'); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Construct and set up the regular expression for this type |
|
| 30 | + * |
|
| 31 | + * @param AbstractObject $parent |
|
| 32 | + * @param string $definition |
|
| 33 | + */ |
|
| 34 | + public function __construct(AbstractObject $parent, $definition) |
|
| 35 | + { |
|
| 36 | + $this->pattern = self::PATTERN; |
|
| 37 | + |
|
| 38 | + parent::__construct($parent, $definition); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public static function getFormats() |
|
| 42 | + { |
|
| 43 | + return self::$formats; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function setFormats(array $formats) |
|
| 47 | + { |
|
| 48 | + self::$formats = $formats; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Parse a type definition string, assuming it belongs to this type |
|
| 53 | + * |
|
| 54 | + * @param string $definition |
|
| 55 | + * @throws Exception |
|
| 56 | + */ |
|
| 57 | + protected function parseDefinition($definition) |
|
| 58 | + { |
|
| 59 | + $definition = self::trim($definition); |
|
| 60 | + |
|
| 61 | + $match = []; |
|
| 62 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | + throw new Exception("Unparseable IPv6 definition: '{$definition}'"); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | + throw new Exception("Not an IPv6: '{$definition}'"); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Check (and optionally reformat) a default value |
|
| 75 | + * |
|
| 76 | + * @param string $value |
|
| 77 | + * @return string |
|
| 78 | + * @throws Exception |
|
| 79 | + */ |
|
| 80 | + protected function validateDefault($value) |
|
| 81 | + { |
|
| 82 | + if (empty($value)) { |
|
| 83 | + throw new Exception("Empty IPv6 default"); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
|
| 87 | + throw new Exception("Invalid IPv6 default value: '{$value}'"); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $value; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -17,77 +17,77 @@ |
||
| 17 | 17 | class Ipv4Type extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - const PATTERN = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * List of formats recognized by this class |
|
| 24 | - * @var string[] |
|
| 25 | - */ |
|
| 26 | - private static $formats = array('ipv4'); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Construct and set up the regular expression for this type |
|
| 30 | - * |
|
| 31 | - * @param AbstractObject $parent |
|
| 32 | - * @param string $definition |
|
| 33 | - */ |
|
| 34 | - public function __construct(AbstractObject $parent, $definition) |
|
| 35 | - { |
|
| 36 | - $this->pattern = self::PATTERN; |
|
| 37 | - |
|
| 38 | - parent::__construct($parent, $definition); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public static function getFormats() |
|
| 42 | - { |
|
| 43 | - return self::$formats; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function setFormats(array $formats) |
|
| 47 | - { |
|
| 48 | - self::$formats = $formats; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Parse a type definition string, assuming it belongs to this type |
|
| 53 | - * |
|
| 54 | - * @param string $definition |
|
| 55 | - * @throws Exception |
|
| 56 | - */ |
|
| 57 | - protected function parseDefinition($definition) |
|
| 58 | - { |
|
| 59 | - $definition = self::trim($definition); |
|
| 60 | - |
|
| 61 | - $match = []; |
|
| 62 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | - throw new Exception("Unparseable IPv4 definition: '{$definition}'"); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | - throw new Exception("Not an IPv4: '{$definition}'"); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Check (and optionally reformat) a default value |
|
| 75 | - * |
|
| 76 | - * @param string $value |
|
| 77 | - * @return string |
|
| 78 | - * @throws Exception |
|
| 79 | - */ |
|
| 80 | - protected function validateDefault($value) |
|
| 81 | - { |
|
| 82 | - if (empty($value)) { |
|
| 83 | - throw new Exception("Empty IPv4 default"); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
|
| 87 | - throw new Exception("Invalid IPv4 default value: '{$value}'"); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $value; |
|
| 91 | - } |
|
| 20 | + const PATTERN = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * List of formats recognized by this class |
|
| 24 | + * @var string[] |
|
| 25 | + */ |
|
| 26 | + private static $formats = array('ipv4'); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Construct and set up the regular expression for this type |
|
| 30 | + * |
|
| 31 | + * @param AbstractObject $parent |
|
| 32 | + * @param string $definition |
|
| 33 | + */ |
|
| 34 | + public function __construct(AbstractObject $parent, $definition) |
|
| 35 | + { |
|
| 36 | + $this->pattern = self::PATTERN; |
|
| 37 | + |
|
| 38 | + parent::__construct($parent, $definition); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public static function getFormats() |
|
| 42 | + { |
|
| 43 | + return self::$formats; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function setFormats(array $formats) |
|
| 47 | + { |
|
| 48 | + self::$formats = $formats; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Parse a type definition string, assuming it belongs to this type |
|
| 53 | + * |
|
| 54 | + * @param string $definition |
|
| 55 | + * @throws Exception |
|
| 56 | + */ |
|
| 57 | + protected function parseDefinition($definition) |
|
| 58 | + { |
|
| 59 | + $definition = self::trim($definition); |
|
| 60 | + |
|
| 61 | + $match = []; |
|
| 62 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | + throw new Exception("Unparseable IPv4 definition: '{$definition}'"); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | + throw new Exception("Not an IPv4: '{$definition}'"); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Check (and optionally reformat) a default value |
|
| 75 | + * |
|
| 76 | + * @param string $value |
|
| 77 | + * @return string |
|
| 78 | + * @throws Exception |
|
| 79 | + */ |
|
| 80 | + protected function validateDefault($value) |
|
| 81 | + { |
|
| 82 | + if (empty($value)) { |
|
| 83 | + throw new Exception("Empty IPv4 default"); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
|
| 87 | + throw new Exception("Invalid IPv4 default value: '{$value}'"); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $value; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -15,61 +15,61 @@ |
||
| 15 | 15 | class BooleanType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | - const REGEX_DEFAULT = '(?:=(true|false|1|0))?'; |
|
| 18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | + const REGEX_DEFAULT = '(?:=(true|false|1|0))?'; |
|
| 20 | 20 | |
| 21 | - private $default = null; |
|
| 21 | + private $default = null; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param string $command The comment command |
|
| 25 | - * @param string $data Any data added after the command |
|
| 26 | - * @return AbstractType|boolean |
|
| 27 | - * @throws Exception |
|
| 28 | - * @throws Exception |
|
| 29 | - */ |
|
| 30 | - public function handleCommand($command, $data = null) |
|
| 31 | - { |
|
| 32 | - if (strtolower($command) === 'default') { |
|
| 33 | - if (!in_array($data, array('0', '1', 'true', 'false'))) { |
|
| 34 | - throw new Exception("Invalid boolean default: '{$data}'"); |
|
| 35 | - } |
|
| 36 | - $this->default = ($data == '1') || (strtolower($data) === 'true'); |
|
| 37 | - return $this; |
|
| 38 | - } |
|
| 23 | + /** |
|
| 24 | + * @param string $command The comment command |
|
| 25 | + * @param string $data Any data added after the command |
|
| 26 | + * @return AbstractType|boolean |
|
| 27 | + * @throws Exception |
|
| 28 | + * @throws Exception |
|
| 29 | + */ |
|
| 30 | + public function handleCommand($command, $data = null) |
|
| 31 | + { |
|
| 32 | + if (strtolower($command) === 'default') { |
|
| 33 | + if (!in_array($data, array('0', '1', 'true', 'false'))) { |
|
| 34 | + throw new Exception("Invalid boolean default: '{$data}'"); |
|
| 35 | + } |
|
| 36 | + $this->default = ($data == '1') || (strtolower($data) === 'true'); |
|
| 37 | + return $this; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return parent::handleCommand($command, $data); |
|
| 41 | - } |
|
| 40 | + return parent::handleCommand($command, $data); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function toArray(): array |
|
| 44 | - { |
|
| 45 | - return self::arrayFilterNull(array_merge(array( |
|
| 46 | - 'type' => 'boolean', |
|
| 47 | - 'default' => $this->default, |
|
| 48 | - ), parent::toArray())); |
|
| 49 | - } |
|
| 43 | + public function toArray(): array |
|
| 44 | + { |
|
| 45 | + return self::arrayFilterNull(array_merge(array( |
|
| 46 | + 'type' => 'boolean', |
|
| 47 | + 'default' => $this->default, |
|
| 48 | + ), parent::toArray())); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public function __toString() |
|
| 52 | - { |
|
| 53 | - return __CLASS__; |
|
| 54 | - } |
|
| 51 | + public function __toString() |
|
| 52 | + { |
|
| 53 | + return __CLASS__; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @throws Exception |
|
| 58 | - */ |
|
| 59 | - protected function parseDefinition($definition) |
|
| 60 | - { |
|
| 61 | - $match = []; |
|
| 62 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | - throw new Exception("Unparseable boolean definition: '{$definition}'"); |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * @throws Exception |
|
| 58 | + */ |
|
| 59 | + protected function parseDefinition($definition) |
|
| 60 | + { |
|
| 61 | + $match = []; |
|
| 62 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | + throw new Exception("Unparseable boolean definition: '{$definition}'"); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - if (strtolower($match[1]) !== 'boolean') { |
|
| 67 | - throw new Exception("Not a boolean: '{$definition}'"); |
|
| 68 | - } |
|
| 66 | + if (strtolower($match[1]) !== 'boolean') { |
|
| 67 | + throw new Exception("Not a boolean: '{$definition}'"); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - if (isset($match[2])) { |
|
| 71 | - $this->default = ($match[2] === '1') || (strtolower($match[2]) === 'true'); |
|
| 72 | - } |
|
| 73 | - } |
|
| 70 | + if (isset($match[2])) { |
|
| 71 | + $this->default = ($match[2] === '1') || (strtolower($match[2]) === 'true'); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | } |
@@ -17,98 +17,98 @@ |
||
| 17 | 17 | class DateType extends AbstractType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 21 | - const REGEX_DEFAULT = '(?:=(\S+))?'; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Map of recognized format names to Swagger formats |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private static $formats = array( |
|
| 28 | - 'date' => 'date', |
|
| 29 | - 'date-time' => 'date-time', |
|
| 30 | - 'datetime' => 'date-time', |
|
| 31 | - ); |
|
| 32 | - private static $datetime_formats = array( |
|
| 33 | - 'date' => 'Y-m-d', |
|
| 34 | - 'date-time' => DateTimeInterface::RFC3339, |
|
| 35 | - ); |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var String |
|
| 39 | - */ |
|
| 40 | - private $format; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var DateTime |
|
| 44 | - */ |
|
| 45 | - private $default = null; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @param string $command The comment command |
|
| 49 | - * @param string $data Any data added after the command |
|
| 50 | - * @return AbstractType|boolean |
|
| 51 | - * @throws Exception |
|
| 52 | - * @throws Exception |
|
| 53 | - */ |
|
| 54 | - public function handleCommand($command, $data = null) |
|
| 55 | - { |
|
| 56 | - if (strtolower($command) === 'default') { |
|
| 57 | - $this->default = $this->validateDefault($data); |
|
| 58 | - return $this; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return parent::handleCommand($command, $data); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @throws Exception |
|
| 66 | - */ |
|
| 67 | - private function validateDefault($value) |
|
| 68 | - { |
|
| 69 | - if (empty($value)) { |
|
| 70 | - throw new Exception("Empty date default"); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if (($DateTime = date_create($value)) !== false) { |
|
| 74 | - return $DateTime; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - throw new Exception("Invalid '{$this->format}' default: '{$value}'"); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function toArray(): array |
|
| 81 | - { |
|
| 82 | - return self::arrayFilterNull(array_merge(array( |
|
| 83 | - 'type' => 'string', |
|
| 84 | - 'format' => $this->format, |
|
| 85 | - 'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null, |
|
| 86 | - ), parent::toArray())); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function __toString() |
|
| 90 | - { |
|
| 91 | - return __CLASS__; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @throws Exception |
|
| 96 | - */ |
|
| 97 | - protected function parseDefinition($definition) |
|
| 98 | - { |
|
| 99 | - $match = []; |
|
| 100 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 101 | - throw new Exception("Unparseable date definition: '{$definition}'"); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $type = strtolower($match[1]); |
|
| 105 | - |
|
| 106 | - if (!isset(self::$formats[$type])) { |
|
| 107 | - throw new Exception("Not a date: '{$definition}'"); |
|
| 108 | - } |
|
| 109 | - $this->format = self::$formats[$type]; |
|
| 110 | - |
|
| 111 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 112 | - } |
|
| 20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 21 | + const REGEX_DEFAULT = '(?:=(\S+))?'; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Map of recognized format names to Swagger formats |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private static $formats = array( |
|
| 28 | + 'date' => 'date', |
|
| 29 | + 'date-time' => 'date-time', |
|
| 30 | + 'datetime' => 'date-time', |
|
| 31 | + ); |
|
| 32 | + private static $datetime_formats = array( |
|
| 33 | + 'date' => 'Y-m-d', |
|
| 34 | + 'date-time' => DateTimeInterface::RFC3339, |
|
| 35 | + ); |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var String |
|
| 39 | + */ |
|
| 40 | + private $format; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var DateTime |
|
| 44 | + */ |
|
| 45 | + private $default = null; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @param string $command The comment command |
|
| 49 | + * @param string $data Any data added after the command |
|
| 50 | + * @return AbstractType|boolean |
|
| 51 | + * @throws Exception |
|
| 52 | + * @throws Exception |
|
| 53 | + */ |
|
| 54 | + public function handleCommand($command, $data = null) |
|
| 55 | + { |
|
| 56 | + if (strtolower($command) === 'default') { |
|
| 57 | + $this->default = $this->validateDefault($data); |
|
| 58 | + return $this; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return parent::handleCommand($command, $data); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @throws Exception |
|
| 66 | + */ |
|
| 67 | + private function validateDefault($value) |
|
| 68 | + { |
|
| 69 | + if (empty($value)) { |
|
| 70 | + throw new Exception("Empty date default"); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if (($DateTime = date_create($value)) !== false) { |
|
| 74 | + return $DateTime; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + throw new Exception("Invalid '{$this->format}' default: '{$value}'"); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function toArray(): array |
|
| 81 | + { |
|
| 82 | + return self::arrayFilterNull(array_merge(array( |
|
| 83 | + 'type' => 'string', |
|
| 84 | + 'format' => $this->format, |
|
| 85 | + 'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null, |
|
| 86 | + ), parent::toArray())); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function __toString() |
|
| 90 | + { |
|
| 91 | + return __CLASS__; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @throws Exception |
|
| 96 | + */ |
|
| 97 | + protected function parseDefinition($definition) |
|
| 98 | + { |
|
| 99 | + $match = []; |
|
| 100 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 101 | + throw new Exception("Unparseable date definition: '{$definition}'"); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $type = strtolower($match[1]); |
|
| 105 | + |
|
| 106 | + if (!isset(self::$formats[$type])) { |
|
| 107 | + throw new Exception("Not a date: '{$definition}'"); |
|
| 108 | + } |
|
| 109 | + $this->format = self::$formats[$type]; |
|
| 110 | + |
|
| 111 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | 114 | } |
@@ -15,166 +15,166 @@ |
||
| 15 | 15 | class IntegerType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | - const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?'; |
|
| 20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 21 | - const REGEX_DEFAULT = '(?:=(-?\d+))?'; |
|
| 22 | - |
|
| 23 | - private static $formats = array( |
|
| 24 | - 'int32' => 'int32', |
|
| 25 | - 'integer' => 'int32', |
|
| 26 | - 'int' => 'int32', |
|
| 27 | - 'int64' => 'int64', |
|
| 28 | - 'long' => 'int64', |
|
| 29 | - ); |
|
| 30 | - private $format; |
|
| 31 | - private $default = null; |
|
| 32 | - private $maximum = null; |
|
| 33 | - private $exclusiveMaximum = null; |
|
| 34 | - private $minimum = null; |
|
| 35 | - private $exclusiveMinimum = null; |
|
| 36 | - private $enum = []; |
|
| 37 | - private $multipleOf = null; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param string $command The comment command |
|
| 41 | - * @param string $data Any data added after the command |
|
| 42 | - * @return AbstractType|boolean |
|
| 43 | - * @throws Exception |
|
| 44 | - * @throws Exception |
|
| 45 | - * @throws Exception |
|
| 46 | - */ |
|
| 47 | - public function handleCommand($command, $data = null) |
|
| 48 | - { |
|
| 49 | - switch (strtolower($command)) { |
|
| 50 | - case 'default': |
|
| 51 | - $this->default = $this->validateDefault($data); |
|
| 52 | - return $this; |
|
| 53 | - |
|
| 54 | - case 'enum': |
|
| 55 | - $words = self::wordSplit($data); |
|
| 56 | - foreach ($words as &$word) { |
|
| 57 | - $word = $this->validateDefault($word); |
|
| 58 | - } |
|
| 59 | - unset($word); |
|
| 60 | - $this->enum = array_merge($this->enum, $words); |
|
| 61 | - return $this; |
|
| 62 | - |
|
| 63 | - case 'step': |
|
| 64 | - if (($step = (int)$data) > 0) { |
|
| 65 | - $this->multipleOf = $step; |
|
| 66 | - } |
|
| 67 | - return $this; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return parent::handleCommand($command, $data); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @throws Exception |
|
| 75 | - */ |
|
| 76 | - private function validateDefault($value) |
|
| 77 | - { |
|
| 78 | - if (preg_match('~^-?\d+$~', $value) !== 1) { |
|
| 79 | - throw new Exception("Invalid integer default: '{$value}'"); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if ($this->maximum) { |
|
| 83 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
| 84 | - throw new Exception("Default integer beyond maximum: '{$value}'"); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - if ($this->minimum) { |
|
| 88 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
| 89 | - throw new Exception("Default integer beyond minimum: '{$value}'"); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return (int)$value; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function toArray(): array |
|
| 97 | - { |
|
| 98 | - return self::arrayFilterNull(array_merge(array( |
|
| 99 | - 'type' => 'integer', |
|
| 100 | - 'format' => $this->format, |
|
| 101 | - 'default' => $this->default, |
|
| 102 | - 'minimum' => $this->minimum, |
|
| 103 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
| 104 | - 'maximum' => $this->maximum, |
|
| 105 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
| 106 | - 'enum' => $this->enum, |
|
| 107 | - 'multipleOf' => $this->multipleOf, |
|
| 108 | - ), parent::toArray())); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - public function __toString() |
|
| 112 | - { |
|
| 113 | - return __CLASS__; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @throws Exception |
|
| 118 | - */ |
|
| 119 | - protected function parseDefinition($definition) |
|
| 120 | - { |
|
| 121 | - $definition = self::trim($definition); |
|
| 122 | - |
|
| 123 | - $match = []; |
|
| 124 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 125 | - throw new Exception("Unparseable integer definition: '{$definition}'"); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $this->parseFormat($definition, $match); |
|
| 129 | - $this->parseRange($definition, $match); |
|
| 130 | - $this->parseDefault($definition, $match); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param string $definition |
|
| 135 | - * @param string[] $match |
|
| 136 | - * @throws Exception |
|
| 137 | - */ |
|
| 138 | - private function parseFormat($definition, $match) |
|
| 139 | - { |
|
| 140 | - $type = strtolower($match[1]); |
|
| 141 | - if (!isset(self::$formats[$type])) { |
|
| 142 | - throw new Exception("Not an integer: '{$definition}'"); |
|
| 143 | - } |
|
| 144 | - $this->format = self::$formats[$type]; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param string $definition |
|
| 149 | - * @param string[] $match |
|
| 150 | - * @throws Exception |
|
| 151 | - */ |
|
| 152 | - private function parseRange($definition, $match) |
|
| 153 | - { |
|
| 154 | - if (!empty($match[2])) { |
|
| 155 | - if ($match[3] === '' && $match[4] === '') { |
|
| 156 | - throw new Exception("Empty integer range: '{$definition}'"); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
| 160 | - $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
| 161 | - $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
| 162 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
| 163 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
| 164 | - self::swap($this->minimum, $this->maximum); |
|
| 165 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $definition |
|
| 172 | - * @param string[] $match |
|
| 173 | - * @throws Exception |
|
| 174 | - */ |
|
| 175 | - private function parseDefault($definition, $match) |
|
| 176 | - { |
|
| 177 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
| 178 | - } |
|
| 18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | + const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?'; |
|
| 20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 21 | + const REGEX_DEFAULT = '(?:=(-?\d+))?'; |
|
| 22 | + |
|
| 23 | + private static $formats = array( |
|
| 24 | + 'int32' => 'int32', |
|
| 25 | + 'integer' => 'int32', |
|
| 26 | + 'int' => 'int32', |
|
| 27 | + 'int64' => 'int64', |
|
| 28 | + 'long' => 'int64', |
|
| 29 | + ); |
|
| 30 | + private $format; |
|
| 31 | + private $default = null; |
|
| 32 | + private $maximum = null; |
|
| 33 | + private $exclusiveMaximum = null; |
|
| 34 | + private $minimum = null; |
|
| 35 | + private $exclusiveMinimum = null; |
|
| 36 | + private $enum = []; |
|
| 37 | + private $multipleOf = null; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param string $command The comment command |
|
| 41 | + * @param string $data Any data added after the command |
|
| 42 | + * @return AbstractType|boolean |
|
| 43 | + * @throws Exception |
|
| 44 | + * @throws Exception |
|
| 45 | + * @throws Exception |
|
| 46 | + */ |
|
| 47 | + public function handleCommand($command, $data = null) |
|
| 48 | + { |
|
| 49 | + switch (strtolower($command)) { |
|
| 50 | + case 'default': |
|
| 51 | + $this->default = $this->validateDefault($data); |
|
| 52 | + return $this; |
|
| 53 | + |
|
| 54 | + case 'enum': |
|
| 55 | + $words = self::wordSplit($data); |
|
| 56 | + foreach ($words as &$word) { |
|
| 57 | + $word = $this->validateDefault($word); |
|
| 58 | + } |
|
| 59 | + unset($word); |
|
| 60 | + $this->enum = array_merge($this->enum, $words); |
|
| 61 | + return $this; |
|
| 62 | + |
|
| 63 | + case 'step': |
|
| 64 | + if (($step = (int)$data) > 0) { |
|
| 65 | + $this->multipleOf = $step; |
|
| 66 | + } |
|
| 67 | + return $this; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return parent::handleCommand($command, $data); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @throws Exception |
|
| 75 | + */ |
|
| 76 | + private function validateDefault($value) |
|
| 77 | + { |
|
| 78 | + if (preg_match('~^-?\d+$~', $value) !== 1) { |
|
| 79 | + throw new Exception("Invalid integer default: '{$value}'"); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if ($this->maximum) { |
|
| 83 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
| 84 | + throw new Exception("Default integer beyond maximum: '{$value}'"); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + if ($this->minimum) { |
|
| 88 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
| 89 | + throw new Exception("Default integer beyond minimum: '{$value}'"); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return (int)$value; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function toArray(): array |
|
| 97 | + { |
|
| 98 | + return self::arrayFilterNull(array_merge(array( |
|
| 99 | + 'type' => 'integer', |
|
| 100 | + 'format' => $this->format, |
|
| 101 | + 'default' => $this->default, |
|
| 102 | + 'minimum' => $this->minimum, |
|
| 103 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
| 104 | + 'maximum' => $this->maximum, |
|
| 105 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
| 106 | + 'enum' => $this->enum, |
|
| 107 | + 'multipleOf' => $this->multipleOf, |
|
| 108 | + ), parent::toArray())); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + public function __toString() |
|
| 112 | + { |
|
| 113 | + return __CLASS__; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @throws Exception |
|
| 118 | + */ |
|
| 119 | + protected function parseDefinition($definition) |
|
| 120 | + { |
|
| 121 | + $definition = self::trim($definition); |
|
| 122 | + |
|
| 123 | + $match = []; |
|
| 124 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 125 | + throw new Exception("Unparseable integer definition: '{$definition}'"); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $this->parseFormat($definition, $match); |
|
| 129 | + $this->parseRange($definition, $match); |
|
| 130 | + $this->parseDefault($definition, $match); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param string $definition |
|
| 135 | + * @param string[] $match |
|
| 136 | + * @throws Exception |
|
| 137 | + */ |
|
| 138 | + private function parseFormat($definition, $match) |
|
| 139 | + { |
|
| 140 | + $type = strtolower($match[1]); |
|
| 141 | + if (!isset(self::$formats[$type])) { |
|
| 142 | + throw new Exception("Not an integer: '{$definition}'"); |
|
| 143 | + } |
|
| 144 | + $this->format = self::$formats[$type]; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param string $definition |
|
| 149 | + * @param string[] $match |
|
| 150 | + * @throws Exception |
|
| 151 | + */ |
|
| 152 | + private function parseRange($definition, $match) |
|
| 153 | + { |
|
| 154 | + if (!empty($match[2])) { |
|
| 155 | + if ($match[3] === '' && $match[4] === '') { |
|
| 156 | + throw new Exception("Empty integer range: '{$definition}'"); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
| 160 | + $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
| 161 | + $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
| 162 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
| 163 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
| 164 | + self::swap($this->minimum, $this->maximum); |
|
| 165 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $definition |
|
| 172 | + * @param string[] $match |
|
| 173 | + * @throws Exception |
|
| 174 | + */ |
|
| 175 | + private function parseDefault($definition, $match) |
|
| 176 | + { |
|
| 177 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | 180 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | return $this; |
| 62 | 62 | |
| 63 | 63 | case 'step': |
| 64 | - if (($step = (int)$data) > 0) { |
|
| 64 | + if (($step = (int) $data) > 0) { |
|
| 65 | 65 | $this->multipleOf = $step; |
| 66 | 66 | } |
| 67 | 67 | return $this; |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - return (int)$value; |
|
| 93 | + return (int) $value; |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | public function toArray(): array |
@@ -157,8 +157,8 @@ discard block |
||
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | $this->exclusiveMinimum = $match[2] == '<'; |
| 160 | - $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
| 161 | - $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
| 160 | + $this->minimum = $match[3] === '' ? null : (int) $match[3]; |
|
| 161 | + $this->maximum = $match[4] === '' ? null : (int) $match[4]; |
|
| 162 | 162 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
| 163 | 163 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
| 164 | 164 | self::swap($this->minimum, $this->maximum); |
@@ -16,61 +16,61 @@ |
||
| 16 | 16 | class AbstractRegexType extends StringType |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - public const REGEX_DEFAULT_START = '(?:=('; |
|
| 20 | - public const REGEX_DEFAULT_END = '))?'; |
|
| 19 | + public const REGEX_DEFAULT_START = '(?:=('; |
|
| 20 | + public const REGEX_DEFAULT_END = '))?'; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The raw regular expression to use. |
|
| 24 | - * Exclude start (`^`) and end (`$`) anchors. |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - private $regex; |
|
| 22 | + /** |
|
| 23 | + * The raw regular expression to use. |
|
| 24 | + * Exclude start (`^`) and end (`$`) anchors. |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + private $regex; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Construct and set up the regular expression for this type |
|
| 31 | - * |
|
| 32 | - * @param AbstractObject $parent |
|
| 33 | - * @param string $definition |
|
| 34 | - * @param string $format Name of the string format |
|
| 35 | - * @param string $regex Regular expression to use as the format and for default validation |
|
| 36 | - */ |
|
| 37 | - public function __construct(AbstractObject $parent, $definition, $format, $regex) |
|
| 38 | - { |
|
| 39 | - $this->format = $format; |
|
| 40 | - $this->regex = $regex; |
|
| 29 | + /** |
|
| 30 | + * Construct and set up the regular expression for this type |
|
| 31 | + * |
|
| 32 | + * @param AbstractObject $parent |
|
| 33 | + * @param string $definition |
|
| 34 | + * @param string $format Name of the string format |
|
| 35 | + * @param string $regex Regular expression to use as the format and for default validation |
|
| 36 | + */ |
|
| 37 | + public function __construct(AbstractObject $parent, $definition, $format, $regex) |
|
| 38 | + { |
|
| 39 | + $this->format = $format; |
|
| 40 | + $this->regex = $regex; |
|
| 41 | 41 | |
| 42 | - parent::__construct($parent, $definition); |
|
| 43 | - } |
|
| 42 | + parent::__construct($parent, $definition); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @throws Exception |
|
| 47 | - */ |
|
| 48 | - protected function parseDefinition($definition) |
|
| 49 | - { |
|
| 50 | - $definition = self::trim($definition); |
|
| 45 | + /** |
|
| 46 | + * @throws Exception |
|
| 47 | + */ |
|
| 48 | + protected function parseDefinition($definition) |
|
| 49 | + { |
|
| 50 | + $definition = self::trim($definition); |
|
| 51 | 51 | |
| 52 | - $match = []; |
|
| 53 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) { |
|
| 54 | - throw new Exception('Unparseable ' . $this->format . ' definition: \'' . $definition . '\''); |
|
| 55 | - } |
|
| 52 | + $match = []; |
|
| 53 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) { |
|
| 54 | + throw new Exception('Unparseable ' . $this->format . ' definition: \'' . $definition . '\''); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - if (strtolower($match[1]) !== $this->format) { |
|
| 58 | - throw new Exception('Not a ' . $this->format . ': \'' . $definition . '\''); |
|
| 59 | - } |
|
| 57 | + if (strtolower($match[1]) !== $this->format) { |
|
| 58 | + throw new Exception('Not a ' . $this->format . ': \'' . $definition . '\''); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $this->pattern = '^' . $this->regex . '$'; |
|
| 62 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 63 | - } |
|
| 61 | + $this->pattern = '^' . $this->regex . '$'; |
|
| 62 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - protected function validateDefault($value) |
|
| 66 | - { |
|
| 67 | - $value = parent::validateDefault($value); |
|
| 65 | + protected function validateDefault($value) |
|
| 66 | + { |
|
| 67 | + $value = parent::validateDefault($value); |
|
| 68 | 68 | |
| 69 | - if (preg_match('/' . $this->pattern . '/', $value) !== 1) { |
|
| 70 | - throw new Exception('Invalid ' . $this->format . ' default value'); |
|
| 71 | - } |
|
| 69 | + if (preg_match('/' . $this->pattern . '/', $value) !== 1) { |
|
| 70 | + throw new Exception('Invalid ' . $this->format . ' default value'); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return $value; |
|
| 74 | - } |
|
| 73 | + return $value; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | } |
@@ -15,157 +15,157 @@ |
||
| 15 | 15 | class NumberType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | - const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
| 20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 21 | - const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
| 22 | - |
|
| 23 | - private static $formats = array( |
|
| 24 | - 'float' => 'float', |
|
| 25 | - 'double' => 'double', |
|
| 26 | - ); |
|
| 27 | - private $format; |
|
| 28 | - private $default = null; |
|
| 29 | - private $maximum = null; |
|
| 30 | - private $exclusiveMaximum = null; |
|
| 31 | - private $minimum = null; |
|
| 32 | - private $exclusiveMinimum = null; |
|
| 33 | - private $enum = []; |
|
| 34 | - private $multipleOf = null; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param string $command The comment command |
|
| 38 | - * @param string $data Any data added after the command |
|
| 39 | - * @return AbstractType|boolean |
|
| 40 | - * @throws Exception |
|
| 41 | - * @throws Exception |
|
| 42 | - * @throws Exception |
|
| 43 | - */ |
|
| 44 | - public function handleCommand($command, $data = null) |
|
| 45 | - { |
|
| 46 | - switch (strtolower($command)) { |
|
| 47 | - case 'default': |
|
| 48 | - $this->default = $this->validateDefault($data); |
|
| 49 | - return $this; |
|
| 50 | - |
|
| 51 | - case 'enum': |
|
| 52 | - $words = self::wordSplit($data); |
|
| 53 | - foreach ($words as &$word) { |
|
| 54 | - $word = $this->validateDefault($word); |
|
| 55 | - } |
|
| 56 | - unset($word); |
|
| 57 | - $this->enum = array_merge($this->enum, $words); |
|
| 58 | - return $this; |
|
| 59 | - |
|
| 60 | - case 'step': |
|
| 61 | - if (($step = (float)$data) > 0) { |
|
| 62 | - $this->multipleOf = $step; |
|
| 63 | - } |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return parent::handleCommand($command, $data); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @throws Exception |
|
| 72 | - */ |
|
| 73 | - private function validateDefault($value) |
|
| 74 | - { |
|
| 75 | - if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
| 76 | - throw new Exception("Invalid number default: '{$value}'"); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - if ($this->maximum) { |
|
| 80 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
| 81 | - throw new Exception("Default number beyond maximum: '{$value}'"); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - if ($this->minimum) { |
|
| 85 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
| 86 | - throw new Exception("Default number beyond minimum: '{$value}'"); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return (float)$value; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function toArray(): array |
|
| 94 | - { |
|
| 95 | - return self::arrayFilterNull(array_merge(array( |
|
| 96 | - 'type' => 'number', |
|
| 97 | - 'format' => $this->format, |
|
| 98 | - 'default' => $this->default, |
|
| 99 | - 'minimum' => $this->minimum, |
|
| 100 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
| 101 | - 'maximum' => $this->maximum, |
|
| 102 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
| 103 | - 'enum' => $this->enum, |
|
| 104 | - 'multipleOf' => $this->multipleOf, |
|
| 105 | - ), parent::toArray())); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function __toString() |
|
| 109 | - { |
|
| 110 | - return __CLASS__; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @throws Exception |
|
| 115 | - */ |
|
| 116 | - protected function parseDefinition($definition) |
|
| 117 | - { |
|
| 118 | - $match = []; |
|
| 119 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 120 | - throw new Exception("Unparseable number definition: '{$definition}'"); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $this->parseFormat($definition, $match); |
|
| 124 | - $this->parseRange($definition, $match); |
|
| 125 | - $this->parseDefault($definition, $match); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param string[] $match |
|
| 130 | - * @throws Exception |
|
| 131 | - */ |
|
| 132 | - private function parseFormat($definition, $match) |
|
| 133 | - { |
|
| 134 | - if (!isset(self::$formats[strtolower($match[1])])) { |
|
| 135 | - throw new Exception("Not a number: '{$definition}'"); |
|
| 136 | - } |
|
| 137 | - $this->format = self::$formats[strtolower($match[1])]; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string[] $match |
|
| 142 | - * @throws Exception |
|
| 143 | - */ |
|
| 144 | - private function parseRange($definition, $match) |
|
| 145 | - { |
|
| 146 | - if (!empty($match[2])) { |
|
| 147 | - if ($match[3] === '' && $match[4] === '') { |
|
| 148 | - throw new Exception("Empty number range: '{$definition}'"); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
| 152 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
| 153 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
| 154 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
| 155 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
| 156 | - self::swap($this->minimum, $this->maximum); |
|
| 157 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param string[] $match |
|
| 164 | - * @throws Exception |
|
| 165 | - */ |
|
| 166 | - private function parseDefault($definition, $match) |
|
| 167 | - { |
|
| 168 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
| 169 | - } |
|
| 18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | + const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
| 20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 21 | + const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
| 22 | + |
|
| 23 | + private static $formats = array( |
|
| 24 | + 'float' => 'float', |
|
| 25 | + 'double' => 'double', |
|
| 26 | + ); |
|
| 27 | + private $format; |
|
| 28 | + private $default = null; |
|
| 29 | + private $maximum = null; |
|
| 30 | + private $exclusiveMaximum = null; |
|
| 31 | + private $minimum = null; |
|
| 32 | + private $exclusiveMinimum = null; |
|
| 33 | + private $enum = []; |
|
| 34 | + private $multipleOf = null; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param string $command The comment command |
|
| 38 | + * @param string $data Any data added after the command |
|
| 39 | + * @return AbstractType|boolean |
|
| 40 | + * @throws Exception |
|
| 41 | + * @throws Exception |
|
| 42 | + * @throws Exception |
|
| 43 | + */ |
|
| 44 | + public function handleCommand($command, $data = null) |
|
| 45 | + { |
|
| 46 | + switch (strtolower($command)) { |
|
| 47 | + case 'default': |
|
| 48 | + $this->default = $this->validateDefault($data); |
|
| 49 | + return $this; |
|
| 50 | + |
|
| 51 | + case 'enum': |
|
| 52 | + $words = self::wordSplit($data); |
|
| 53 | + foreach ($words as &$word) { |
|
| 54 | + $word = $this->validateDefault($word); |
|
| 55 | + } |
|
| 56 | + unset($word); |
|
| 57 | + $this->enum = array_merge($this->enum, $words); |
|
| 58 | + return $this; |
|
| 59 | + |
|
| 60 | + case 'step': |
|
| 61 | + if (($step = (float)$data) > 0) { |
|
| 62 | + $this->multipleOf = $step; |
|
| 63 | + } |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return parent::handleCommand($command, $data); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @throws Exception |
|
| 72 | + */ |
|
| 73 | + private function validateDefault($value) |
|
| 74 | + { |
|
| 75 | + if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
| 76 | + throw new Exception("Invalid number default: '{$value}'"); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + if ($this->maximum) { |
|
| 80 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
| 81 | + throw new Exception("Default number beyond maximum: '{$value}'"); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + if ($this->minimum) { |
|
| 85 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
| 86 | + throw new Exception("Default number beyond minimum: '{$value}'"); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return (float)$value; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function toArray(): array |
|
| 94 | + { |
|
| 95 | + return self::arrayFilterNull(array_merge(array( |
|
| 96 | + 'type' => 'number', |
|
| 97 | + 'format' => $this->format, |
|
| 98 | + 'default' => $this->default, |
|
| 99 | + 'minimum' => $this->minimum, |
|
| 100 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
| 101 | + 'maximum' => $this->maximum, |
|
| 102 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
| 103 | + 'enum' => $this->enum, |
|
| 104 | + 'multipleOf' => $this->multipleOf, |
|
| 105 | + ), parent::toArray())); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function __toString() |
|
| 109 | + { |
|
| 110 | + return __CLASS__; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @throws Exception |
|
| 115 | + */ |
|
| 116 | + protected function parseDefinition($definition) |
|
| 117 | + { |
|
| 118 | + $match = []; |
|
| 119 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 120 | + throw new Exception("Unparseable number definition: '{$definition}'"); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $this->parseFormat($definition, $match); |
|
| 124 | + $this->parseRange($definition, $match); |
|
| 125 | + $this->parseDefault($definition, $match); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param string[] $match |
|
| 130 | + * @throws Exception |
|
| 131 | + */ |
|
| 132 | + private function parseFormat($definition, $match) |
|
| 133 | + { |
|
| 134 | + if (!isset(self::$formats[strtolower($match[1])])) { |
|
| 135 | + throw new Exception("Not a number: '{$definition}'"); |
|
| 136 | + } |
|
| 137 | + $this->format = self::$formats[strtolower($match[1])]; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string[] $match |
|
| 142 | + * @throws Exception |
|
| 143 | + */ |
|
| 144 | + private function parseRange($definition, $match) |
|
| 145 | + { |
|
| 146 | + if (!empty($match[2])) { |
|
| 147 | + if ($match[3] === '' && $match[4] === '') { |
|
| 148 | + throw new Exception("Empty number range: '{$definition}'"); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
| 152 | + $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
| 153 | + $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
| 154 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
| 155 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
| 156 | + self::swap($this->minimum, $this->maximum); |
|
| 157 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param string[] $match |
|
| 164 | + * @throws Exception |
|
| 165 | + */ |
|
| 166 | + private function parseDefault($definition, $match) |
|
| 167 | + { |
|
| 168 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | 171 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | return $this; |
| 59 | 59 | |
| 60 | 60 | case 'step': |
| 61 | - if (($step = (float)$data) > 0) { |
|
| 61 | + if (($step = (float) $data) > 0) { |
|
| 62 | 62 | $this->multipleOf = $step; |
| 63 | 63 | } |
| 64 | 64 | return $this; |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | } |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - return (float)$value; |
|
| 90 | + return (float) $value; |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | public function toArray(): array |
@@ -149,8 +149,8 @@ discard block |
||
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | $this->exclusiveMinimum = $match[2] == '<'; |
| 152 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
| 153 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
| 152 | + $this->minimum = $match[3] === '' ? null : (float) $match[3]; |
|
| 153 | + $this->maximum = $match[4] === '' ? null : (float) $match[4]; |
|
| 154 | 154 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
| 155 | 155 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
| 156 | 156 | self::swap($this->minimum, $this->maximum); |