@@ -16,77 +16,77 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Ipv4Type extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | - public const PATTERN = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$'; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * List of formats recognized by this class |
|
| 23 | - * @var string[] |
|
| 24 | - */ |
|
| 25 | - private static $formats = array('ipv4'); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Construct and set up the regular expression for this type |
|
| 29 | - * |
|
| 30 | - * @param AbstractObject $parent |
|
| 31 | - * @param string $definition |
|
| 32 | - */ |
|
| 33 | - public function __construct(AbstractObject $parent, $definition) |
|
| 34 | - { |
|
| 35 | - $this->pattern = self::PATTERN; |
|
| 36 | - |
|
| 37 | - parent::__construct($parent, $definition); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public static function getFormats(): array |
|
| 41 | - { |
|
| 42 | - return self::$formats; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public static function setFormats(array $formats): void |
|
| 46 | - { |
|
| 47 | - self::$formats = $formats; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Parse a type definition string, assuming it belongs to this type |
|
| 52 | - * |
|
| 53 | - * @param string $definition |
|
| 54 | - * @throws Exception |
|
| 55 | - */ |
|
| 56 | - protected function parseDefinition($definition): void |
|
| 57 | - { |
|
| 58 | - $definition = self::trim($definition); |
|
| 59 | - |
|
| 60 | - $match = []; |
|
| 61 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 62 | - throw new Exception("Unparseable IPv4 definition: '{$definition}'"); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 66 | - throw new Exception("Not an IPv4: '{$definition}'"); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Check (and optionally reformat) a default value |
|
| 74 | - * |
|
| 75 | - * @param string $value |
|
| 76 | - * @return string |
|
| 77 | - * @throws Exception |
|
| 78 | - */ |
|
| 79 | - protected function validateDefault($value): string |
|
| 80 | - { |
|
| 81 | - if (empty($value)) { |
|
| 82 | - throw new Exception("Empty IPv4 default"); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
|
| 86 | - throw new Exception("Invalid IPv4 default value: '{$value}'"); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return $value; |
|
| 90 | - } |
|
| 19 | + public const PATTERN = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$'; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * List of formats recognized by this class |
|
| 23 | + * @var string[] |
|
| 24 | + */ |
|
| 25 | + private static $formats = array('ipv4'); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Construct and set up the regular expression for this type |
|
| 29 | + * |
|
| 30 | + * @param AbstractObject $parent |
|
| 31 | + * @param string $definition |
|
| 32 | + */ |
|
| 33 | + public function __construct(AbstractObject $parent, $definition) |
|
| 34 | + { |
|
| 35 | + $this->pattern = self::PATTERN; |
|
| 36 | + |
|
| 37 | + parent::__construct($parent, $definition); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public static function getFormats(): array |
|
| 41 | + { |
|
| 42 | + return self::$formats; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public static function setFormats(array $formats): void |
|
| 46 | + { |
|
| 47 | + self::$formats = $formats; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Parse a type definition string, assuming it belongs to this type |
|
| 52 | + * |
|
| 53 | + * @param string $definition |
|
| 54 | + * @throws Exception |
|
| 55 | + */ |
|
| 56 | + protected function parseDefinition($definition): void |
|
| 57 | + { |
|
| 58 | + $definition = self::trim($definition); |
|
| 59 | + |
|
| 60 | + $match = []; |
|
| 61 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 62 | + throw new Exception("Unparseable IPv4 definition: '{$definition}'"); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 66 | + throw new Exception("Not an IPv4: '{$definition}'"); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Check (and optionally reformat) a default value |
|
| 74 | + * |
|
| 75 | + * @param string $value |
|
| 76 | + * @return string |
|
| 77 | + * @throws Exception |
|
| 78 | + */ |
|
| 79 | + protected function validateDefault($value): string |
|
| 80 | + { |
|
| 81 | + if (empty($value)) { |
|
| 82 | + throw new Exception("Empty IPv4 default"); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
|
| 86 | + throw new Exception("Invalid IPv4 default value: '{$value}'"); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return $value; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | } |
@@ -13,16 +13,16 @@ |
||
| 13 | 13 | interface ICustomType |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Return a list of formats recognized by this type |
|
| 18 | - * @return string[] |
|
| 19 | - */ |
|
| 20 | - public static function getFormats(): array; |
|
| 16 | + /** |
|
| 17 | + * Return a list of formats recognized by this type |
|
| 18 | + * @return string[] |
|
| 19 | + */ |
|
| 20 | + public static function getFormats(): array; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Overwrite format names recognized by this type |
|
| 24 | - * @param string[] $formats |
|
| 25 | - */ |
|
| 26 | - public static function setFormats(array $formats); |
|
| 22 | + /** |
|
| 23 | + * Overwrite format names recognized by this type |
|
| 24 | + * @param string[] $formats |
|
| 25 | + */ |
|
| 26 | + public static function setFormats(array $formats); |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -17,45 +17,45 @@ |
||
| 17 | 17 | class FileType extends AbstractType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - public function toArray(): array |
|
| 21 | - { |
|
| 22 | - return self::arrayFilterNull(array_merge(array( |
|
| 23 | - 'type' => 'file', |
|
| 24 | - ), parent::toArray())); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function __toString() |
|
| 28 | - { |
|
| 29 | - return __CLASS__; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @throws Exception |
|
| 34 | - */ |
|
| 35 | - protected function parseDefinition($definition): void |
|
| 36 | - { |
|
| 37 | - $type = strtolower($definition); |
|
| 38 | - |
|
| 39 | - if ($type !== 'file') { |
|
| 40 | - throw new Exception("Not a file: '{$definition}'"); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $parent = $this->getParent(); |
|
| 44 | - if (!($parent instanceof Parameter) || !$parent->isForm()) { |
|
| 45 | - throw new Exception("File type '{$definition}' only allowed on form parameter"); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** @var Operation $parentOperation */ |
|
| 49 | - $parentOperation = $this->getParentClass(Operation::class); |
|
| 50 | - $consumes = $parentOperation->getConsumes(); |
|
| 51 | - if (empty($consumes)) { |
|
| 52 | - $consumes = $this->getSwagger()->getConsumes(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $valid_consumes = ((int)in_array('multipart/form-data', $consumes, true)) + ((int)in_array('application/x-www-form-urlencoded', $consumes, true)); |
|
| 56 | - if (empty($consumes) || $valid_consumes !== count($consumes)) { |
|
| 57 | - throw new Exception("File type '{$definition}' without valid consume"); |
|
| 58 | - } |
|
| 59 | - } |
|
| 20 | + public function toArray(): array |
|
| 21 | + { |
|
| 22 | + return self::arrayFilterNull(array_merge(array( |
|
| 23 | + 'type' => 'file', |
|
| 24 | + ), parent::toArray())); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function __toString() |
|
| 28 | + { |
|
| 29 | + return __CLASS__; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @throws Exception |
|
| 34 | + */ |
|
| 35 | + protected function parseDefinition($definition): void |
|
| 36 | + { |
|
| 37 | + $type = strtolower($definition); |
|
| 38 | + |
|
| 39 | + if ($type !== 'file') { |
|
| 40 | + throw new Exception("Not a file: '{$definition}'"); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $parent = $this->getParent(); |
|
| 44 | + if (!($parent instanceof Parameter) || !$parent->isForm()) { |
|
| 45 | + throw new Exception("File type '{$definition}' only allowed on form parameter"); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** @var Operation $parentOperation */ |
|
| 49 | + $parentOperation = $this->getParentClass(Operation::class); |
|
| 50 | + $consumes = $parentOperation->getConsumes(); |
|
| 51 | + if (empty($consumes)) { |
|
| 52 | + $consumes = $this->getSwagger()->getConsumes(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $valid_consumes = ((int)in_array('multipart/form-data', $consumes, true)) + ((int)in_array('application/x-www-form-urlencoded', $consumes, true)); |
|
| 56 | + if (empty($consumes) || $valid_consumes !== count($consumes)) { |
|
| 57 | + throw new Exception("File type '{$definition}' without valid consume"); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | } |
@@ -52,7 +52,7 @@ |
||
| 52 | 52 | $consumes = $this->getSwagger()->getConsumes(); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - $valid_consumes = ((int)in_array('multipart/form-data', $consumes, true)) + ((int)in_array('application/x-www-form-urlencoded', $consumes, true)); |
|
| 55 | + $valid_consumes = ((int) in_array('multipart/form-data', $consumes, true)) + ((int) in_array('application/x-www-form-urlencoded', $consumes, true)); |
|
| 56 | 56 | if (empty($consumes) || $valid_consumes !== count($consumes)) { |
| 57 | 57 | throw new Exception("File type '{$definition}' without valid consume"); |
| 58 | 58 | } |
@@ -15,61 +15,61 @@ |
||
| 15 | 15 | class BooleanType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | - protected const REGEX_DEFAULT = '(?:=(true|false|1|0))?'; |
|
| 18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | + protected const REGEX_DEFAULT = '(?:=(true|false|1|0))?'; |
|
| 20 | 20 | |
| 21 | - private $default; |
|
| 21 | + private $default; |
|
| 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'), true)) { |
|
| 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'), true)) { |
|
| 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): void |
|
| 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): void |
|
| 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 | - public 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; |
|
| 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?->format(self::$datetime_formats[$this->format]), |
|
| 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): void |
|
| 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 | + public 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; |
|
| 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?->format(self::$datetime_formats[$this->format]), |
|
| 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): void |
|
| 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; |
|
| 32 | - private $maximum; |
|
| 33 | - private $exclusiveMaximum; |
|
| 34 | - private $minimum; |
|
| 35 | - private $exclusiveMinimum; |
|
| 36 | - private $enum = []; |
|
| 37 | - private $multipleOf; |
|
| 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): int |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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; |
|
| 32 | + private $maximum; |
|
| 33 | + private $exclusiveMaximum; |
|
| 34 | + private $minimum; |
|
| 35 | + private $exclusiveMinimum; |
|
| 36 | + private $enum = []; |
|
| 37 | + private $multipleOf; |
|
| 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): int |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 49 | - { |
|
| 50 | - $definition = self::trim($definition); |
|
| 45 | + /** |
|
| 46 | + * @throws Exception |
|
| 47 | + */ |
|
| 48 | + protected function parseDefinition($definition): void |
|
| 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): string |
|
| 66 | - { |
|
| 67 | - $value = parent::validateDefault($value); |
|
| 65 | + protected function validateDefault($value): string |
|
| 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; |
|
| 29 | - private $maximum; |
|
| 30 | - private $exclusiveMaximum; |
|
| 31 | - private $minimum; |
|
| 32 | - private $exclusiveMinimum; |
|
| 33 | - private $enum = []; |
|
| 34 | - private $multipleOf; |
|
| 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): float |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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; |
|
| 29 | + private $maximum; |
|
| 30 | + private $exclusiveMaximum; |
|
| 31 | + private $minimum; |
|
| 32 | + private $exclusiveMinimum; |
|
| 33 | + private $enum = []; |
|
| 34 | + private $multipleOf; |
|
| 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): float |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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): void |
|
| 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); |
@@ -16,182 +16,182 @@ |
||
| 16 | 16 | class ArrayType extends AbstractType |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 20 | - private const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?'; |
|
| 21 | - |
|
| 22 | - private static $collectionFormats = array( |
|
| 23 | - 'array' => 'csv', |
|
| 24 | - 'csv' => 'csv', |
|
| 25 | - 'ssv' => 'ssv', |
|
| 26 | - 'tsv' => 'tsv', |
|
| 27 | - 'pipes' => 'pipes', |
|
| 28 | - 'multi' => 'multi', |
|
| 29 | - ); |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var AbstractType |
|
| 33 | - */ |
|
| 34 | - private $Items; |
|
| 35 | - private $minItems; |
|
| 36 | - private $maxItems; |
|
| 37 | - private $collectionFormat; |
|
| 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 | - * @throws Exception |
|
| 47 | - * @throws Exception |
|
| 48 | - * @throws Exception |
|
| 49 | - * @throws Exception |
|
| 50 | - */ |
|
| 51 | - public function handleCommand($command, $data = null) |
|
| 52 | - { |
|
| 53 | - if ($this->Items) { |
|
| 54 | - $return = $this->Items->handleCommand($command, $data); |
|
| 55 | - if ($return) { |
|
| 56 | - return $return; |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - switch (strtolower($command)) { |
|
| 61 | - case 'min': |
|
| 62 | - $this->minItems = (int)$data; |
|
| 63 | - if ($this->minItems < 0) { |
|
| 64 | - throw new Exception("Minimum less than zero: '$data'"); |
|
| 65 | - } |
|
| 66 | - if ($this->maxItems !== null && $this->minItems > $this->maxItems) { |
|
| 67 | - throw new Exception("Minimum greater than maximum: '$data'"); |
|
| 68 | - } |
|
| 69 | - return $this; |
|
| 70 | - |
|
| 71 | - case 'max': |
|
| 72 | - $this->maxItems = (int)$data; |
|
| 73 | - if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
|
| 74 | - throw new Exception("Maximum less than minimum: '$data'"); |
|
| 75 | - } |
|
| 76 | - if ($this->maxItems < 0) { |
|
| 77 | - throw new Exception("Maximum less than zero: '$data'"); |
|
| 78 | - } |
|
| 79 | - return $this; |
|
| 80 | - |
|
| 81 | - case 'items': |
|
| 82 | - $this->Items = $this->validateItems($data); |
|
| 83 | - return $this->Items; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return parent::handleCommand($command, $data); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @throws Exception |
|
| 91 | - */ |
|
| 92 | - private function validateItems($items) |
|
| 93 | - { |
|
| 94 | - if (empty($items)) { |
|
| 95 | - throw new Exception("Empty items definition: '$items'"); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - return self::typeFactory($this, $items, "Unparseable items definition: '%s'"); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function toArray(): array |
|
| 102 | - { |
|
| 103 | - return self::arrayFilterNull(array_merge(array( |
|
| 104 | - 'type' => 'array', |
|
| 105 | - 'items' => empty($this->Items) ? null : $this->Items->toArray(), |
|
| 106 | - 'collectionFormat' => $this->collectionFormat === 'csv' ? null : $this->collectionFormat, |
|
| 107 | - 'minItems' => $this->minItems, |
|
| 108 | - 'maxItems' => $this->maxItems, |
|
| 109 | - ), parent::toArray())); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - public function __toString() |
|
| 113 | - { |
|
| 114 | - return __CLASS__; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @throws Exception |
|
| 119 | - */ |
|
| 120 | - protected function parseDefinition($definition): void |
|
| 121 | - { |
|
| 122 | - $definition = self::trim($definition); |
|
| 123 | - $match = []; |
|
| 124 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 125 | - $match[1] = strtolower($match[1]); |
|
| 126 | - } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 127 | - $match[1] = 'array'; |
|
| 128 | - } else { |
|
| 129 | - throw new Exception('Unparseable array definition: \'' . $definition . '\''); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $this->parseFormat($definition, $match); |
|
| 133 | - $this->parseItems($definition, $match); |
|
| 134 | - $this->parseRange($definition, $match); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param string $definition |
|
| 139 | - * @param string[] $match |
|
| 140 | - * @throws Exception |
|
| 141 | - * @throws Exception |
|
| 142 | - */ |
|
| 143 | - private function parseFormat($definition, $match): void |
|
| 144 | - { |
|
| 145 | - $type = strtolower($match[1]); |
|
| 146 | - if (!isset(self::$collectionFormats[$type])) { |
|
| 147 | - throw new Exception("Not an array: '$definition'"); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - if ($type === 'multi') { |
|
| 151 | - $parent = $this->getParent(); |
|
| 152 | - if (!($parent instanceof Parameter) || !$parent->isMulti()) { |
|
| 153 | - throw new Exception("Multi array only allowed on query or form parameter: '$definition'"); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $this->collectionFormat = self::$collectionFormats[$type]; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param string $definition |
|
| 162 | - * @param string[] $match |
|
| 163 | - * @throws Exception |
|
| 164 | - */ |
|
| 165 | - private function parseItems($definition, $match): void |
|
| 166 | - { |
|
| 167 | - if (!empty($match[2])) { |
|
| 168 | - $this->Items = $this->validateItems($match[2]); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @param string $definition |
|
| 174 | - * @param string[] $match |
|
| 175 | - * @throws Exception |
|
| 176 | - */ |
|
| 177 | - private function parseRange($definition, $match): void |
|
| 178 | - { |
|
| 179 | - if (!empty($match[3])) { |
|
| 180 | - if ($match[4] === '' && $match[5] === '') { |
|
| 181 | - throw new Exception("Empty array range: '$definition'"); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $exclusiveMinimum = $match[3] === '<'; |
|
| 185 | - $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
| 186 | - $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
| 187 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
| 188 | - if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
|
| 189 | - self::swap($this->minItems, $this->maxItems); |
|
| 190 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
| 191 | - } |
|
| 192 | - $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems); |
|
| 193 | - $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems); |
|
| 194 | - } |
|
| 195 | - } |
|
| 19 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 20 | + private const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?'; |
|
| 21 | + |
|
| 22 | + private static $collectionFormats = array( |
|
| 23 | + 'array' => 'csv', |
|
| 24 | + 'csv' => 'csv', |
|
| 25 | + 'ssv' => 'ssv', |
|
| 26 | + 'tsv' => 'tsv', |
|
| 27 | + 'pipes' => 'pipes', |
|
| 28 | + 'multi' => 'multi', |
|
| 29 | + ); |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var AbstractType |
|
| 33 | + */ |
|
| 34 | + private $Items; |
|
| 35 | + private $minItems; |
|
| 36 | + private $maxItems; |
|
| 37 | + private $collectionFormat; |
|
| 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 | + * @throws Exception |
|
| 47 | + * @throws Exception |
|
| 48 | + * @throws Exception |
|
| 49 | + * @throws Exception |
|
| 50 | + */ |
|
| 51 | + public function handleCommand($command, $data = null) |
|
| 52 | + { |
|
| 53 | + if ($this->Items) { |
|
| 54 | + $return = $this->Items->handleCommand($command, $data); |
|
| 55 | + if ($return) { |
|
| 56 | + return $return; |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + switch (strtolower($command)) { |
|
| 61 | + case 'min': |
|
| 62 | + $this->minItems = (int)$data; |
|
| 63 | + if ($this->minItems < 0) { |
|
| 64 | + throw new Exception("Minimum less than zero: '$data'"); |
|
| 65 | + } |
|
| 66 | + if ($this->maxItems !== null && $this->minItems > $this->maxItems) { |
|
| 67 | + throw new Exception("Minimum greater than maximum: '$data'"); |
|
| 68 | + } |
|
| 69 | + return $this; |
|
| 70 | + |
|
| 71 | + case 'max': |
|
| 72 | + $this->maxItems = (int)$data; |
|
| 73 | + if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
|
| 74 | + throw new Exception("Maximum less than minimum: '$data'"); |
|
| 75 | + } |
|
| 76 | + if ($this->maxItems < 0) { |
|
| 77 | + throw new Exception("Maximum less than zero: '$data'"); |
|
| 78 | + } |
|
| 79 | + return $this; |
|
| 80 | + |
|
| 81 | + case 'items': |
|
| 82 | + $this->Items = $this->validateItems($data); |
|
| 83 | + return $this->Items; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return parent::handleCommand($command, $data); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @throws Exception |
|
| 91 | + */ |
|
| 92 | + private function validateItems($items) |
|
| 93 | + { |
|
| 94 | + if (empty($items)) { |
|
| 95 | + throw new Exception("Empty items definition: '$items'"); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + return self::typeFactory($this, $items, "Unparseable items definition: '%s'"); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function toArray(): array |
|
| 102 | + { |
|
| 103 | + return self::arrayFilterNull(array_merge(array( |
|
| 104 | + 'type' => 'array', |
|
| 105 | + 'items' => empty($this->Items) ? null : $this->Items->toArray(), |
|
| 106 | + 'collectionFormat' => $this->collectionFormat === 'csv' ? null : $this->collectionFormat, |
|
| 107 | + 'minItems' => $this->minItems, |
|
| 108 | + 'maxItems' => $this->maxItems, |
|
| 109 | + ), parent::toArray())); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + public function __toString() |
|
| 113 | + { |
|
| 114 | + return __CLASS__; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @throws Exception |
|
| 119 | + */ |
|
| 120 | + protected function parseDefinition($definition): void |
|
| 121 | + { |
|
| 122 | + $definition = self::trim($definition); |
|
| 123 | + $match = []; |
|
| 124 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 125 | + $match[1] = strtolower($match[1]); |
|
| 126 | + } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 127 | + $match[1] = 'array'; |
|
| 128 | + } else { |
|
| 129 | + throw new Exception('Unparseable array definition: \'' . $definition . '\''); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $this->parseFormat($definition, $match); |
|
| 133 | + $this->parseItems($definition, $match); |
|
| 134 | + $this->parseRange($definition, $match); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param string $definition |
|
| 139 | + * @param string[] $match |
|
| 140 | + * @throws Exception |
|
| 141 | + * @throws Exception |
|
| 142 | + */ |
|
| 143 | + private function parseFormat($definition, $match): void |
|
| 144 | + { |
|
| 145 | + $type = strtolower($match[1]); |
|
| 146 | + if (!isset(self::$collectionFormats[$type])) { |
|
| 147 | + throw new Exception("Not an array: '$definition'"); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + if ($type === 'multi') { |
|
| 151 | + $parent = $this->getParent(); |
|
| 152 | + if (!($parent instanceof Parameter) || !$parent->isMulti()) { |
|
| 153 | + throw new Exception("Multi array only allowed on query or form parameter: '$definition'"); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $this->collectionFormat = self::$collectionFormats[$type]; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param string $definition |
|
| 162 | + * @param string[] $match |
|
| 163 | + * @throws Exception |
|
| 164 | + */ |
|
| 165 | + private function parseItems($definition, $match): void |
|
| 166 | + { |
|
| 167 | + if (!empty($match[2])) { |
|
| 168 | + $this->Items = $this->validateItems($match[2]); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @param string $definition |
|
| 174 | + * @param string[] $match |
|
| 175 | + * @throws Exception |
|
| 176 | + */ |
|
| 177 | + private function parseRange($definition, $match): void |
|
| 178 | + { |
|
| 179 | + if (!empty($match[3])) { |
|
| 180 | + if ($match[4] === '' && $match[5] === '') { |
|
| 181 | + throw new Exception("Empty array range: '$definition'"); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $exclusiveMinimum = $match[3] === '<'; |
|
| 185 | + $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
| 186 | + $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
| 187 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
| 188 | + if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
|
| 189 | + self::swap($this->minItems, $this->maxItems); |
|
| 190 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
| 191 | + } |
|
| 192 | + $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems); |
|
| 193 | + $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | switch (strtolower($command)) { |
| 61 | 61 | case 'min': |
| 62 | - $this->minItems = (int)$data; |
|
| 62 | + $this->minItems = (int) $data; |
|
| 63 | 63 | if ($this->minItems < 0) { |
| 64 | 64 | throw new Exception("Minimum less than zero: '$data'"); |
| 65 | 65 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | return $this; |
| 70 | 70 | |
| 71 | 71 | case 'max': |
| 72 | - $this->maxItems = (int)$data; |
|
| 72 | + $this->maxItems = (int) $data; |
|
| 73 | 73 | if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
| 74 | 74 | throw new Exception("Maximum less than minimum: '$data'"); |
| 75 | 75 | } |
@@ -182,8 +182,8 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | $exclusiveMinimum = $match[3] === '<'; |
| 185 | - $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
| 186 | - $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
| 185 | + $this->minItems = $match[4] === '' ? null : (int) $match[4]; |
|
| 186 | + $this->maxItems = $match[5] === '' ? null : (int) $match[5]; |
|
| 187 | 187 | $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
| 188 | 188 | if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
| 189 | 189 | self::swap($this->minItems, $this->maxItems); |