@@ -15,77 +15,77 @@ |
||
| 15 | 15 | class BodyParameter extends AbstractObject implements IParameter |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var string|boolean |
|
| 20 | - */ |
|
| 21 | - private $name = ''; |
|
| 22 | - private $description; |
|
| 23 | - private $required = false; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var Schema |
|
| 27 | - */ |
|
| 28 | - private $schema; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @throws Exception |
|
| 32 | - */ |
|
| 33 | - public function __construct(AbstractObject $parent, $data, $required = false) |
|
| 34 | - { |
|
| 35 | - parent::__construct($parent); |
|
| 36 | - |
|
| 37 | - $type = self::wordShift($data); |
|
| 38 | - if (empty($type)) { |
|
| 39 | - throw new Exception('No type definition for body parameter'); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $this->name = self::wordShift($data); |
|
| 43 | - if (empty($this->name)) { |
|
| 44 | - throw new Exception('No name for body parameter'); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $this->description = $data; |
|
| 48 | - $this->required = (bool)$required; |
|
| 49 | - |
|
| 50 | - $this->schema = new Schema($this, $type); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param string $command |
|
| 55 | - * @param string $data |
|
| 56 | - * @return AbstractObject|boolean |
|
| 57 | - * @throws Exception |
|
| 58 | - */ |
|
| 59 | - public function handleCommand($command, $data = null) |
|
| 60 | - { |
|
| 61 | - // Pass through to Type |
|
| 62 | - $return = $this->schema->handleCommand($command, $data); |
|
| 63 | - if ($return) { |
|
| 64 | - return $return; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return parent::handleCommand($command, $data); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function toArray() |
|
| 71 | - { |
|
| 72 | - return self::arrayFilterNull(array_merge(array( |
|
| 73 | - 'name' => $this->name, |
|
| 74 | - 'in' => 'body', |
|
| 75 | - 'description' => empty($this->description) ? null : $this->description, |
|
| 76 | - 'required' => $this->required ? true : null, |
|
| 77 | - 'schema' => $this->schema->toArray(), |
|
| 78 | - ), parent::toArray())); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function __toString() |
|
| 82 | - { |
|
| 83 | - return __CLASS__ . ' ' . $this->name; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function getName() |
|
| 87 | - { |
|
| 88 | - return $this->name; |
|
| 89 | - } |
|
| 18 | + /** |
|
| 19 | + * @var string|boolean |
|
| 20 | + */ |
|
| 21 | + private $name = ''; |
|
| 22 | + private $description; |
|
| 23 | + private $required = false; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var Schema |
|
| 27 | + */ |
|
| 28 | + private $schema; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @throws Exception |
|
| 32 | + */ |
|
| 33 | + public function __construct(AbstractObject $parent, $data, $required = false) |
|
| 34 | + { |
|
| 35 | + parent::__construct($parent); |
|
| 36 | + |
|
| 37 | + $type = self::wordShift($data); |
|
| 38 | + if (empty($type)) { |
|
| 39 | + throw new Exception('No type definition for body parameter'); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $this->name = self::wordShift($data); |
|
| 43 | + if (empty($this->name)) { |
|
| 44 | + throw new Exception('No name for body parameter'); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $this->description = $data; |
|
| 48 | + $this->required = (bool)$required; |
|
| 49 | + |
|
| 50 | + $this->schema = new Schema($this, $type); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $command |
|
| 55 | + * @param string $data |
|
| 56 | + * @return AbstractObject|boolean |
|
| 57 | + * @throws Exception |
|
| 58 | + */ |
|
| 59 | + public function handleCommand($command, $data = null) |
|
| 60 | + { |
|
| 61 | + // Pass through to Type |
|
| 62 | + $return = $this->schema->handleCommand($command, $data); |
|
| 63 | + if ($return) { |
|
| 64 | + return $return; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return parent::handleCommand($command, $data); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function toArray() |
|
| 71 | + { |
|
| 72 | + return self::arrayFilterNull(array_merge(array( |
|
| 73 | + 'name' => $this->name, |
|
| 74 | + 'in' => 'body', |
|
| 75 | + 'description' => empty($this->description) ? null : $this->description, |
|
| 76 | + 'required' => $this->required ? true : null, |
|
| 77 | + 'schema' => $this->schema->toArray(), |
|
| 78 | + ), parent::toArray())); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function __toString() |
|
| 82 | + { |
|
| 83 | + return __CLASS__ . ' ' . $this->name; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function getName() |
|
| 87 | + { |
|
| 88 | + return $this->name; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | 91 | } |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | $this->description = $data; |
| 48 | - $this->required = (bool)$required; |
|
| 48 | + $this->required = (bool) $required; |
|
| 49 | 49 | |
| 50 | 50 | $this->schema = new Schema($this, $type); |
| 51 | 51 | } |
@@ -14,94 +14,94 @@ |
||
| 14 | 14 | class License extends AbstractObject |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - // @todo make this a separate resource file? (licenseUrls.json) |
|
| 18 | - private static $licenses = array( |
|
| 19 | - 'artistic-1.0' => 'http://opensource.org/licenses/artistic-license-1.0', |
|
| 20 | - 'artistic-1' => 'http://opensource.org/licenses/artistic-license-1.0', |
|
| 21 | - 'artistic-2.0' => 'http://opensource.org/licenses/artistic-license-2.0', |
|
| 22 | - 'artistic-2' => 'http://opensource.org/licenses/artistic-license-2.0', |
|
| 23 | - 'artistic' => 'http://opensource.org/licenses/artistic-license-2.0', |
|
| 24 | - 'bsd-new' => 'https://opensource.org/licenses/BSD-3-Clause', |
|
| 25 | - 'bsd-3' => 'https://opensource.org/licenses/BSD-3-Clause', |
|
| 26 | - 'bsd-2' => 'https://opensource.org/licenses/BSD-2-Clause', |
|
| 27 | - 'bsd' => 'https://opensource.org/licenses/BSD-2-Clause', |
|
| 28 | - 'epl-1.0' => 'http://www.eclipse.org/legal/epl-v10.html', |
|
| 29 | - 'epl-1' => 'http://www.eclipse.org/legal/epl-v10.html', |
|
| 30 | - 'epl' => 'http://www.eclipse.org/legal/epl-v10.html', |
|
| 31 | - 'apache-2.0' => 'http://www.apache.org/licenses/LICENSE-2.0.html', |
|
| 32 | - 'apache-2' => 'http://www.apache.org/licenses/LICENSE-2.0.html', |
|
| 33 | - 'apache' => 'http://www.apache.org/licenses/LICENSE-2.0.html', |
|
| 34 | - 'gpl-1.0' => 'https://www.gnu.org/licenses/gpl-1.0.html', |
|
| 35 | - 'gpl-1' => 'https://www.gnu.org/licenses/gpl-1.0.html', |
|
| 36 | - 'gpl-2.0' => 'https://www.gnu.org/licenses/gpl-2.0.html', |
|
| 37 | - 'gpl-2' => 'https://www.gnu.org/licenses/gpl-2.0.html', |
|
| 38 | - 'gpl-3.0' => 'http://www.gnu.org/licenses/gpl-3.0.html', |
|
| 39 | - 'gpl-3' => 'http://www.gnu.org/licenses/gpl-3.0.html', |
|
| 40 | - 'gpl' => 'http://www.gnu.org/licenses/gpl-3.0.html', |
|
| 41 | - 'lgpl-2.0' => 'http://www.gnu.org/licenses/lgpl-2.0.html', |
|
| 42 | - 'lgpl-2.1' => 'http://www.gnu.org/licenses/lgpl-2.1.html', |
|
| 43 | - 'lgpl-2' => 'http://www.gnu.org/licenses/lgpl-2.1.html', |
|
| 44 | - 'lgpl-3.0' => 'http://www.gnu.org/licenses/lgpl-3.0.html', |
|
| 45 | - 'lgpl-3' => 'http://www.gnu.org/licenses/lgpl-3.0.html', |
|
| 46 | - 'lgpl' => 'http://www.gnu.org/licenses/lgpl-3.0.html', |
|
| 47 | - 'mit' => 'http://opensource.org/licenses/MIT', |
|
| 48 | - 'mpl-1.1' => 'https://www.mozilla.org/en-US/MPL/1.1/', |
|
| 49 | - 'mpl-1' => 'https://www.mozilla.org/en-US/MPL/1.1/', |
|
| 50 | - 'mpl-2.0' => 'https://www.mozilla.org/en-US/MPL/', |
|
| 51 | - 'mpl-2' => 'https://www.mozilla.org/en-US/MPL/', |
|
| 52 | - 'mpl' => 'https://www.mozilla.org/en-US/MPL/', |
|
| 53 | - 'mspl' => 'https://msdn.microsoft.com/en-us/library/ff648068.aspx', |
|
| 54 | - ); |
|
| 55 | - private $name; |
|
| 56 | - private $url; |
|
| 17 | + // @todo make this a separate resource file? (licenseUrls.json) |
|
| 18 | + private static $licenses = array( |
|
| 19 | + 'artistic-1.0' => 'http://opensource.org/licenses/artistic-license-1.0', |
|
| 20 | + 'artistic-1' => 'http://opensource.org/licenses/artistic-license-1.0', |
|
| 21 | + 'artistic-2.0' => 'http://opensource.org/licenses/artistic-license-2.0', |
|
| 22 | + 'artistic-2' => 'http://opensource.org/licenses/artistic-license-2.0', |
|
| 23 | + 'artistic' => 'http://opensource.org/licenses/artistic-license-2.0', |
|
| 24 | + 'bsd-new' => 'https://opensource.org/licenses/BSD-3-Clause', |
|
| 25 | + 'bsd-3' => 'https://opensource.org/licenses/BSD-3-Clause', |
|
| 26 | + 'bsd-2' => 'https://opensource.org/licenses/BSD-2-Clause', |
|
| 27 | + 'bsd' => 'https://opensource.org/licenses/BSD-2-Clause', |
|
| 28 | + 'epl-1.0' => 'http://www.eclipse.org/legal/epl-v10.html', |
|
| 29 | + 'epl-1' => 'http://www.eclipse.org/legal/epl-v10.html', |
|
| 30 | + 'epl' => 'http://www.eclipse.org/legal/epl-v10.html', |
|
| 31 | + 'apache-2.0' => 'http://www.apache.org/licenses/LICENSE-2.0.html', |
|
| 32 | + 'apache-2' => 'http://www.apache.org/licenses/LICENSE-2.0.html', |
|
| 33 | + 'apache' => 'http://www.apache.org/licenses/LICENSE-2.0.html', |
|
| 34 | + 'gpl-1.0' => 'https://www.gnu.org/licenses/gpl-1.0.html', |
|
| 35 | + 'gpl-1' => 'https://www.gnu.org/licenses/gpl-1.0.html', |
|
| 36 | + 'gpl-2.0' => 'https://www.gnu.org/licenses/gpl-2.0.html', |
|
| 37 | + 'gpl-2' => 'https://www.gnu.org/licenses/gpl-2.0.html', |
|
| 38 | + 'gpl-3.0' => 'http://www.gnu.org/licenses/gpl-3.0.html', |
|
| 39 | + 'gpl-3' => 'http://www.gnu.org/licenses/gpl-3.0.html', |
|
| 40 | + 'gpl' => 'http://www.gnu.org/licenses/gpl-3.0.html', |
|
| 41 | + 'lgpl-2.0' => 'http://www.gnu.org/licenses/lgpl-2.0.html', |
|
| 42 | + 'lgpl-2.1' => 'http://www.gnu.org/licenses/lgpl-2.1.html', |
|
| 43 | + 'lgpl-2' => 'http://www.gnu.org/licenses/lgpl-2.1.html', |
|
| 44 | + 'lgpl-3.0' => 'http://www.gnu.org/licenses/lgpl-3.0.html', |
|
| 45 | + 'lgpl-3' => 'http://www.gnu.org/licenses/lgpl-3.0.html', |
|
| 46 | + 'lgpl' => 'http://www.gnu.org/licenses/lgpl-3.0.html', |
|
| 47 | + 'mit' => 'http://opensource.org/licenses/MIT', |
|
| 48 | + 'mpl-1.1' => 'https://www.mozilla.org/en-US/MPL/1.1/', |
|
| 49 | + 'mpl-1' => 'https://www.mozilla.org/en-US/MPL/1.1/', |
|
| 50 | + 'mpl-2.0' => 'https://www.mozilla.org/en-US/MPL/', |
|
| 51 | + 'mpl-2' => 'https://www.mozilla.org/en-US/MPL/', |
|
| 52 | + 'mpl' => 'https://www.mozilla.org/en-US/MPL/', |
|
| 53 | + 'mspl' => 'https://msdn.microsoft.com/en-us/library/ff648068.aspx', |
|
| 54 | + ); |
|
| 55 | + private $name; |
|
| 56 | + private $url; |
|
| 57 | 57 | |
| 58 | - public function __construct(AbstractObject $parent, $name, $url = null) |
|
| 59 | - { |
|
| 60 | - parent::__construct($parent); |
|
| 58 | + public function __construct(AbstractObject $parent, $name, $url = null) |
|
| 59 | + { |
|
| 60 | + parent::__construct($parent); |
|
| 61 | 61 | |
| 62 | - $this->name = empty($name) ? null : $name; |
|
| 62 | + $this->name = empty($name) ? null : $name; |
|
| 63 | 63 | |
| 64 | - if (!empty($url)) { |
|
| 65 | - $this->url = $url; |
|
| 66 | - } elseif (!empty(self::$licenses[strtolower($name)])) { |
|
| 67 | - $this->url = self::$licenses[strtolower($name)]; |
|
| 68 | - } |
|
| 69 | - } |
|
| 64 | + if (!empty($url)) { |
|
| 65 | + $this->url = $url; |
|
| 66 | + } elseif (!empty(self::$licenses[strtolower($name)])) { |
|
| 67 | + $this->url = self::$licenses[strtolower($name)]; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param string $command |
|
| 73 | - * @param string $data |
|
| 74 | - * @return AbstractObject|boolean |
|
| 75 | - */ |
|
| 76 | - public function handleCommand($command, $data = null) |
|
| 77 | - { |
|
| 78 | - switch (strtolower($command)) { |
|
| 79 | - case 'name': |
|
| 80 | - $this->name = $data; |
|
| 81 | - if (empty($this->url) && !empty(self::$licenses[strtolower($data)])) { |
|
| 82 | - $this->url = self::$licenses[strtolower($data)]; |
|
| 83 | - } |
|
| 84 | - return $this; |
|
| 71 | + /** |
|
| 72 | + * @param string $command |
|
| 73 | + * @param string $data |
|
| 74 | + * @return AbstractObject|boolean |
|
| 75 | + */ |
|
| 76 | + public function handleCommand($command, $data = null) |
|
| 77 | + { |
|
| 78 | + switch (strtolower($command)) { |
|
| 79 | + case 'name': |
|
| 80 | + $this->name = $data; |
|
| 81 | + if (empty($this->url) && !empty(self::$licenses[strtolower($data)])) { |
|
| 82 | + $this->url = self::$licenses[strtolower($data)]; |
|
| 83 | + } |
|
| 84 | + return $this; |
|
| 85 | 85 | |
| 86 | - case 'url': |
|
| 87 | - $this->url = $data; |
|
| 88 | - return $this; |
|
| 89 | - } |
|
| 86 | + case 'url': |
|
| 87 | + $this->url = $data; |
|
| 88 | + return $this; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - return parent::handleCommand($command, $data); |
|
| 92 | - } |
|
| 91 | + return parent::handleCommand($command, $data); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - public function toArray() |
|
| 95 | - { |
|
| 96 | - return self::arrayFilterNull(array_merge(array( |
|
| 97 | - 'name' => $this->name, |
|
| 98 | - 'url' => $this->url, |
|
| 99 | - ), parent::toArray())); |
|
| 100 | - } |
|
| 94 | + public function toArray() |
|
| 95 | + { |
|
| 96 | + return self::arrayFilterNull(array_merge(array( |
|
| 97 | + 'name' => $this->name, |
|
| 98 | + 'url' => $this->url, |
|
| 99 | + ), parent::toArray())); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - public function __toString() |
|
| 103 | - { |
|
| 104 | - return __CLASS__ . " {$this->name}, {$this->url}"; |
|
| 105 | - } |
|
| 102 | + public function __toString() |
|
| 103 | + { |
|
| 104 | + return __CLASS__ . " {$this->name}, {$this->url}"; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | 107 | } |
@@ -13,5 +13,5 @@ |
||
| 13 | 13 | interface IParameter |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - public function getName(); |
|
| 16 | + public function getName(); |
|
| 17 | 17 | } |
@@ -15,48 +15,48 @@ |
||
| 15 | 15 | class ReferenceObjectType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - private $reference = null; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @throws Exception |
|
| 22 | - */ |
|
| 23 | - protected function parseDefinition($definition) |
|
| 24 | - { |
|
| 25 | - $definition = self::trim($definition); |
|
| 26 | - |
|
| 27 | - $match = array(); |
|
| 28 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 29 | - throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - $type = strtolower($match[1]); |
|
| 33 | - |
|
| 34 | - $reference = null; |
|
| 35 | - if ($type === 'refobject') { |
|
| 36 | - if (isset($match[2])) { |
|
| 37 | - $reference = $match[2]; |
|
| 38 | - } |
|
| 39 | - } else { |
|
| 40 | - $reference = $match[1]; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - if (empty($reference)) { |
|
| 44 | - throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $this->reference = $reference; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function toArray() |
|
| 51 | - { |
|
| 52 | - return self::arrayFilterNull(array_merge(array( |
|
| 53 | - '$ref' => '#/definitions/' . $this->reference, |
|
| 54 | - ), parent::toArray())); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function __toString() |
|
| 58 | - { |
|
| 59 | - return __CLASS__ . ' ' . $this->reference; |
|
| 60 | - } |
|
| 18 | + private $reference = null; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @throws Exception |
|
| 22 | + */ |
|
| 23 | + protected function parseDefinition($definition) |
|
| 24 | + { |
|
| 25 | + $definition = self::trim($definition); |
|
| 26 | + |
|
| 27 | + $match = array(); |
|
| 28 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 29 | + throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + $type = strtolower($match[1]); |
|
| 33 | + |
|
| 34 | + $reference = null; |
|
| 35 | + if ($type === 'refobject') { |
|
| 36 | + if (isset($match[2])) { |
|
| 37 | + $reference = $match[2]; |
|
| 38 | + } |
|
| 39 | + } else { |
|
| 40 | + $reference = $match[1]; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + if (empty($reference)) { |
|
| 44 | + throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $this->reference = $reference; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function toArray() |
|
| 51 | + { |
|
| 52 | + return self::arrayFilterNull(array_merge(array( |
|
| 53 | + '$ref' => '#/definitions/' . $this->reference, |
|
| 54 | + ), parent::toArray())); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function __toString() |
|
| 58 | + { |
|
| 59 | + return __CLASS__ . ' ' . $this->reference; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -14,54 +14,54 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class AllOfType extends AbstractType |
| 16 | 16 | { |
| 17 | - private $allOfItems = array(); |
|
| 18 | - private $mostRecentItem; |
|
| 17 | + private $allOfItems = array(); |
|
| 18 | + private $mostRecentItem; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @throws Exception |
|
| 22 | - */ |
|
| 23 | - protected function parseDefinition($definition) |
|
| 24 | - { |
|
| 25 | - $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
| 26 | - $inlineDef = ''; |
|
| 27 | - if (preg_match($pattern, $definition, $matches)) { |
|
| 28 | - if (isset($matches[1])) { |
|
| 29 | - $inlineDef = $matches[1]; |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - if ($inlineDef) { |
|
| 33 | - foreach ($this->parseList($inlineDef) as $item) { |
|
| 34 | - $this->handleCommand('item', $item); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - } |
|
| 20 | + /** |
|
| 21 | + * @throws Exception |
|
| 22 | + */ |
|
| 23 | + protected function parseDefinition($definition) |
|
| 24 | + { |
|
| 25 | + $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
| 26 | + $inlineDef = ''; |
|
| 27 | + if (preg_match($pattern, $definition, $matches)) { |
|
| 28 | + if (isset($matches[1])) { |
|
| 29 | + $inlineDef = $matches[1]; |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + if ($inlineDef) { |
|
| 33 | + foreach ($this->parseList($inlineDef) as $item) { |
|
| 34 | + $this->handleCommand('item', $item); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @throws Exception |
|
| 41 | - */ |
|
| 42 | - public function handleCommand($command, $data = null) |
|
| 43 | - { |
|
| 44 | - if (strtolower($command) === 'item') { |
|
| 45 | - $this->mostRecentItem = self::typeFactory($this, $data); |
|
| 46 | - $this->allOfItems[] = $this->mostRecentItem; |
|
| 47 | - return $this; |
|
| 48 | - } |
|
| 49 | - if (isset($this->mostRecentItem)) { |
|
| 50 | - if ($this->mostRecentItem->handleCommand($command, $data)) { |
|
| 51 | - return $this; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - return parent::handleCommand($command, $data); |
|
| 55 | - } |
|
| 39 | + /** |
|
| 40 | + * @throws Exception |
|
| 41 | + */ |
|
| 42 | + public function handleCommand($command, $data = null) |
|
| 43 | + { |
|
| 44 | + if (strtolower($command) === 'item') { |
|
| 45 | + $this->mostRecentItem = self::typeFactory($this, $data); |
|
| 46 | + $this->allOfItems[] = $this->mostRecentItem; |
|
| 47 | + return $this; |
|
| 48 | + } |
|
| 49 | + if (isset($this->mostRecentItem)) { |
|
| 50 | + if ($this->mostRecentItem->handleCommand($command, $data)) { |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + return parent::handleCommand($command, $data); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function toArray() |
|
| 58 | - { |
|
| 59 | - $allOf = array(); |
|
| 60 | - foreach ($this->allOfItems as $item) { |
|
| 61 | - $allOf[] = $item->toArray(); |
|
| 62 | - } |
|
| 63 | - return self::arrayFilterNull(array_merge(array( |
|
| 64 | - 'allOf' => $allOf, |
|
| 65 | - ), parent::toArray())); |
|
| 66 | - } |
|
| 57 | + public function toArray() |
|
| 58 | + { |
|
| 59 | + $allOf = array(); |
|
| 60 | + foreach ($this->allOfItems as $item) { |
|
| 61 | + $allOf[] = $item->toArray(); |
|
| 62 | + } |
|
| 63 | + return self::arrayFilterNull(array_merge(array( |
|
| 64 | + 'allOf' => $allOf, |
|
| 65 | + ), parent::toArray())); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -17,45 +17,45 @@ |
||
| 17 | 17 | class FileType extends AbstractType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @throws Exception |
|
| 22 | - */ |
|
| 23 | - protected function parseDefinition($definition) |
|
| 24 | - { |
|
| 25 | - $type = strtolower($definition); |
|
| 26 | - |
|
| 27 | - if ($type !== 'file') { |
|
| 28 | - throw new Exception("Not a file: '{$definition}'"); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - $parent = $this->getParent(); |
|
| 32 | - if (!($parent instanceof Parameter) || !$parent->isForm()) { |
|
| 33 | - throw new Exception("File type '{$definition}' only allowed on form parameter"); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** @var Operation $parentOperation */ |
|
| 37 | - $parentOperation = $this->getParentClass(Operation::class); |
|
| 38 | - $consumes = $parentOperation->getConsumes(); |
|
| 39 | - if (empty($consumes)) { |
|
| 40 | - $consumes = $this->getSwagger()->getConsumes(); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes)); |
|
| 44 | - if (empty($consumes) || $valid_consumes !== count($consumes)) { |
|
| 45 | - throw new Exception("File type '{$definition}' without valid consume"); |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function toArray() |
|
| 50 | - { |
|
| 51 | - return self::arrayFilterNull(array_merge(array( |
|
| 52 | - 'type' => 'file', |
|
| 53 | - ), parent::toArray())); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public function __toString() |
|
| 57 | - { |
|
| 58 | - return __CLASS__; |
|
| 59 | - } |
|
| 20 | + /** |
|
| 21 | + * @throws Exception |
|
| 22 | + */ |
|
| 23 | + protected function parseDefinition($definition) |
|
| 24 | + { |
|
| 25 | + $type = strtolower($definition); |
|
| 26 | + |
|
| 27 | + if ($type !== 'file') { |
|
| 28 | + throw new Exception("Not a file: '{$definition}'"); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + $parent = $this->getParent(); |
|
| 32 | + if (!($parent instanceof Parameter) || !$parent->isForm()) { |
|
| 33 | + throw new Exception("File type '{$definition}' only allowed on form parameter"); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** @var Operation $parentOperation */ |
|
| 37 | + $parentOperation = $this->getParentClass(Operation::class); |
|
| 38 | + $consumes = $parentOperation->getConsumes(); |
|
| 39 | + if (empty($consumes)) { |
|
| 40 | + $consumes = $this->getSwagger()->getConsumes(); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes)); |
|
| 44 | + if (empty($consumes) || $valid_consumes !== count($consumes)) { |
|
| 45 | + throw new Exception("File type '{$definition}' without valid consume"); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function toArray() |
|
| 50 | + { |
|
| 51 | + return self::arrayFilterNull(array_merge(array( |
|
| 52 | + 'type' => 'file', |
|
| 53 | + ), parent::toArray())); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public function __toString() |
|
| 57 | + { |
|
| 58 | + return __CLASS__; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | $consumes = $this->getSwagger()->getConsumes(); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - $valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes)); |
|
| 43 | + $valid_consumes = ((int) in_array('multipart/form-data', $consumes)) + ((int) in_array('application/x-www-form-urlencoded', $consumes)); |
|
| 44 | 44 | if (empty($consumes) || $valid_consumes !== count($consumes)) { |
| 45 | 45 | throw new Exception("File type '{$definition}' without valid consume"); |
| 46 | 46 | } |
@@ -16,97 +16,97 @@ |
||
| 16 | 16 | class DateType extends AbstractType |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - const REGEX_DEFAULT = '(?:=(\S+))?'; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Map of recognized format names to Swagger formats |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - private static $formats = array( |
|
| 26 | - 'date' => 'date', |
|
| 27 | - 'date-time' => 'date-time', |
|
| 28 | - 'datetime' => 'date-time', |
|
| 29 | - ); |
|
| 30 | - private static $datetime_formats = array( |
|
| 31 | - 'date' => 'Y-m-d', |
|
| 32 | - 'date-time' => DateTime::RFC3339, |
|
| 33 | - ); |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var String |
|
| 37 | - */ |
|
| 38 | - private $format; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var DateTime |
|
| 42 | - */ |
|
| 43 | - private $default = null; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @throws Exception |
|
| 47 | - */ |
|
| 48 | - protected function parseDefinition($definition) |
|
| 49 | - { |
|
| 50 | - $match = array(); |
|
| 51 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 52 | - throw new Exception("Unparseable date definition: '{$definition}'"); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $type = strtolower($match[1]); |
|
| 56 | - |
|
| 57 | - if (!isset(self::$formats[$type])) { |
|
| 58 | - throw new Exception("Not a date: '{$definition}'"); |
|
| 59 | - } |
|
| 60 | - $this->format = self::$formats[$type]; |
|
| 61 | - |
|
| 62 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param string $command The comment command |
|
| 67 | - * @param string $data Any data added after the command |
|
| 68 | - * @return AbstractType|boolean |
|
| 69 | - * @throws Exception |
|
| 70 | - * @throws Exception |
|
| 71 | - */ |
|
| 72 | - public function handleCommand($command, $data = null) |
|
| 73 | - { |
|
| 74 | - if (strtolower($command) === 'default') { |
|
| 75 | - $this->default = $this->validateDefault($data); |
|
| 76 | - return $this; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return parent::handleCommand($command, $data); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function toArray() |
|
| 83 | - { |
|
| 84 | - return self::arrayFilterNull(array_merge(array( |
|
| 85 | - 'type' => 'string', |
|
| 86 | - 'format' => $this->format, |
|
| 87 | - 'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null, |
|
| 88 | - ), parent::toArray())); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @throws Exception |
|
| 93 | - */ |
|
| 94 | - private function validateDefault($value) |
|
| 95 | - { |
|
| 96 | - if (empty($value)) { |
|
| 97 | - throw new Exception("Empty date default"); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - if (($DateTime = date_create($value)) !== false) { |
|
| 101 | - return $DateTime; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - throw new Exception("Invalid '{$this->format}' default: '{$value}'"); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - public function __toString() |
|
| 108 | - { |
|
| 109 | - return __CLASS__; |
|
| 110 | - } |
|
| 19 | + const REGEX_DEFAULT = '(?:=(\S+))?'; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Map of recognized format names to Swagger formats |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + private static $formats = array( |
|
| 26 | + 'date' => 'date', |
|
| 27 | + 'date-time' => 'date-time', |
|
| 28 | + 'datetime' => 'date-time', |
|
| 29 | + ); |
|
| 30 | + private static $datetime_formats = array( |
|
| 31 | + 'date' => 'Y-m-d', |
|
| 32 | + 'date-time' => DateTime::RFC3339, |
|
| 33 | + ); |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var String |
|
| 37 | + */ |
|
| 38 | + private $format; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var DateTime |
|
| 42 | + */ |
|
| 43 | + private $default = null; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @throws Exception |
|
| 47 | + */ |
|
| 48 | + protected function parseDefinition($definition) |
|
| 49 | + { |
|
| 50 | + $match = array(); |
|
| 51 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 52 | + throw new Exception("Unparseable date definition: '{$definition}'"); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $type = strtolower($match[1]); |
|
| 56 | + |
|
| 57 | + if (!isset(self::$formats[$type])) { |
|
| 58 | + throw new Exception("Not a date: '{$definition}'"); |
|
| 59 | + } |
|
| 60 | + $this->format = self::$formats[$type]; |
|
| 61 | + |
|
| 62 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param string $command The comment command |
|
| 67 | + * @param string $data Any data added after the command |
|
| 68 | + * @return AbstractType|boolean |
|
| 69 | + * @throws Exception |
|
| 70 | + * @throws Exception |
|
| 71 | + */ |
|
| 72 | + public function handleCommand($command, $data = null) |
|
| 73 | + { |
|
| 74 | + if (strtolower($command) === 'default') { |
|
| 75 | + $this->default = $this->validateDefault($data); |
|
| 76 | + return $this; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return parent::handleCommand($command, $data); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function toArray() |
|
| 83 | + { |
|
| 84 | + return self::arrayFilterNull(array_merge(array( |
|
| 85 | + 'type' => 'string', |
|
| 86 | + 'format' => $this->format, |
|
| 87 | + 'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null, |
|
| 88 | + ), parent::toArray())); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @throws Exception |
|
| 93 | + */ |
|
| 94 | + private function validateDefault($value) |
|
| 95 | + { |
|
| 96 | + if (empty($value)) { |
|
| 97 | + throw new Exception("Empty date default"); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + if (($DateTime = date_create($value)) !== false) { |
|
| 101 | + return $DateTime; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + throw new Exception("Invalid '{$this->format}' default: '{$value}'"); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + public function __toString() |
|
| 108 | + { |
|
| 109 | + return __CLASS__; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | } |
@@ -15,15 +15,15 @@ |
||
| 15 | 15 | class StringUuidType extends AbstractRegexType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Construct and set up the regular expression for this type |
|
| 20 | - * |
|
| 21 | - * @param AbstractObject $parent |
|
| 22 | - * @param string $definition |
|
| 23 | - */ |
|
| 24 | - public function __construct(AbstractObject $parent, $definition) |
|
| 25 | - { |
|
| 26 | - parent::__construct($parent, $definition, 'uuid', '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}'); |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * Construct and set up the regular expression for this type |
|
| 20 | + * |
|
| 21 | + * @param AbstractObject $parent |
|
| 22 | + * @param string $definition |
|
| 23 | + */ |
|
| 24 | + public function __construct(AbstractObject $parent, $definition) |
|
| 25 | + { |
|
| 26 | + parent::__construct($parent, $definition, 'uuid', '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}'); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
@@ -15,187 +15,187 @@ |
||
| 15 | 15 | class StringType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - private static $formats = array( |
|
| 19 | - 'string' => '', |
|
| 20 | - 'byte' => 'byte', |
|
| 21 | - 'binary' => 'binary', |
|
| 22 | - 'password' => 'password', |
|
| 23 | - 'enum' => '', |
|
| 24 | - ); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Name of the type |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $format = ''; |
|
| 31 | - protected $pattern = null; |
|
| 32 | - protected $default = null; |
|
| 33 | - protected $maxLength = null; |
|
| 34 | - protected $minLength = null; |
|
| 35 | - protected $enum = array(); |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @throws Exception |
|
| 39 | - */ |
|
| 40 | - protected function parseDefinition($definition) |
|
| 41 | - { |
|
| 42 | - $definition = self::trim($definition); |
|
| 43 | - |
|
| 44 | - $match = array(); |
|
| 45 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 46 | - throw new Exception("Unparseable string definition: '{$definition}'"); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $this->parseFormat($definition, $match); |
|
| 50 | - $this->parseContent($definition, $match); |
|
| 51 | - $this->parseRange($definition, $match); |
|
| 52 | - $this->parseDefault($definition, $match); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param string $definition |
|
| 57 | - * @param string[] $match |
|
| 58 | - * @throws Exception |
|
| 59 | - */ |
|
| 60 | - private function parseFormat($definition, $match) |
|
| 61 | - { |
|
| 62 | - $type = strtolower($match[1]); |
|
| 63 | - if (!isset(self::$formats[$type])) { |
|
| 64 | - throw new Exception("Not a string: '{$definition}'"); |
|
| 65 | - } |
|
| 66 | - $this->format = self::$formats[$type]; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param string $definition |
|
| 71 | - * @param string[] $match |
|
| 72 | - */ |
|
| 73 | - private function parseContent($definition, $match) |
|
| 74 | - { |
|
| 75 | - if (strtolower($match[1]) === 'enum') { |
|
| 76 | - $this->enum = explode(',', $match[2]); |
|
| 77 | - } else { |
|
| 78 | - $this->pattern = empty($match[2]) ? null : $match[2]; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param string $definition |
|
| 84 | - * @param string[] $match |
|
| 85 | - * @throws Exception |
|
| 86 | - * @throws Exception |
|
| 87 | - */ |
|
| 88 | - private function parseRange($definition, $match) |
|
| 89 | - { |
|
| 90 | - |
|
| 91 | - if (!empty($match[3])) { |
|
| 92 | - if ($match[1] === 'enum') { |
|
| 93 | - throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
| 94 | - } |
|
| 95 | - if ($match[4] === '' && $match[5] === '') { |
|
| 96 | - throw new Exception("Empty string range: '{$definition}'"); |
|
| 97 | - } |
|
| 98 | - $exclusiveMinimum = $match[3] == '<'; |
|
| 99 | - $this->minLength = $match[4] === '' ? null : $match[4]; |
|
| 100 | - $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
| 101 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
| 102 | - if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
| 103 | - self::swap($this->minLength, $this->maxLength); |
|
| 104 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
| 105 | - } |
|
| 106 | - $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
| 107 | - $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $definition |
|
| 113 | - * @param string[] $match |
|
| 114 | - * @throws Exception |
|
| 115 | - */ |
|
| 116 | - private function parseDefault($definition, $match) |
|
| 117 | - { |
|
| 118 | - $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param string $command The comment command |
|
| 123 | - * @param string $data Any data added after the command |
|
| 124 | - * @return AbstractType|boolean |
|
| 125 | - * @throws Exception |
|
| 126 | - * @throws Exception |
|
| 127 | - * @throws Exception |
|
| 128 | - */ |
|
| 129 | - public function handleCommand($command, $data = null) |
|
| 130 | - { |
|
| 131 | - switch (strtolower($command)) { |
|
| 132 | - case 'default': |
|
| 133 | - $this->default = $this->validateDefault($data); |
|
| 134 | - return $this; |
|
| 135 | - |
|
| 136 | - case 'pattern': |
|
| 137 | - $this->pattern = $data; |
|
| 138 | - return $this; |
|
| 139 | - |
|
| 140 | - case 'enum': |
|
| 141 | - if ($this->minLength !== null || $this->maxLength !== null) { |
|
| 142 | - throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
| 143 | - } |
|
| 144 | - $words = self::wordSplit($data); |
|
| 145 | - $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return parent::handleCommand($command, $data); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - public function toArray() |
|
| 153 | - { |
|
| 154 | - return self::arrayFilterNull(array_merge(array( |
|
| 155 | - 'type' => 'string', |
|
| 156 | - 'format' => empty($this->format) ? null : $this->format, |
|
| 157 | - 'pattern' => $this->pattern, |
|
| 158 | - 'default' => $this->default, |
|
| 159 | - 'minLength' => $this->minLength ? intval($this->minLength) : null, |
|
| 160 | - 'maxLength' => $this->maxLength ? intval($this->maxLength) : null, |
|
| 161 | - 'enum' => $this->enum, |
|
| 162 | - ), parent::toArray())); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Validate a default string value, depending on subtype |
|
| 167 | - * |
|
| 168 | - * @param string $value the value to validate |
|
| 169 | - * @return string the value after validation (might become trimmed) |
|
| 170 | - * @throws Exception |
|
| 171 | - */ |
|
| 172 | - protected function validateDefault($value) |
|
| 173 | - { |
|
| 174 | - if (empty($value)) { |
|
| 175 | - $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
|
| 176 | - throw new Exception("Empty {$type} default"); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if (!empty($this->enum) && !in_array($value, $this->enum)) { |
|
| 180 | - throw new Exception("Invalid enum default: '{$value}'"); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
| 184 | - $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
|
| 185 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
| 189 | - $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
|
| 190 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - return $value; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - public function __toString() |
|
| 197 | - { |
|
| 198 | - return __CLASS__; |
|
| 199 | - } |
|
| 18 | + private static $formats = array( |
|
| 19 | + 'string' => '', |
|
| 20 | + 'byte' => 'byte', |
|
| 21 | + 'binary' => 'binary', |
|
| 22 | + 'password' => 'password', |
|
| 23 | + 'enum' => '', |
|
| 24 | + ); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Name of the type |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $format = ''; |
|
| 31 | + protected $pattern = null; |
|
| 32 | + protected $default = null; |
|
| 33 | + protected $maxLength = null; |
|
| 34 | + protected $minLength = null; |
|
| 35 | + protected $enum = array(); |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @throws Exception |
|
| 39 | + */ |
|
| 40 | + protected function parseDefinition($definition) |
|
| 41 | + { |
|
| 42 | + $definition = self::trim($definition); |
|
| 43 | + |
|
| 44 | + $match = array(); |
|
| 45 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 46 | + throw new Exception("Unparseable string definition: '{$definition}'"); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $this->parseFormat($definition, $match); |
|
| 50 | + $this->parseContent($definition, $match); |
|
| 51 | + $this->parseRange($definition, $match); |
|
| 52 | + $this->parseDefault($definition, $match); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param string $definition |
|
| 57 | + * @param string[] $match |
|
| 58 | + * @throws Exception |
|
| 59 | + */ |
|
| 60 | + private function parseFormat($definition, $match) |
|
| 61 | + { |
|
| 62 | + $type = strtolower($match[1]); |
|
| 63 | + if (!isset(self::$formats[$type])) { |
|
| 64 | + throw new Exception("Not a string: '{$definition}'"); |
|
| 65 | + } |
|
| 66 | + $this->format = self::$formats[$type]; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param string $definition |
|
| 71 | + * @param string[] $match |
|
| 72 | + */ |
|
| 73 | + private function parseContent($definition, $match) |
|
| 74 | + { |
|
| 75 | + if (strtolower($match[1]) === 'enum') { |
|
| 76 | + $this->enum = explode(',', $match[2]); |
|
| 77 | + } else { |
|
| 78 | + $this->pattern = empty($match[2]) ? null : $match[2]; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param string $definition |
|
| 84 | + * @param string[] $match |
|
| 85 | + * @throws Exception |
|
| 86 | + * @throws Exception |
|
| 87 | + */ |
|
| 88 | + private function parseRange($definition, $match) |
|
| 89 | + { |
|
| 90 | + |
|
| 91 | + if (!empty($match[3])) { |
|
| 92 | + if ($match[1] === 'enum') { |
|
| 93 | + throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
| 94 | + } |
|
| 95 | + if ($match[4] === '' && $match[5] === '') { |
|
| 96 | + throw new Exception("Empty string range: '{$definition}'"); |
|
| 97 | + } |
|
| 98 | + $exclusiveMinimum = $match[3] == '<'; |
|
| 99 | + $this->minLength = $match[4] === '' ? null : $match[4]; |
|
| 100 | + $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
| 101 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
| 102 | + if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
| 103 | + self::swap($this->minLength, $this->maxLength); |
|
| 104 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
| 105 | + } |
|
| 106 | + $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
| 107 | + $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $definition |
|
| 113 | + * @param string[] $match |
|
| 114 | + * @throws Exception |
|
| 115 | + */ |
|
| 116 | + private function parseDefault($definition, $match) |
|
| 117 | + { |
|
| 118 | + $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param string $command The comment command |
|
| 123 | + * @param string $data Any data added after the command |
|
| 124 | + * @return AbstractType|boolean |
|
| 125 | + * @throws Exception |
|
| 126 | + * @throws Exception |
|
| 127 | + * @throws Exception |
|
| 128 | + */ |
|
| 129 | + public function handleCommand($command, $data = null) |
|
| 130 | + { |
|
| 131 | + switch (strtolower($command)) { |
|
| 132 | + case 'default': |
|
| 133 | + $this->default = $this->validateDefault($data); |
|
| 134 | + return $this; |
|
| 135 | + |
|
| 136 | + case 'pattern': |
|
| 137 | + $this->pattern = $data; |
|
| 138 | + return $this; |
|
| 139 | + |
|
| 140 | + case 'enum': |
|
| 141 | + if ($this->minLength !== null || $this->maxLength !== null) { |
|
| 142 | + throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
| 143 | + } |
|
| 144 | + $words = self::wordSplit($data); |
|
| 145 | + $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return parent::handleCommand($command, $data); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + public function toArray() |
|
| 153 | + { |
|
| 154 | + return self::arrayFilterNull(array_merge(array( |
|
| 155 | + 'type' => 'string', |
|
| 156 | + 'format' => empty($this->format) ? null : $this->format, |
|
| 157 | + 'pattern' => $this->pattern, |
|
| 158 | + 'default' => $this->default, |
|
| 159 | + 'minLength' => $this->minLength ? intval($this->minLength) : null, |
|
| 160 | + 'maxLength' => $this->maxLength ? intval($this->maxLength) : null, |
|
| 161 | + 'enum' => $this->enum, |
|
| 162 | + ), parent::toArray())); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Validate a default string value, depending on subtype |
|
| 167 | + * |
|
| 168 | + * @param string $value the value to validate |
|
| 169 | + * @return string the value after validation (might become trimmed) |
|
| 170 | + * @throws Exception |
|
| 171 | + */ |
|
| 172 | + protected function validateDefault($value) |
|
| 173 | + { |
|
| 174 | + if (empty($value)) { |
|
| 175 | + $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
|
| 176 | + throw new Exception("Empty {$type} default"); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if (!empty($this->enum) && !in_array($value, $this->enum)) { |
|
| 180 | + throw new Exception("Invalid enum default: '{$value}'"); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
| 184 | + $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
|
| 185 | + throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
| 189 | + $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
|
| 190 | + throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + return $value; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + public function __toString() |
|
| 197 | + { |
|
| 198 | + return __CLASS__; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | 201 | } |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | { |
| 174 | 174 | if (empty($value)) { |
| 175 | 175 | $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
| 176 | - throw new Exception("Empty {$type} default"); |
|
| 176 | + throw new Exception("empty {$type} default"); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | if (!empty($this->enum) && !in_array($value, $this->enum)) { |
@@ -182,12 +182,12 @@ discard block |
||
| 182 | 182 | |
| 183 | 183 | if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
| 184 | 184 | $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
| 185 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
| 185 | + throw new Exception("default {$type} length beyond maximum: '{$value}'"); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
| 189 | 189 | $type = $this->format ?: ($this->enum ? 'enum' : 'string'); |
| 190 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
| 190 | + throw new Exception("default {$type} length beyond minimum: '{$value}'"); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | return $value; |