@@ -14,38 +14,38 @@ |
||
| 14 | 14 | abstract class AbstractDocumentableObject extends AbstractObject |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * External documentation |
|
| 19 | - * @var ExternalDocumentation |
|
| 20 | - */ |
|
| 21 | - private $externalDocs = null; |
|
| 17 | + /** |
|
| 18 | + * External documentation |
|
| 19 | + * @var ExternalDocumentation |
|
| 20 | + */ |
|
| 21 | + private $externalDocs = null; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param string $command |
|
| 25 | - * @param string $data |
|
| 26 | - * @return AbstractObject|boolean |
|
| 27 | - */ |
|
| 28 | - public function handleCommand($command, $data = null) |
|
| 29 | - { |
|
| 30 | - switch (strtolower($command)) { |
|
| 31 | - case 'doc': |
|
| 32 | - case 'docs': |
|
| 33 | - $url = self::wordShift($data); |
|
| 34 | - $this->externalDocs = new ExternalDocumentation($this, $url, $data); |
|
| 35 | - return $this->externalDocs; |
|
| 36 | - } |
|
| 23 | + /** |
|
| 24 | + * @param string $command |
|
| 25 | + * @param string $data |
|
| 26 | + * @return AbstractObject|boolean |
|
| 27 | + */ |
|
| 28 | + public function handleCommand($command, $data = null) |
|
| 29 | + { |
|
| 30 | + switch (strtolower($command)) { |
|
| 31 | + case 'doc': |
|
| 32 | + case 'docs': |
|
| 33 | + $url = self::wordShift($data); |
|
| 34 | + $this->externalDocs = new ExternalDocumentation($this, $url, $data); |
|
| 35 | + return $this->externalDocs; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - return parent::handleCommand($command, $data); |
|
| 39 | - } |
|
| 38 | + return parent::handleCommand($command, $data); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - public function toArray(): array |
|
| 45 | - { |
|
| 46 | - return self::arrayFilterNull(array_merge(array( |
|
| 47 | - 'externalDocs' => $this->externalDocs ? $this->externalDocs->toArray() : null, |
|
| 48 | - ), parent::toArray())); |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + public function toArray(): array |
|
| 45 | + { |
|
| 46 | + return self::arrayFilterNull(array_merge(array( |
|
| 47 | + 'externalDocs' => $this->externalDocs ? $this->externalDocs->toArray() : null, |
|
| 48 | + ), parent::toArray())); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -13,25 +13,25 @@ |
||
| 13 | 13 | class ResponseReference extends AbstractObject |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - private $reference; |
|
| 17 | - |
|
| 18 | - public function __construct(AbstractObject $parent, $reference) |
|
| 19 | - { |
|
| 20 | - parent::__construct($parent); |
|
| 21 | - |
|
| 22 | - $this->reference = $reference; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function toArray(): array |
|
| 26 | - { |
|
| 27 | - return self::arrayFilterNull(array( |
|
| 28 | - '$ref' => '#/responses/' . $this->reference, |
|
| 29 | - )); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public function __toString() |
|
| 33 | - { |
|
| 34 | - return __CLASS__ . ' `' . $this->reference . '`'; |
|
| 35 | - } |
|
| 16 | + private $reference; |
|
| 17 | + |
|
| 18 | + public function __construct(AbstractObject $parent, $reference) |
|
| 19 | + { |
|
| 20 | + parent::__construct($parent); |
|
| 21 | + |
|
| 22 | + $this->reference = $reference; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function toArray(): array |
|
| 26 | + { |
|
| 27 | + return self::arrayFilterNull(array( |
|
| 28 | + '$ref' => '#/responses/' . $this->reference, |
|
| 29 | + )); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public function __toString() |
|
| 33 | + { |
|
| 34 | + return __CLASS__ . ' `' . $this->reference . '`'; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | } |
@@ -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(): array |
|
| 95 | - { |
|
| 96 | - return self::arrayFilterNull(array_merge(array( |
|
| 97 | - 'name' => $this->name, |
|
| 98 | - 'url' => $this->url, |
|
| 99 | - ), parent::toArray())); |
|
| 100 | - } |
|
| 94 | + public function toArray(): array |
|
| 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,30 +13,30 @@ |
||
| 13 | 13 | class ParameterReference extends AbstractObject implements IParameter |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - private $reference; |
|
| 17 | - |
|
| 18 | - public function __construct(AbstractObject $parent, $reference) |
|
| 19 | - { |
|
| 20 | - parent::__construct($parent); |
|
| 21 | - |
|
| 22 | - $this->reference = $reference; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function toArray(): array |
|
| 26 | - { |
|
| 27 | - return self::arrayFilterNull(array( |
|
| 28 | - '$ref' => '#/parameters/' . $this->reference, |
|
| 29 | - )); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public function __toString() |
|
| 33 | - { |
|
| 34 | - return __CLASS__ . ' `' . $this->reference . '`'; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function getName() |
|
| 38 | - { |
|
| 39 | - return $this->reference; |
|
| 40 | - } |
|
| 16 | + private $reference; |
|
| 17 | + |
|
| 18 | + public function __construct(AbstractObject $parent, $reference) |
|
| 19 | + { |
|
| 20 | + parent::__construct($parent); |
|
| 21 | + |
|
| 22 | + $this->reference = $reference; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function toArray(): array |
|
| 26 | + { |
|
| 27 | + return self::arrayFilterNull(array( |
|
| 28 | + '$ref' => '#/parameters/' . $this->reference, |
|
| 29 | + )); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public function __toString() |
|
| 33 | + { |
|
| 34 | + return __CLASS__ . ' `' . $this->reference . '`'; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function getName() |
|
| 38 | + { |
|
| 39 | + return $this->reference; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | } |
@@ -14,49 +14,49 @@ |
||
| 14 | 14 | class Contact extends AbstractObject |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - private $name; |
|
| 18 | - private $url; |
|
| 19 | - private $email; |
|
| 20 | - |
|
| 21 | - public function __construct(AbstractObject $parent, $name = null, $url = null, $email = null) |
|
| 22 | - { |
|
| 23 | - parent::__construct($parent); |
|
| 24 | - |
|
| 25 | - $this->name = empty($name) ? null : $name; |
|
| 26 | - $this->url = empty($url) ? null : $url; |
|
| 27 | - $this->email = empty($email) ? null : $email; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param string $command |
|
| 32 | - * @param string $data |
|
| 33 | - * @return AbstractObject|boolean |
|
| 34 | - */ |
|
| 35 | - public function handleCommand($command, $data = null) |
|
| 36 | - { |
|
| 37 | - switch (strtolower($command)) { |
|
| 38 | - case 'name': |
|
| 39 | - case 'url': |
|
| 40 | - case 'email': |
|
| 41 | - $this->$command = $data; |
|
| 42 | - return $this; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return parent::handleCommand($command, $data); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function toArray(): array |
|
| 49 | - { |
|
| 50 | - return self::arrayFilterNull(array_merge(array( |
|
| 51 | - 'name' => $this->name, |
|
| 52 | - 'url' => $this->url, |
|
| 53 | - 'email' => $this->email, |
|
| 54 | - ), parent::toArray())); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function __toString() |
|
| 58 | - { |
|
| 59 | - return __CLASS__ . " {$this->name} <{$this->email}>, {$this->url}"; |
|
| 60 | - } |
|
| 17 | + private $name; |
|
| 18 | + private $url; |
|
| 19 | + private $email; |
|
| 20 | + |
|
| 21 | + public function __construct(AbstractObject $parent, $name = null, $url = null, $email = null) |
|
| 22 | + { |
|
| 23 | + parent::__construct($parent); |
|
| 24 | + |
|
| 25 | + $this->name = empty($name) ? null : $name; |
|
| 26 | + $this->url = empty($url) ? null : $url; |
|
| 27 | + $this->email = empty($email) ? null : $email; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param string $command |
|
| 32 | + * @param string $data |
|
| 33 | + * @return AbstractObject|boolean |
|
| 34 | + */ |
|
| 35 | + public function handleCommand($command, $data = null) |
|
| 36 | + { |
|
| 37 | + switch (strtolower($command)) { |
|
| 38 | + case 'name': |
|
| 39 | + case 'url': |
|
| 40 | + case 'email': |
|
| 41 | + $this->$command = $data; |
|
| 42 | + return $this; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return parent::handleCommand($command, $data); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function toArray(): array |
|
| 49 | + { |
|
| 50 | + return self::arrayFilterNull(array_merge(array( |
|
| 51 | + 'name' => $this->name, |
|
| 52 | + 'url' => $this->url, |
|
| 53 | + 'email' => $this->email, |
|
| 54 | + ), parent::toArray())); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function __toString() |
|
| 58 | + { |
|
| 59 | + return __CLASS__ . " {$this->name} <{$this->email}>, {$this->url}"; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -15,147 +15,147 @@ |
||
| 15 | 15 | class Parameter extends AbstractObject implements IParameter |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - private static $classTypes = array( |
|
| 19 | - 'integer' => 'Integer', |
|
| 20 | - 'int' => 'Integer', |
|
| 21 | - 'int32' => 'Integer', |
|
| 22 | - 'int64' => 'Integer', |
|
| 23 | - 'long' => 'Integer', |
|
| 24 | - 'float' => 'Number', |
|
| 25 | - 'double' => 'Number', |
|
| 26 | - 'string' => 'String', |
|
| 27 | - 'byte' => 'String', |
|
| 28 | - 'binary' => 'String', |
|
| 29 | - 'password' => 'String', |
|
| 30 | - 'enum' => 'String', |
|
| 31 | - 'uuid' => 'StringUuid', |
|
| 32 | - 'boolean' => 'Boolean', |
|
| 33 | - 'bool' => 'Boolean', |
|
| 34 | - 'array' => 'Array', |
|
| 35 | - 'csv' => 'Array', |
|
| 36 | - 'ssv' => 'Array', |
|
| 37 | - 'tsv' => 'Array', |
|
| 38 | - 'pipes' => 'Array', |
|
| 39 | - 'multi' => 'Array', |
|
| 40 | - 'date' => 'Date', |
|
| 41 | - 'datetime' => 'Date', |
|
| 42 | - 'date-time' => 'Date', |
|
| 43 | - 'file' => 'File', |
|
| 44 | - ); |
|
| 45 | - private $name = ''; |
|
| 46 | - private $in; |
|
| 47 | - private $description; |
|
| 48 | - private $required = false; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var Type\AbstractType |
|
| 52 | - */ |
|
| 53 | - private $Type; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Returns true if the "multi" array collectionFormat is allowed for this |
|
| 57 | - * parameter. |
|
| 58 | - * @return bool |
|
| 59 | - */ |
|
| 60 | - public function isMulti(): bool |
|
| 61 | - { |
|
| 62 | - return in_array($this->in, array('query', 'form')); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Return true if the parameter is of type 'formData' |
|
| 67 | - * @return bool |
|
| 68 | - */ |
|
| 69 | - public function isForm(): bool |
|
| 70 | - { |
|
| 71 | - return $this->in === 'form'; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @throws Exception |
|
| 76 | - */ |
|
| 77 | - public function __construct(AbstractObject $parent, $in, $data, $required = false) |
|
| 78 | - { |
|
| 79 | - parent::__construct($parent); |
|
| 80 | - |
|
| 81 | - if ($in !== 'path' && $in !== 'form' && $in !== 'query' && $in !== 'header') { |
|
| 82 | - throw new Exception("Invalid in for parameter: '{$in}'"); |
|
| 83 | - } |
|
| 84 | - $this->in = $in; |
|
| 85 | - |
|
| 86 | - $definition = self::wordShift($data); |
|
| 87 | - if (empty($definition)) { |
|
| 88 | - throw new Exception('No type definition for parameter'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $name = self::wordShift($data); |
|
| 92 | - if (empty($name)) { |
|
| 93 | - throw new Exception('No name for parameter'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $this->name = $name; |
|
| 97 | - |
|
| 98 | - $this->description = $data; |
|
| 99 | - $this->required = (bool)$required; |
|
| 100 | - |
|
| 101 | - // Parse regex |
|
| 102 | - $match = array(); |
|
| 103 | - if (preg_match('/^([a-z][a-z0-9]*)/i', $definition, $match) === 1) { |
|
| 104 | - $format = strtolower($match[1]); |
|
| 105 | - } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
| 106 | - $format = 'array'; |
|
| 107 | - } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
| 108 | - $format = 'object'; |
|
| 109 | - } else { |
|
| 110 | - throw new Exception('Unparseable parameter format definition: \'' . $definition . '\''); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if ($this->getTypeRegistry()->has($format)) { |
|
| 114 | - $class = $this->getTypeRegistry()->get($format); |
|
| 115 | - $this->Type = new $class($this, $definition); |
|
| 116 | - } elseif (isset(self::$classTypes[$format])) { |
|
| 117 | - $type = self::$classTypes[$format]; |
|
| 118 | - $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
| 119 | - $this->Type = new $class($this, $definition); |
|
| 120 | - } else { |
|
| 121 | - throw new Exception("Type format not recognized: '{$format}'"); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param string $command |
|
| 127 | - * @param string $data |
|
| 128 | - * @return AbstractObject|boolean |
|
| 129 | - * @throws Exception |
|
| 130 | - */ |
|
| 131 | - public function handleCommand($command, $data = null) |
|
| 132 | - { |
|
| 133 | - // Pass through to Type |
|
| 134 | - if ($this->Type && $this->Type->handleCommand($command, $data)) { |
|
| 135 | - return $this; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - return parent::handleCommand($command, $data); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function toArray(): array |
|
| 142 | - { |
|
| 143 | - return self::arrayFilterNull(array_merge(array( |
|
| 144 | - 'name' => $this->name, |
|
| 145 | - 'in' => $this->in === 'form' ? 'formData' : $this->in, |
|
| 146 | - 'description' => empty($this->description) ? null : $this->description, |
|
| 147 | - 'required' => $this->in === 'path' || $this->required ? true : null, |
|
| 148 | - ), $this->Type->toArray(), parent::toArray())); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function __toString() |
|
| 152 | - { |
|
| 153 | - return __CLASS__ . " {$this->name} {$this->in}"; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - public function getName() |
|
| 157 | - { |
|
| 158 | - return $this->name; |
|
| 159 | - } |
|
| 18 | + private static $classTypes = array( |
|
| 19 | + 'integer' => 'Integer', |
|
| 20 | + 'int' => 'Integer', |
|
| 21 | + 'int32' => 'Integer', |
|
| 22 | + 'int64' => 'Integer', |
|
| 23 | + 'long' => 'Integer', |
|
| 24 | + 'float' => 'Number', |
|
| 25 | + 'double' => 'Number', |
|
| 26 | + 'string' => 'String', |
|
| 27 | + 'byte' => 'String', |
|
| 28 | + 'binary' => 'String', |
|
| 29 | + 'password' => 'String', |
|
| 30 | + 'enum' => 'String', |
|
| 31 | + 'uuid' => 'StringUuid', |
|
| 32 | + 'boolean' => 'Boolean', |
|
| 33 | + 'bool' => 'Boolean', |
|
| 34 | + 'array' => 'Array', |
|
| 35 | + 'csv' => 'Array', |
|
| 36 | + 'ssv' => 'Array', |
|
| 37 | + 'tsv' => 'Array', |
|
| 38 | + 'pipes' => 'Array', |
|
| 39 | + 'multi' => 'Array', |
|
| 40 | + 'date' => 'Date', |
|
| 41 | + 'datetime' => 'Date', |
|
| 42 | + 'date-time' => 'Date', |
|
| 43 | + 'file' => 'File', |
|
| 44 | + ); |
|
| 45 | + private $name = ''; |
|
| 46 | + private $in; |
|
| 47 | + private $description; |
|
| 48 | + private $required = false; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var Type\AbstractType |
|
| 52 | + */ |
|
| 53 | + private $Type; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Returns true if the "multi" array collectionFormat is allowed for this |
|
| 57 | + * parameter. |
|
| 58 | + * @return bool |
|
| 59 | + */ |
|
| 60 | + public function isMulti(): bool |
|
| 61 | + { |
|
| 62 | + return in_array($this->in, array('query', 'form')); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Return true if the parameter is of type 'formData' |
|
| 67 | + * @return bool |
|
| 68 | + */ |
|
| 69 | + public function isForm(): bool |
|
| 70 | + { |
|
| 71 | + return $this->in === 'form'; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @throws Exception |
|
| 76 | + */ |
|
| 77 | + public function __construct(AbstractObject $parent, $in, $data, $required = false) |
|
| 78 | + { |
|
| 79 | + parent::__construct($parent); |
|
| 80 | + |
|
| 81 | + if ($in !== 'path' && $in !== 'form' && $in !== 'query' && $in !== 'header') { |
|
| 82 | + throw new Exception("Invalid in for parameter: '{$in}'"); |
|
| 83 | + } |
|
| 84 | + $this->in = $in; |
|
| 85 | + |
|
| 86 | + $definition = self::wordShift($data); |
|
| 87 | + if (empty($definition)) { |
|
| 88 | + throw new Exception('No type definition for parameter'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $name = self::wordShift($data); |
|
| 92 | + if (empty($name)) { |
|
| 93 | + throw new Exception('No name for parameter'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $this->name = $name; |
|
| 97 | + |
|
| 98 | + $this->description = $data; |
|
| 99 | + $this->required = (bool)$required; |
|
| 100 | + |
|
| 101 | + // Parse regex |
|
| 102 | + $match = array(); |
|
| 103 | + if (preg_match('/^([a-z][a-z0-9]*)/i', $definition, $match) === 1) { |
|
| 104 | + $format = strtolower($match[1]); |
|
| 105 | + } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
| 106 | + $format = 'array'; |
|
| 107 | + } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
| 108 | + $format = 'object'; |
|
| 109 | + } else { |
|
| 110 | + throw new Exception('Unparseable parameter format definition: \'' . $definition . '\''); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if ($this->getTypeRegistry()->has($format)) { |
|
| 114 | + $class = $this->getTypeRegistry()->get($format); |
|
| 115 | + $this->Type = new $class($this, $definition); |
|
| 116 | + } elseif (isset(self::$classTypes[$format])) { |
|
| 117 | + $type = self::$classTypes[$format]; |
|
| 118 | + $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
| 119 | + $this->Type = new $class($this, $definition); |
|
| 120 | + } else { |
|
| 121 | + throw new Exception("Type format not recognized: '{$format}'"); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param string $command |
|
| 127 | + * @param string $data |
|
| 128 | + * @return AbstractObject|boolean |
|
| 129 | + * @throws Exception |
|
| 130 | + */ |
|
| 131 | + public function handleCommand($command, $data = null) |
|
| 132 | + { |
|
| 133 | + // Pass through to Type |
|
| 134 | + if ($this->Type && $this->Type->handleCommand($command, $data)) { |
|
| 135 | + return $this; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + return parent::handleCommand($command, $data); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function toArray(): array |
|
| 142 | + { |
|
| 143 | + return self::arrayFilterNull(array_merge(array( |
|
| 144 | + 'name' => $this->name, |
|
| 145 | + 'in' => $this->in === 'form' ? 'formData' : $this->in, |
|
| 146 | + 'description' => empty($this->description) ? null : $this->description, |
|
| 147 | + 'required' => $this->in === 'path' || $this->required ? true : null, |
|
| 148 | + ), $this->Type->toArray(), parent::toArray())); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function __toString() |
|
| 152 | + { |
|
| 153 | + return __CLASS__ . " {$this->name} {$this->in}"; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + public function getName() |
|
| 157 | + { |
|
| 158 | + return $this->name; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | 161 | } |
@@ -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(): array |
|
| 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(): array |
|
| 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 | } |
@@ -16,379 +16,379 @@ |
||
| 16 | 16 | class Swagger extends AbstractDocumentableObject |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private $swagger = '2.0'; |
|
| 20 | - private $host; |
|
| 21 | - private $basePath; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var TypeRegistry |
|
| 25 | - */ |
|
| 26 | - private $typeRegistry; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var Info $Info |
|
| 30 | - */ |
|
| 31 | - private $info; |
|
| 32 | - private $schemes = []; |
|
| 33 | - private $consumes = []; |
|
| 34 | - private $produces = []; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var Path[] $Paths |
|
| 38 | - */ |
|
| 39 | - private $paths = []; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Schema[] $definitions |
|
| 43 | - */ |
|
| 44 | - private $definitions = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var IParameter[] $parameters |
|
| 48 | - */ |
|
| 49 | - private $parameters = []; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var Response[] $responses |
|
| 53 | - */ |
|
| 54 | - private $responses = []; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var Tag[] $Tags |
|
| 58 | - */ |
|
| 59 | - private $tags = []; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Default tag for new endpoints/operations. Set by the api command. |
|
| 63 | - * |
|
| 64 | - * @var Tag |
|
| 65 | - */ |
|
| 66 | - private $defaultTag = null; |
|
| 67 | - private $securityDefinitions = []; |
|
| 68 | - private $security = []; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param string $host |
|
| 72 | - * @param string $basePath |
|
| 73 | - * @param TypeRegistry $typeRegistry |
|
| 74 | - */ |
|
| 75 | - public function __construct($host = null, $basePath = null, $typeRegistry = null) |
|
| 76 | - { |
|
| 77 | - parent::__construct(); |
|
| 78 | - |
|
| 79 | - $this->host = $host; |
|
| 80 | - $this->basePath = $basePath; |
|
| 81 | - |
|
| 82 | - $this->info = new Info($this); |
|
| 83 | - |
|
| 84 | - $this->typeRegistry = $typeRegistry ?: new TypeRegistry; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @inheritDoc |
|
| 89 | - */ |
|
| 90 | - protected function getSwagger(): Swagger |
|
| 91 | - { |
|
| 92 | - return $this; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @inheritDoc |
|
| 97 | - */ |
|
| 98 | - protected function getTypeRegistry(): TypeRegistry |
|
| 99 | - { |
|
| 100 | - return $this->typeRegistry; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Return all consumes |
|
| 105 | - * |
|
| 106 | - * @return array |
|
| 107 | - * @todo Deprecate in favour of a getConsume($name); |
|
| 108 | - */ |
|
| 109 | - public function getConsumes(): array |
|
| 110 | - { |
|
| 111 | - return $this->consumes; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Return the named security if it exists, otherwise return FALSE |
|
| 116 | - * |
|
| 117 | - * @param string $name |
|
| 118 | - * |
|
| 119 | - * @return boolean|SecurityScheme |
|
| 120 | - */ |
|
| 121 | - public function getSecurity($name) |
|
| 122 | - { |
|
| 123 | - return $this->securityDefinitions[$name] ?? false; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $command |
|
| 128 | - * @param string $data |
|
| 129 | - * |
|
| 130 | - * @return AbstractObject|boolean |
|
| 131 | - * @throws Exception |
|
| 132 | - * @throws Exception |
|
| 133 | - * @throws Exception |
|
| 134 | - * @throws Exception |
|
| 135 | - * @throws Exception |
|
| 136 | - * @throws Exception |
|
| 137 | - * @throws Exception |
|
| 138 | - * @throws Exception |
|
| 139 | - * @throws Exception |
|
| 140 | - */ |
|
| 141 | - public function handleCommand($command, $data = null) |
|
| 142 | - { |
|
| 143 | - switch (strtolower($command)) { |
|
| 144 | - // pass to Info |
|
| 145 | - case 'title': |
|
| 146 | - case 'description': |
|
| 147 | - case 'version': |
|
| 148 | - case 'terms': // alias |
|
| 149 | - case 'tos': // alias |
|
| 150 | - case 'termsofservice': |
|
| 151 | - case 'contact': |
|
| 152 | - case 'license': |
|
| 153 | - return $this->info->handleCommand($command, $data); |
|
| 154 | - |
|
| 155 | - // string[] |
|
| 156 | - case 'scheme': |
|
| 157 | - case 'schemes': |
|
| 158 | - $this->schemes = array_unique(array_merge($this->schemes, self::wordSplit($data))); |
|
| 159 | - |
|
| 160 | - return $this; |
|
| 161 | - |
|
| 162 | - // MIME[] |
|
| 163 | - case 'consume': |
|
| 164 | - case 'consumes': |
|
| 165 | - $this->consumes = array_merge($this->consumes, self::translateMimeTypes(self::wordSplit($data))); |
|
| 166 | - |
|
| 167 | - return $this; |
|
| 168 | - |
|
| 169 | - case 'produce': |
|
| 170 | - case 'produces': |
|
| 171 | - $this->produces = array_merge($this->produces, self::translateMimeTypes(self::wordSplit($data))); |
|
| 172 | - |
|
| 173 | - return $this; |
|
| 174 | - |
|
| 175 | - case 'model': |
|
| 176 | - case 'model!': |
|
| 177 | - case 'definition': |
|
| 178 | - case 'definition!': |
|
| 179 | - $name = self::wordShift($data); |
|
| 180 | - if (empty($name)) { |
|
| 181 | - throw new Exception('Missing definition name'); |
|
| 182 | - } |
|
| 183 | - $typeDef = self::wordShift($data); |
|
| 184 | - if (empty($typeDef)) { |
|
| 185 | - $typeDef = 'object'; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $definition = new Schema($this, $typeDef); |
|
| 189 | - if (substr($command, -1) === '!') { |
|
| 190 | - $definition->setReadOnly(); |
|
| 191 | - } |
|
| 192 | - $this->definitions[$name] = $definition; |
|
| 193 | - |
|
| 194 | - return $definition; |
|
| 195 | - |
|
| 196 | - case 'path': |
|
| 197 | - case 'query': |
|
| 198 | - case 'query?': |
|
| 199 | - case 'header': |
|
| 200 | - case 'header?': |
|
| 201 | - case 'form': |
|
| 202 | - case 'form?': |
|
| 203 | - $in = rtrim($command, '?'); |
|
| 204 | - $Parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?'); |
|
| 205 | - $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 206 | - |
|
| 207 | - return $Parameter; |
|
| 208 | - |
|
| 209 | - case 'body': |
|
| 210 | - case 'body?': |
|
| 211 | - $Parameter = new BodyParameter($this, $data, substr($command, -1) !== '?'); |
|
| 212 | - $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 213 | - |
|
| 214 | - return $Parameter; |
|
| 215 | - |
|
| 216 | - case 'response': |
|
| 217 | - $name = self::wordShift($data); |
|
| 218 | - $definition = self::wordShift($data); |
|
| 219 | - $description = $data; |
|
| 220 | - if (empty($description)) { |
|
| 221 | - throw new Exception('Response definition missing description'); |
|
| 222 | - } |
|
| 223 | - $Response = new Response($this, $name, $definition === 'null' ? null : $definition, $description); |
|
| 224 | - $this->responses[$name] = $Response; |
|
| 225 | - |
|
| 226 | - return $Response; |
|
| 227 | - |
|
| 228 | - case 'api': // alias |
|
| 229 | - case 'tag': |
|
| 230 | - $tagname = self::wordShift($data); |
|
| 231 | - if (empty($tagname)) { |
|
| 232 | - throw new Exception('Missing tag name'); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $Tag = null; |
|
| 236 | - foreach ($this->tags as $T) { |
|
| 237 | - if ($T->getName() === $tagname) { |
|
| 238 | - $Tag = $T; |
|
| 239 | - break; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - if (!$Tag) { |
|
| 243 | - $Tag = new Tag($this, $tagname, $data); |
|
| 244 | - $this->tags[] = $Tag; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - // backwards compatibility |
|
| 248 | - if ($command === 'api') { |
|
| 249 | - $this->defaultTag = $Tag; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $Tag; |
|
| 253 | - |
|
| 254 | - case 'endpoint': |
|
| 255 | - $path = self::wordShift($data); |
|
| 256 | - if (false === $path) { |
|
| 257 | - $path = '/'; |
|
| 258 | - } else if ($path[0] !== '/') { |
|
| 259 | - $path = '/' . $path; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - $Tag = null; |
|
| 263 | - if (($tagname = self::wordShift($data)) !== false) { |
|
| 264 | - foreach ($this->tags as $T) { |
|
| 265 | - if (strtolower($T->getName()) === strtolower($tagname)) { |
|
| 266 | - $Tag = $T; |
|
| 267 | - break; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - if (!$Tag) { |
|
| 271 | - $Tag = new Tag($this, $tagname, $data); |
|
| 272 | - $this->tags[] = $Tag; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - if (!isset($this->paths[$path])) { |
|
| 277 | - $this->paths[$path] = new Path($this, $Tag ?: $this->defaultTag); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return $this->paths[$path]; |
|
| 281 | - |
|
| 282 | - case 'security': |
|
| 283 | - $name = self::wordShift($data); |
|
| 284 | - if (empty($name)) { |
|
| 285 | - throw new Exception('Missing security name'); |
|
| 286 | - } |
|
| 287 | - $type = self::wordShift($data); |
|
| 288 | - if (empty($type)) { |
|
| 289 | - throw new Exception('Missing security type'); |
|
| 290 | - } |
|
| 291 | - $SecurityScheme = new SecurityScheme($this, $type, $data); |
|
| 292 | - $this->securityDefinitions[$name] = $SecurityScheme; |
|
| 293 | - |
|
| 294 | - return $SecurityScheme; |
|
| 295 | - |
|
| 296 | - case 'require': |
|
| 297 | - $name = self::wordShift($data); |
|
| 298 | - if (empty($name)) { |
|
| 299 | - throw new Exception('Missing require name'); |
|
| 300 | - } |
|
| 301 | - $scopes = self::wordSplit($data); |
|
| 302 | - sort($scopes); |
|
| 303 | - $this->security[] = [ |
|
| 304 | - $name => empty($scopes) ? [] : $scopes, |
|
| 305 | - ]; |
|
| 306 | - |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return parent::handleCommand($command, $data); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @inheritDoc |
|
| 315 | - * @throws Exception |
|
| 316 | - * @throws Exception |
|
| 317 | - */ |
|
| 318 | - public function toArray(): array |
|
| 319 | - { |
|
| 320 | - if (empty($this->paths)) { |
|
| 321 | - throw new Exception('No path defined'); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $schemes = array_unique($this->schemes); |
|
| 325 | - sort($schemes); |
|
| 326 | - |
|
| 327 | - $consumes = array_unique($this->consumes); |
|
| 328 | - sort($consumes); |
|
| 329 | - |
|
| 330 | - $produces = array_unique($this->produces); |
|
| 331 | - sort($produces); |
|
| 332 | - |
|
| 333 | - foreach ($this->security as $security) { |
|
| 334 | - foreach ($security as $name => $scopes) { |
|
| 335 | - if (!isset($this->securityDefinitions[$name])) { |
|
| 336 | - throw new Exception('Required security scheme not defined: \'' . $name . '\''); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - return self::arrayFilterNull(array_merge([ |
|
| 342 | - 'swagger' => $this->swagger, |
|
| 343 | - 'info' => $this->info->toArray(), |
|
| 344 | - 'host' => empty($this->host) ? null : $this->host, |
|
| 345 | - 'basePath' => empty($this->basePath) ? null : $this->basePath, |
|
| 346 | - 'consumes' => $consumes, |
|
| 347 | - 'produces' => $produces, |
|
| 348 | - 'schemes' => $schemes, |
|
| 349 | - 'paths' => self::objectsToArray($this->paths), |
|
| 350 | - 'definitions' => self::objectsToArray($this->definitions), |
|
| 351 | - 'parameters' => self::objectsToArray($this->parameters), |
|
| 352 | - 'responses' => self::objectsToArray($this->responses), |
|
| 353 | - 'securityDefinitions' => self::objectsToArray($this->securityDefinitions), |
|
| 354 | - 'security' => $this->security, |
|
| 355 | - 'tags' => self::objectsToArray($this->tags), |
|
| 356 | - ], parent::toArray())); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - public function __toString() |
|
| 360 | - { |
|
| 361 | - return __CLASS__; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Check if an operation with the given id exists. |
|
| 366 | - * |
|
| 367 | - * @param string $operationId |
|
| 368 | - * |
|
| 369 | - * @return boolean |
|
| 370 | - */ |
|
| 371 | - public function hasOperationId($operationId) |
|
| 372 | - { |
|
| 373 | - foreach ($this->paths as $path) { |
|
| 374 | - if ($path->hasOperationId($operationId)) { |
|
| 375 | - return true; |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return false; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Check if a definition with the given name exists |
|
| 384 | - * |
|
| 385 | - * @param string $name |
|
| 386 | - * |
|
| 387 | - * @return boolean |
|
| 388 | - */ |
|
| 389 | - public function hasDefinition($name) |
|
| 390 | - { |
|
| 391 | - return isset($this->definitions[$name]); |
|
| 392 | - } |
|
| 19 | + private $swagger = '2.0'; |
|
| 20 | + private $host; |
|
| 21 | + private $basePath; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var TypeRegistry |
|
| 25 | + */ |
|
| 26 | + private $typeRegistry; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var Info $Info |
|
| 30 | + */ |
|
| 31 | + private $info; |
|
| 32 | + private $schemes = []; |
|
| 33 | + private $consumes = []; |
|
| 34 | + private $produces = []; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var Path[] $Paths |
|
| 38 | + */ |
|
| 39 | + private $paths = []; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Schema[] $definitions |
|
| 43 | + */ |
|
| 44 | + private $definitions = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var IParameter[] $parameters |
|
| 48 | + */ |
|
| 49 | + private $parameters = []; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var Response[] $responses |
|
| 53 | + */ |
|
| 54 | + private $responses = []; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var Tag[] $Tags |
|
| 58 | + */ |
|
| 59 | + private $tags = []; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Default tag for new endpoints/operations. Set by the api command. |
|
| 63 | + * |
|
| 64 | + * @var Tag |
|
| 65 | + */ |
|
| 66 | + private $defaultTag = null; |
|
| 67 | + private $securityDefinitions = []; |
|
| 68 | + private $security = []; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param string $host |
|
| 72 | + * @param string $basePath |
|
| 73 | + * @param TypeRegistry $typeRegistry |
|
| 74 | + */ |
|
| 75 | + public function __construct($host = null, $basePath = null, $typeRegistry = null) |
|
| 76 | + { |
|
| 77 | + parent::__construct(); |
|
| 78 | + |
|
| 79 | + $this->host = $host; |
|
| 80 | + $this->basePath = $basePath; |
|
| 81 | + |
|
| 82 | + $this->info = new Info($this); |
|
| 83 | + |
|
| 84 | + $this->typeRegistry = $typeRegistry ?: new TypeRegistry; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @inheritDoc |
|
| 89 | + */ |
|
| 90 | + protected function getSwagger(): Swagger |
|
| 91 | + { |
|
| 92 | + return $this; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @inheritDoc |
|
| 97 | + */ |
|
| 98 | + protected function getTypeRegistry(): TypeRegistry |
|
| 99 | + { |
|
| 100 | + return $this->typeRegistry; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Return all consumes |
|
| 105 | + * |
|
| 106 | + * @return array |
|
| 107 | + * @todo Deprecate in favour of a getConsume($name); |
|
| 108 | + */ |
|
| 109 | + public function getConsumes(): array |
|
| 110 | + { |
|
| 111 | + return $this->consumes; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Return the named security if it exists, otherwise return FALSE |
|
| 116 | + * |
|
| 117 | + * @param string $name |
|
| 118 | + * |
|
| 119 | + * @return boolean|SecurityScheme |
|
| 120 | + */ |
|
| 121 | + public function getSecurity($name) |
|
| 122 | + { |
|
| 123 | + return $this->securityDefinitions[$name] ?? false; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $command |
|
| 128 | + * @param string $data |
|
| 129 | + * |
|
| 130 | + * @return AbstractObject|boolean |
|
| 131 | + * @throws Exception |
|
| 132 | + * @throws Exception |
|
| 133 | + * @throws Exception |
|
| 134 | + * @throws Exception |
|
| 135 | + * @throws Exception |
|
| 136 | + * @throws Exception |
|
| 137 | + * @throws Exception |
|
| 138 | + * @throws Exception |
|
| 139 | + * @throws Exception |
|
| 140 | + */ |
|
| 141 | + public function handleCommand($command, $data = null) |
|
| 142 | + { |
|
| 143 | + switch (strtolower($command)) { |
|
| 144 | + // pass to Info |
|
| 145 | + case 'title': |
|
| 146 | + case 'description': |
|
| 147 | + case 'version': |
|
| 148 | + case 'terms': // alias |
|
| 149 | + case 'tos': // alias |
|
| 150 | + case 'termsofservice': |
|
| 151 | + case 'contact': |
|
| 152 | + case 'license': |
|
| 153 | + return $this->info->handleCommand($command, $data); |
|
| 154 | + |
|
| 155 | + // string[] |
|
| 156 | + case 'scheme': |
|
| 157 | + case 'schemes': |
|
| 158 | + $this->schemes = array_unique(array_merge($this->schemes, self::wordSplit($data))); |
|
| 159 | + |
|
| 160 | + return $this; |
|
| 161 | + |
|
| 162 | + // MIME[] |
|
| 163 | + case 'consume': |
|
| 164 | + case 'consumes': |
|
| 165 | + $this->consumes = array_merge($this->consumes, self::translateMimeTypes(self::wordSplit($data))); |
|
| 166 | + |
|
| 167 | + return $this; |
|
| 168 | + |
|
| 169 | + case 'produce': |
|
| 170 | + case 'produces': |
|
| 171 | + $this->produces = array_merge($this->produces, self::translateMimeTypes(self::wordSplit($data))); |
|
| 172 | + |
|
| 173 | + return $this; |
|
| 174 | + |
|
| 175 | + case 'model': |
|
| 176 | + case 'model!': |
|
| 177 | + case 'definition': |
|
| 178 | + case 'definition!': |
|
| 179 | + $name = self::wordShift($data); |
|
| 180 | + if (empty($name)) { |
|
| 181 | + throw new Exception('Missing definition name'); |
|
| 182 | + } |
|
| 183 | + $typeDef = self::wordShift($data); |
|
| 184 | + if (empty($typeDef)) { |
|
| 185 | + $typeDef = 'object'; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $definition = new Schema($this, $typeDef); |
|
| 189 | + if (substr($command, -1) === '!') { |
|
| 190 | + $definition->setReadOnly(); |
|
| 191 | + } |
|
| 192 | + $this->definitions[$name] = $definition; |
|
| 193 | + |
|
| 194 | + return $definition; |
|
| 195 | + |
|
| 196 | + case 'path': |
|
| 197 | + case 'query': |
|
| 198 | + case 'query?': |
|
| 199 | + case 'header': |
|
| 200 | + case 'header?': |
|
| 201 | + case 'form': |
|
| 202 | + case 'form?': |
|
| 203 | + $in = rtrim($command, '?'); |
|
| 204 | + $Parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?'); |
|
| 205 | + $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 206 | + |
|
| 207 | + return $Parameter; |
|
| 208 | + |
|
| 209 | + case 'body': |
|
| 210 | + case 'body?': |
|
| 211 | + $Parameter = new BodyParameter($this, $data, substr($command, -1) !== '?'); |
|
| 212 | + $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 213 | + |
|
| 214 | + return $Parameter; |
|
| 215 | + |
|
| 216 | + case 'response': |
|
| 217 | + $name = self::wordShift($data); |
|
| 218 | + $definition = self::wordShift($data); |
|
| 219 | + $description = $data; |
|
| 220 | + if (empty($description)) { |
|
| 221 | + throw new Exception('Response definition missing description'); |
|
| 222 | + } |
|
| 223 | + $Response = new Response($this, $name, $definition === 'null' ? null : $definition, $description); |
|
| 224 | + $this->responses[$name] = $Response; |
|
| 225 | + |
|
| 226 | + return $Response; |
|
| 227 | + |
|
| 228 | + case 'api': // alias |
|
| 229 | + case 'tag': |
|
| 230 | + $tagname = self::wordShift($data); |
|
| 231 | + if (empty($tagname)) { |
|
| 232 | + throw new Exception('Missing tag name'); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $Tag = null; |
|
| 236 | + foreach ($this->tags as $T) { |
|
| 237 | + if ($T->getName() === $tagname) { |
|
| 238 | + $Tag = $T; |
|
| 239 | + break; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + if (!$Tag) { |
|
| 243 | + $Tag = new Tag($this, $tagname, $data); |
|
| 244 | + $this->tags[] = $Tag; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + // backwards compatibility |
|
| 248 | + if ($command === 'api') { |
|
| 249 | + $this->defaultTag = $Tag; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $Tag; |
|
| 253 | + |
|
| 254 | + case 'endpoint': |
|
| 255 | + $path = self::wordShift($data); |
|
| 256 | + if (false === $path) { |
|
| 257 | + $path = '/'; |
|
| 258 | + } else if ($path[0] !== '/') { |
|
| 259 | + $path = '/' . $path; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + $Tag = null; |
|
| 263 | + if (($tagname = self::wordShift($data)) !== false) { |
|
| 264 | + foreach ($this->tags as $T) { |
|
| 265 | + if (strtolower($T->getName()) === strtolower($tagname)) { |
|
| 266 | + $Tag = $T; |
|
| 267 | + break; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + if (!$Tag) { |
|
| 271 | + $Tag = new Tag($this, $tagname, $data); |
|
| 272 | + $this->tags[] = $Tag; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + if (!isset($this->paths[$path])) { |
|
| 277 | + $this->paths[$path] = new Path($this, $Tag ?: $this->defaultTag); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return $this->paths[$path]; |
|
| 281 | + |
|
| 282 | + case 'security': |
|
| 283 | + $name = self::wordShift($data); |
|
| 284 | + if (empty($name)) { |
|
| 285 | + throw new Exception('Missing security name'); |
|
| 286 | + } |
|
| 287 | + $type = self::wordShift($data); |
|
| 288 | + if (empty($type)) { |
|
| 289 | + throw new Exception('Missing security type'); |
|
| 290 | + } |
|
| 291 | + $SecurityScheme = new SecurityScheme($this, $type, $data); |
|
| 292 | + $this->securityDefinitions[$name] = $SecurityScheme; |
|
| 293 | + |
|
| 294 | + return $SecurityScheme; |
|
| 295 | + |
|
| 296 | + case 'require': |
|
| 297 | + $name = self::wordShift($data); |
|
| 298 | + if (empty($name)) { |
|
| 299 | + throw new Exception('Missing require name'); |
|
| 300 | + } |
|
| 301 | + $scopes = self::wordSplit($data); |
|
| 302 | + sort($scopes); |
|
| 303 | + $this->security[] = [ |
|
| 304 | + $name => empty($scopes) ? [] : $scopes, |
|
| 305 | + ]; |
|
| 306 | + |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return parent::handleCommand($command, $data); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @inheritDoc |
|
| 315 | + * @throws Exception |
|
| 316 | + * @throws Exception |
|
| 317 | + */ |
|
| 318 | + public function toArray(): array |
|
| 319 | + { |
|
| 320 | + if (empty($this->paths)) { |
|
| 321 | + throw new Exception('No path defined'); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $schemes = array_unique($this->schemes); |
|
| 325 | + sort($schemes); |
|
| 326 | + |
|
| 327 | + $consumes = array_unique($this->consumes); |
|
| 328 | + sort($consumes); |
|
| 329 | + |
|
| 330 | + $produces = array_unique($this->produces); |
|
| 331 | + sort($produces); |
|
| 332 | + |
|
| 333 | + foreach ($this->security as $security) { |
|
| 334 | + foreach ($security as $name => $scopes) { |
|
| 335 | + if (!isset($this->securityDefinitions[$name])) { |
|
| 336 | + throw new Exception('Required security scheme not defined: \'' . $name . '\''); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + return self::arrayFilterNull(array_merge([ |
|
| 342 | + 'swagger' => $this->swagger, |
|
| 343 | + 'info' => $this->info->toArray(), |
|
| 344 | + 'host' => empty($this->host) ? null : $this->host, |
|
| 345 | + 'basePath' => empty($this->basePath) ? null : $this->basePath, |
|
| 346 | + 'consumes' => $consumes, |
|
| 347 | + 'produces' => $produces, |
|
| 348 | + 'schemes' => $schemes, |
|
| 349 | + 'paths' => self::objectsToArray($this->paths), |
|
| 350 | + 'definitions' => self::objectsToArray($this->definitions), |
|
| 351 | + 'parameters' => self::objectsToArray($this->parameters), |
|
| 352 | + 'responses' => self::objectsToArray($this->responses), |
|
| 353 | + 'securityDefinitions' => self::objectsToArray($this->securityDefinitions), |
|
| 354 | + 'security' => $this->security, |
|
| 355 | + 'tags' => self::objectsToArray($this->tags), |
|
| 356 | + ], parent::toArray())); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + public function __toString() |
|
| 360 | + { |
|
| 361 | + return __CLASS__; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Check if an operation with the given id exists. |
|
| 366 | + * |
|
| 367 | + * @param string $operationId |
|
| 368 | + * |
|
| 369 | + * @return boolean |
|
| 370 | + */ |
|
| 371 | + public function hasOperationId($operationId) |
|
| 372 | + { |
|
| 373 | + foreach ($this->paths as $path) { |
|
| 374 | + if ($path->hasOperationId($operationId)) { |
|
| 375 | + return true; |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return false; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Check if a definition with the given name exists |
|
| 384 | + * |
|
| 385 | + * @param string $name |
|
| 386 | + * |
|
| 387 | + * @return boolean |
|
| 388 | + */ |
|
| 389 | + public function hasDefinition($name) |
|
| 390 | + { |
|
| 391 | + return isset($this->definitions[$name]); |
|
| 392 | + } |
|
| 393 | 393 | |
| 394 | 394 | } |
@@ -38,215 +38,215 @@ |
||
| 38 | 38 | class SwaggerGen |
| 39 | 39 | { |
| 40 | 40 | |
| 41 | - const FORMAT_ARRAY = ''; |
|
| 42 | - const FORMAT_JSON = 'json'; |
|
| 43 | - const FORMAT_JSON_PRETTY = 'json+'; |
|
| 44 | - const FORMAT_YAML = 'yaml'; |
|
| 45 | - |
|
| 46 | - private $host; |
|
| 47 | - private $basePath; |
|
| 48 | - private $dirs; |
|
| 49 | - private $defines = []; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var TypeRegistry |
|
| 53 | - */ |
|
| 54 | - private $typeRegistry; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Create a new SwaggerGen instance |
|
| 58 | - * |
|
| 59 | - * @param string $host |
|
| 60 | - * @param string $basePath |
|
| 61 | - * @param string[] $dirs |
|
| 62 | - * @param TypeRegistry $typeRegistry |
|
| 63 | - */ |
|
| 64 | - public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null) |
|
| 65 | - { |
|
| 66 | - $this->host = $host; |
|
| 67 | - $this->basePath = $basePath; |
|
| 68 | - $this->dirs = $dirs; |
|
| 69 | - $this->typeRegistry = $typeRegistry; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Set a new type registry |
|
| 74 | - * |
|
| 75 | - * @param TypeRegistry $typeRegistry |
|
| 76 | - */ |
|
| 77 | - public function setTypeRegistry($typeRegistry = null) |
|
| 78 | - { |
|
| 79 | - $this->typeRegistry = $typeRegistry; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param string $name |
|
| 84 | - */ |
|
| 85 | - public function define($name, $value = 1) |
|
| 86 | - { |
|
| 87 | - $this->defines[$name] = $value; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $name |
|
| 92 | - */ |
|
| 93 | - public function undefine($name) |
|
| 94 | - { |
|
| 95 | - unset($this->defines[$name]); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $file |
|
| 100 | - * @param string[] $dirs |
|
| 101 | - * @return Statement[] |
|
| 102 | - * @throws Exception |
|
| 103 | - */ |
|
| 104 | - private function parsePhpFile(string $file, array $dirs): array |
|
| 105 | - { |
|
| 106 | - return (new Parser())->parse($file, $dirs, $this->defines); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param string $file |
|
| 111 | - * @param string[] $dirs |
|
| 112 | - * @return Statement[] |
|
| 113 | - */ |
|
| 114 | - private function parseTextFile($file, $dirs) |
|
| 115 | - { |
|
| 116 | - return (new TextParser())->parse($file, $dirs, $this->defines); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @param string $text |
|
| 121 | - * @param string[] $dirs |
|
| 122 | - * @return Statement[] |
|
| 123 | - */ |
|
| 124 | - private function parseText(string $text, array $dirs): array |
|
| 125 | - { |
|
| 126 | - return (new TextParser())->parseText($text, $dirs, $this->defines); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Creates Swagger\Swagger object and populates it with statements |
|
| 131 | - * |
|
| 132 | - * This effectively converts the linear list of statements into parse-tree |
|
| 133 | - * like structure, performing some checks (like rejecting unknown |
|
| 134 | - * subcommands) during the process. Returned Swagger\Swagger object is |
|
| 135 | - * ready for serialization with {@see Swagger\Swagger::toArray} |
|
| 136 | - * |
|
| 137 | - * @param string $host |
|
| 138 | - * @param string $basePath |
|
| 139 | - * @param Statement[] $statements |
|
| 140 | - * @return Swagger |
|
| 141 | - * @throws StatementException |
|
| 142 | - */ |
|
| 143 | - public function parseStatements($host, $basePath, $statements) |
|
| 144 | - { |
|
| 145 | - $swagger = new Swagger($host, $basePath, $this->typeRegistry); |
|
| 146 | - |
|
| 147 | - $stack = array($swagger); |
|
| 148 | - /* @var AbstractObject[] $stack */ |
|
| 149 | - foreach ($statements as $statement) { |
|
| 150 | - try { |
|
| 151 | - $top = end($stack); |
|
| 152 | - |
|
| 153 | - do { |
|
| 154 | - $result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
|
| 155 | - |
|
| 156 | - if ($result) { |
|
| 157 | - if ($result !== $top) { |
|
| 158 | - // Remove all similar classes from array first! |
|
| 159 | - $classname = get_class($result); |
|
| 160 | - $stack = array_filter($stack, static function ($class) use ($classname) { |
|
| 161 | - return !(is_a($class, $classname)); |
|
| 162 | - }); |
|
| 163 | - |
|
| 164 | - $stack[] = $result; |
|
| 165 | - } |
|
| 166 | - } else { |
|
| 167 | - $top = prev($stack); |
|
| 168 | - } |
|
| 169 | - } while (!$result && $top); |
|
| 170 | - } catch (Exception $e) { |
|
| 171 | - throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if (!$result && !$top) { |
|
| 175 | - $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
|
| 176 | - |
|
| 177 | - $stacktrace = []; |
|
| 178 | - foreach ($stack as $object) { |
|
| 179 | - $stacktrace[] = (string)$object; |
|
| 180 | - } |
|
| 181 | - $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
|
| 182 | - |
|
| 183 | - throw new StatementException(implode('. ', $messages), 0, null, $statement); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return $swagger; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Get Swagger 2.x output |
|
| 192 | - * |
|
| 193 | - * @param string[] $files |
|
| 194 | - * @param string[] $dirs |
|
| 195 | - * @param string $format |
|
| 196 | - * @return array|false|string |
|
| 197 | - * @throws Exception |
|
| 198 | - * @throws StatementException |
|
| 199 | - */ |
|
| 200 | - public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY) |
|
| 201 | - { |
|
| 202 | - $dirs = array_merge($this->dirs, $dirs); |
|
| 203 | - |
|
| 204 | - $statements = []; |
|
| 205 | - foreach ($files as $file) { |
|
| 206 | - switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
| 207 | - case 'php': |
|
| 208 | - $fileStatements = $this->parsePhpFile($file, $dirs); |
|
| 209 | - break; |
|
| 210 | - |
|
| 211 | - case 'txt': |
|
| 212 | - $fileStatements = $this->parseTextFile($file, $dirs); |
|
| 213 | - break; |
|
| 214 | - |
|
| 215 | - default: |
|
| 216 | - $fileStatements = $this->parseText($file, $dirs); |
|
| 217 | - break; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $statements[] = $fileStatements; |
|
| 221 | - } |
|
| 222 | - $statements = array_merge(...$statements); |
|
| 223 | - |
|
| 224 | - $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray(); |
|
| 225 | - |
|
| 226 | - switch ($format) { |
|
| 227 | - case self::FORMAT_JSON: |
|
| 228 | - $output = json_encode($output); |
|
| 229 | - break; |
|
| 230 | - |
|
| 231 | - case self::FORMAT_JSON_PRETTY: |
|
| 232 | - $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
|
| 233 | - $output = json_encode($output, $flags); |
|
| 234 | - break; |
|
| 235 | - |
|
| 236 | - case self::FORMAT_YAML: |
|
| 237 | - if (!function_exists('yaml_emit')) { |
|
| 238 | - throw new Exception('YAML extension not installed.'); |
|
| 239 | - } |
|
| 240 | - array_walk_recursive($output, static function (&$value) { |
|
| 241 | - if (is_object($value)) { |
|
| 242 | - $value = (array)$value; |
|
| 243 | - } |
|
| 244 | - }); |
|
| 245 | - $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
| 246 | - break; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $output; |
|
| 250 | - } |
|
| 41 | + const FORMAT_ARRAY = ''; |
|
| 42 | + const FORMAT_JSON = 'json'; |
|
| 43 | + const FORMAT_JSON_PRETTY = 'json+'; |
|
| 44 | + const FORMAT_YAML = 'yaml'; |
|
| 45 | + |
|
| 46 | + private $host; |
|
| 47 | + private $basePath; |
|
| 48 | + private $dirs; |
|
| 49 | + private $defines = []; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var TypeRegistry |
|
| 53 | + */ |
|
| 54 | + private $typeRegistry; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Create a new SwaggerGen instance |
|
| 58 | + * |
|
| 59 | + * @param string $host |
|
| 60 | + * @param string $basePath |
|
| 61 | + * @param string[] $dirs |
|
| 62 | + * @param TypeRegistry $typeRegistry |
|
| 63 | + */ |
|
| 64 | + public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null) |
|
| 65 | + { |
|
| 66 | + $this->host = $host; |
|
| 67 | + $this->basePath = $basePath; |
|
| 68 | + $this->dirs = $dirs; |
|
| 69 | + $this->typeRegistry = $typeRegistry; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Set a new type registry |
|
| 74 | + * |
|
| 75 | + * @param TypeRegistry $typeRegistry |
|
| 76 | + */ |
|
| 77 | + public function setTypeRegistry($typeRegistry = null) |
|
| 78 | + { |
|
| 79 | + $this->typeRegistry = $typeRegistry; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param string $name |
|
| 84 | + */ |
|
| 85 | + public function define($name, $value = 1) |
|
| 86 | + { |
|
| 87 | + $this->defines[$name] = $value; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $name |
|
| 92 | + */ |
|
| 93 | + public function undefine($name) |
|
| 94 | + { |
|
| 95 | + unset($this->defines[$name]); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $file |
|
| 100 | + * @param string[] $dirs |
|
| 101 | + * @return Statement[] |
|
| 102 | + * @throws Exception |
|
| 103 | + */ |
|
| 104 | + private function parsePhpFile(string $file, array $dirs): array |
|
| 105 | + { |
|
| 106 | + return (new Parser())->parse($file, $dirs, $this->defines); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param string $file |
|
| 111 | + * @param string[] $dirs |
|
| 112 | + * @return Statement[] |
|
| 113 | + */ |
|
| 114 | + private function parseTextFile($file, $dirs) |
|
| 115 | + { |
|
| 116 | + return (new TextParser())->parse($file, $dirs, $this->defines); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @param string $text |
|
| 121 | + * @param string[] $dirs |
|
| 122 | + * @return Statement[] |
|
| 123 | + */ |
|
| 124 | + private function parseText(string $text, array $dirs): array |
|
| 125 | + { |
|
| 126 | + return (new TextParser())->parseText($text, $dirs, $this->defines); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Creates Swagger\Swagger object and populates it with statements |
|
| 131 | + * |
|
| 132 | + * This effectively converts the linear list of statements into parse-tree |
|
| 133 | + * like structure, performing some checks (like rejecting unknown |
|
| 134 | + * subcommands) during the process. Returned Swagger\Swagger object is |
|
| 135 | + * ready for serialization with {@see Swagger\Swagger::toArray} |
|
| 136 | + * |
|
| 137 | + * @param string $host |
|
| 138 | + * @param string $basePath |
|
| 139 | + * @param Statement[] $statements |
|
| 140 | + * @return Swagger |
|
| 141 | + * @throws StatementException |
|
| 142 | + */ |
|
| 143 | + public function parseStatements($host, $basePath, $statements) |
|
| 144 | + { |
|
| 145 | + $swagger = new Swagger($host, $basePath, $this->typeRegistry); |
|
| 146 | + |
|
| 147 | + $stack = array($swagger); |
|
| 148 | + /* @var AbstractObject[] $stack */ |
|
| 149 | + foreach ($statements as $statement) { |
|
| 150 | + try { |
|
| 151 | + $top = end($stack); |
|
| 152 | + |
|
| 153 | + do { |
|
| 154 | + $result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
|
| 155 | + |
|
| 156 | + if ($result) { |
|
| 157 | + if ($result !== $top) { |
|
| 158 | + // Remove all similar classes from array first! |
|
| 159 | + $classname = get_class($result); |
|
| 160 | + $stack = array_filter($stack, static function ($class) use ($classname) { |
|
| 161 | + return !(is_a($class, $classname)); |
|
| 162 | + }); |
|
| 163 | + |
|
| 164 | + $stack[] = $result; |
|
| 165 | + } |
|
| 166 | + } else { |
|
| 167 | + $top = prev($stack); |
|
| 168 | + } |
|
| 169 | + } while (!$result && $top); |
|
| 170 | + } catch (Exception $e) { |
|
| 171 | + throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if (!$result && !$top) { |
|
| 175 | + $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
|
| 176 | + |
|
| 177 | + $stacktrace = []; |
|
| 178 | + foreach ($stack as $object) { |
|
| 179 | + $stacktrace[] = (string)$object; |
|
| 180 | + } |
|
| 181 | + $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
|
| 182 | + |
|
| 183 | + throw new StatementException(implode('. ', $messages), 0, null, $statement); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return $swagger; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Get Swagger 2.x output |
|
| 192 | + * |
|
| 193 | + * @param string[] $files |
|
| 194 | + * @param string[] $dirs |
|
| 195 | + * @param string $format |
|
| 196 | + * @return array|false|string |
|
| 197 | + * @throws Exception |
|
| 198 | + * @throws StatementException |
|
| 199 | + */ |
|
| 200 | + public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY) |
|
| 201 | + { |
|
| 202 | + $dirs = array_merge($this->dirs, $dirs); |
|
| 203 | + |
|
| 204 | + $statements = []; |
|
| 205 | + foreach ($files as $file) { |
|
| 206 | + switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
| 207 | + case 'php': |
|
| 208 | + $fileStatements = $this->parsePhpFile($file, $dirs); |
|
| 209 | + break; |
|
| 210 | + |
|
| 211 | + case 'txt': |
|
| 212 | + $fileStatements = $this->parseTextFile($file, $dirs); |
|
| 213 | + break; |
|
| 214 | + |
|
| 215 | + default: |
|
| 216 | + $fileStatements = $this->parseText($file, $dirs); |
|
| 217 | + break; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $statements[] = $fileStatements; |
|
| 221 | + } |
|
| 222 | + $statements = array_merge(...$statements); |
|
| 223 | + |
|
| 224 | + $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray(); |
|
| 225 | + |
|
| 226 | + switch ($format) { |
|
| 227 | + case self::FORMAT_JSON: |
|
| 228 | + $output = json_encode($output); |
|
| 229 | + break; |
|
| 230 | + |
|
| 231 | + case self::FORMAT_JSON_PRETTY: |
|
| 232 | + $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
|
| 233 | + $output = json_encode($output, $flags); |
|
| 234 | + break; |
|
| 235 | + |
|
| 236 | + case self::FORMAT_YAML: |
|
| 237 | + if (!function_exists('yaml_emit')) { |
|
| 238 | + throw new Exception('YAML extension not installed.'); |
|
| 239 | + } |
|
| 240 | + array_walk_recursive($output, static function (&$value) { |
|
| 241 | + if (is_object($value)) { |
|
| 242 | + $value = (array)$value; |
|
| 243 | + } |
|
| 244 | + }); |
|
| 245 | + $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
| 246 | + break; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $output; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | 252 | } |