@@ -14,52 +14,52 @@ |
||
14 | 14 | */ |
15 | 15 | class AllOfType extends AbstractType |
16 | 16 | { |
17 | - private $allOfItems = []; |
|
18 | - private $mostRecentItem; |
|
17 | + private $allOfItems = []; |
|
18 | + private $mostRecentItem; |
|
19 | 19 | |
20 | - public function toArray(): array |
|
21 | - { |
|
22 | - $allOf = []; |
|
23 | - foreach ($this->allOfItems as $item) { |
|
24 | - $allOf[] = $item->toArray(); |
|
25 | - } |
|
26 | - return self::arrayFilterNull(array_merge(array( |
|
27 | - 'allOf' => $allOf, |
|
28 | - ), parent::toArray())); |
|
29 | - } |
|
20 | + public function toArray(): array |
|
21 | + { |
|
22 | + $allOf = []; |
|
23 | + foreach ($this->allOfItems as $item) { |
|
24 | + $allOf[] = $item->toArray(); |
|
25 | + } |
|
26 | + return self::arrayFilterNull(array_merge(array( |
|
27 | + 'allOf' => $allOf, |
|
28 | + ), parent::toArray())); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @throws Exception |
|
33 | - */ |
|
34 | - protected function parseDefinition($definition): void |
|
35 | - { |
|
36 | - $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
37 | - $inlineDef = ''; |
|
38 | - if (preg_match($pattern, $definition, $matches) |
|
39 | - && isset($matches[1])) { |
|
40 | - $inlineDef = $matches[1]; |
|
41 | - } |
|
42 | - if ($inlineDef) { |
|
43 | - foreach (self::parseList($inlineDef) as $item) { |
|
44 | - $this->handleCommand('item', $item); |
|
45 | - } |
|
46 | - } |
|
47 | - } |
|
31 | + /** |
|
32 | + * @throws Exception |
|
33 | + */ |
|
34 | + protected function parseDefinition($definition): void |
|
35 | + { |
|
36 | + $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
37 | + $inlineDef = ''; |
|
38 | + if (preg_match($pattern, $definition, $matches) |
|
39 | + && isset($matches[1])) { |
|
40 | + $inlineDef = $matches[1]; |
|
41 | + } |
|
42 | + if ($inlineDef) { |
|
43 | + foreach (self::parseList($inlineDef) as $item) { |
|
44 | + $this->handleCommand('item', $item); |
|
45 | + } |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @throws Exception |
|
51 | - */ |
|
52 | - public function handleCommand($command, $data = null) |
|
53 | - { |
|
54 | - if (strtolower($command) === 'item') { |
|
55 | - $this->mostRecentItem = self::typeFactory($this, $data); |
|
56 | - $this->allOfItems[] = $this->mostRecentItem; |
|
57 | - return $this; |
|
58 | - } |
|
59 | - if (isset($this->mostRecentItem) |
|
60 | - && $this->mostRecentItem->handleCommand($command, $data)) { |
|
61 | - return $this; |
|
62 | - } |
|
63 | - return parent::handleCommand($command, $data); |
|
64 | - } |
|
49 | + /** |
|
50 | + * @throws Exception |
|
51 | + */ |
|
52 | + public function handleCommand($command, $data = null) |
|
53 | + { |
|
54 | + if (strtolower($command) === 'item') { |
|
55 | + $this->mostRecentItem = self::typeFactory($this, $data); |
|
56 | + $this->allOfItems[] = $this->mostRecentItem; |
|
57 | + return $this; |
|
58 | + } |
|
59 | + if (isset($this->mostRecentItem) |
|
60 | + && $this->mostRecentItem->handleCommand($command, $data)) { |
|
61 | + return $this; |
|
62 | + } |
|
63 | + return parent::handleCommand($command, $data); |
|
64 | + } |
|
65 | 65 | } |
@@ -17,77 +17,77 @@ |
||
17 | 17 | class EmailType extends StringType implements ICustomType |
18 | 18 | { |
19 | 19 | |
20 | - public 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(): array |
|
42 | - { |
|
43 | - return self::$formats; |
|
44 | - } |
|
45 | - |
|
46 | - public static function setFormats(array $formats): void |
|
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): void |
|
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): string |
|
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 | + public 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(): array |
|
42 | + { |
|
43 | + return self::$formats; |
|
44 | + } |
|
45 | + |
|
46 | + public static function setFormats(array $formats): void |
|
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): void |
|
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): string |
|
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 | } |
@@ -16,77 +16,77 @@ |
||
16 | 16 | */ |
17 | 17 | class MacType extends StringType implements ICustomType |
18 | 18 | { |
19 | - public const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$'; |
|
20 | - |
|
21 | - /** |
|
22 | - * List of formats recognized by this class |
|
23 | - * @var string[] |
|
24 | - */ |
|
25 | - private static $formats = array('mac'); |
|
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 MAC definition: '{$definition}'"); |
|
63 | - } |
|
64 | - |
|
65 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
66 | - throw new Exception("Not a MAC: '{$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 MAC default"); |
|
83 | - } |
|
84 | - |
|
85 | - if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) { |
|
86 | - throw new Exception("Invalid MAC default value: '{$value}'"); |
|
87 | - } |
|
88 | - |
|
89 | - return $value; |
|
90 | - } |
|
19 | + public const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$'; |
|
20 | + |
|
21 | + /** |
|
22 | + * List of formats recognized by this class |
|
23 | + * @var string[] |
|
24 | + */ |
|
25 | + private static $formats = array('mac'); |
|
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 MAC definition: '{$definition}'"); |
|
63 | + } |
|
64 | + |
|
65 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
66 | + throw new Exception("Not a MAC: '{$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 MAC default"); |
|
83 | + } |
|
84 | + |
|
85 | + if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) { |
|
86 | + throw new Exception("Invalid MAC default value: '{$value}'"); |
|
87 | + } |
|
88 | + |
|
89 | + return $value; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | } |
@@ -16,77 +16,77 @@ |
||
16 | 16 | */ |
17 | 17 | class Ipv6Type extends StringType implements ICustomType |
18 | 18 | { |
19 | - public 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]))'; |
|
20 | - |
|
21 | - /** |
|
22 | - * List of formats recognized by this class |
|
23 | - * @var string[] |
|
24 | - */ |
|
25 | - private static $formats = array('ipv6'); |
|
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 IPv6 definition: '{$definition}'"); |
|
63 | - } |
|
64 | - |
|
65 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
66 | - throw new Exception("Not an IPv6: '{$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 IPv6 default"); |
|
83 | - } |
|
84 | - |
|
85 | - if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
|
86 | - throw new Exception("Invalid IPv6 default value: '{$value}'"); |
|
87 | - } |
|
88 | - |
|
89 | - return $value; |
|
90 | - } |
|
19 | + public 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]))'; |
|
20 | + |
|
21 | + /** |
|
22 | + * List of formats recognized by this class |
|
23 | + * @var string[] |
|
24 | + */ |
|
25 | + private static $formats = array('ipv6'); |
|
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 IPv6 definition: '{$definition}'"); |
|
63 | + } |
|
64 | + |
|
65 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
66 | + throw new Exception("Not an IPv6: '{$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 IPv6 default"); |
|
83 | + } |
|
84 | + |
|
85 | + if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
|
86 | + throw new Exception("Invalid IPv6 default value: '{$value}'"); |
|
87 | + } |
|
88 | + |
|
89 | + return $value; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | } |
@@ -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 | } |