@@ -16,102 +16,102 @@ |
||
| 16 | 16 | class Path extends AbstractObject |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private static $methods = array( |
|
| 20 | - 'get', |
|
| 21 | - 'put', |
|
| 22 | - 'post', |
|
| 23 | - 'delete', |
|
| 24 | - 'options', |
|
| 25 | - 'head', |
|
| 26 | - 'patch', |
|
| 27 | - ); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Operation[] $operation |
|
| 31 | - */ |
|
| 32 | - private $operations = []; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var Tag|null $tag ; |
|
| 36 | - */ |
|
| 37 | - private $tag; |
|
| 38 | - |
|
| 39 | - public function __construct(AbstractObject $parent, ?Tag $Tag = null) |
|
| 40 | - { |
|
| 41 | - parent::__construct($parent); |
|
| 42 | - $this->tag = $Tag; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param string $command |
|
| 47 | - * @param string $data |
|
| 48 | - * @return AbstractObject|boolean |
|
| 49 | - * @throws Exception |
|
| 50 | - */ |
|
| 51 | - public function handleCommand($command, $data = null) |
|
| 52 | - { |
|
| 53 | - switch (strtolower($command)) { |
|
| 54 | - case 'method': // alias |
|
| 55 | - case 'operation': |
|
| 56 | - $method = strtolower(self::wordShift($data)); |
|
| 57 | - |
|
| 58 | - if (!in_array($method, self::$methods)) { |
|
| 59 | - throw new Exception('Unrecognized operation method \'' . $method . '\''); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if (isset($this->operations[$method])) { |
|
| 63 | - $Operation = $this->operations[$method]; |
|
| 64 | - } else { |
|
| 65 | - $summary = $data; |
|
| 66 | - $Operation = new Operation($this, $summary, $this->tag); |
|
| 67 | - $this->operations[$method] = $Operation; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return $Operation; |
|
| 71 | - |
|
| 72 | - case 'description': |
|
| 73 | - if ($this->tag) { |
|
| 74 | - return $this->tag->handleCommand($command, $data); |
|
| 75 | - } |
|
| 76 | - break; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return parent::handleCommand($command, $data); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function toArray(): array |
|
| 83 | - { |
|
| 84 | - $methods = self::$methods; |
|
| 85 | - uksort($this->operations, static function ($a, $b) use ($methods) { |
|
| 86 | - return array_search($a, $methods, true) - array_search($b, $methods, true); |
|
| 87 | - }); |
|
| 88 | - |
|
| 89 | - return self::arrayFilterNull(array_merge( |
|
| 90 | - self::objectsToArray($this->operations) |
|
| 91 | - , parent::toArray())); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - public function __toString() |
|
| 95 | - { |
|
| 96 | - end($this->operations); |
|
| 97 | - return __CLASS__ . ' ' . key($this->operations); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Check if an operation with the given id is registered to this Path. |
|
| 102 | - * |
|
| 103 | - * @param string $operationId |
|
| 104 | - * @return boolean |
|
| 105 | - */ |
|
| 106 | - public function hasOperationId($operationId): bool |
|
| 107 | - { |
|
| 108 | - foreach ($this->operations as $operation) { |
|
| 109 | - if ($operation->getId() === $operationId) { |
|
| 110 | - return true; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 19 | + private static $methods = array( |
|
| 20 | + 'get', |
|
| 21 | + 'put', |
|
| 22 | + 'post', |
|
| 23 | + 'delete', |
|
| 24 | + 'options', |
|
| 25 | + 'head', |
|
| 26 | + 'patch', |
|
| 27 | + ); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Operation[] $operation |
|
| 31 | + */ |
|
| 32 | + private $operations = []; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var Tag|null $tag ; |
|
| 36 | + */ |
|
| 37 | + private $tag; |
|
| 38 | + |
|
| 39 | + public function __construct(AbstractObject $parent, ?Tag $Tag = null) |
|
| 40 | + { |
|
| 41 | + parent::__construct($parent); |
|
| 42 | + $this->tag = $Tag; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param string $command |
|
| 47 | + * @param string $data |
|
| 48 | + * @return AbstractObject|boolean |
|
| 49 | + * @throws Exception |
|
| 50 | + */ |
|
| 51 | + public function handleCommand($command, $data = null) |
|
| 52 | + { |
|
| 53 | + switch (strtolower($command)) { |
|
| 54 | + case 'method': // alias |
|
| 55 | + case 'operation': |
|
| 56 | + $method = strtolower(self::wordShift($data)); |
|
| 57 | + |
|
| 58 | + if (!in_array($method, self::$methods)) { |
|
| 59 | + throw new Exception('Unrecognized operation method \'' . $method . '\''); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if (isset($this->operations[$method])) { |
|
| 63 | + $Operation = $this->operations[$method]; |
|
| 64 | + } else { |
|
| 65 | + $summary = $data; |
|
| 66 | + $Operation = new Operation($this, $summary, $this->tag); |
|
| 67 | + $this->operations[$method] = $Operation; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return $Operation; |
|
| 71 | + |
|
| 72 | + case 'description': |
|
| 73 | + if ($this->tag) { |
|
| 74 | + return $this->tag->handleCommand($command, $data); |
|
| 75 | + } |
|
| 76 | + break; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return parent::handleCommand($command, $data); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function toArray(): array |
|
| 83 | + { |
|
| 84 | + $methods = self::$methods; |
|
| 85 | + uksort($this->operations, static function ($a, $b) use ($methods) { |
|
| 86 | + return array_search($a, $methods, true) - array_search($b, $methods, true); |
|
| 87 | + }); |
|
| 88 | + |
|
| 89 | + return self::arrayFilterNull(array_merge( |
|
| 90 | + self::objectsToArray($this->operations) |
|
| 91 | + , parent::toArray())); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + public function __toString() |
|
| 95 | + { |
|
| 96 | + end($this->operations); |
|
| 97 | + return __CLASS__ . ' ' . key($this->operations); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Check if an operation with the given id is registered to this Path. |
|
| 102 | + * |
|
| 103 | + * @param string $operationId |
|
| 104 | + * @return boolean |
|
| 105 | + */ |
|
| 106 | + public function hasOperationId($operationId): bool |
|
| 107 | + { |
|
| 108 | + foreach ($this->operations as $operation) { |
|
| 109 | + if ($operation->getId() === $operationId) { |
|
| 110 | + return true; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | } |
@@ -82,7 +82,7 @@ |
||
| 82 | 82 | public function toArray(): array |
| 83 | 83 | { |
| 84 | 84 | $methods = self::$methods; |
| 85 | - uksort($this->operations, static function ($a, $b) use ($methods) { |
|
| 85 | + uksort($this->operations, static function($a, $b) use ($methods) { |
|
| 86 | 86 | return array_search($a, $methods, true) - array_search($b, $methods, true); |
| 87 | 87 | }); |
| 88 | 88 | |
@@ -17,88 +17,88 @@ |
||
| 17 | 17 | class Schema extends AbstractDocumentableObject implements IDefinition |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - private $description; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - private $title; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var bool |
|
| 32 | - */ |
|
| 33 | - private $readOnly; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var AbstractType |
|
| 37 | - */ |
|
| 38 | - private $type; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param string $description |
|
| 42 | - * @throws Exception |
|
| 43 | - */ |
|
| 44 | - public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
|
| 45 | - { |
|
| 46 | - parent::__construct($parent); |
|
| 47 | - |
|
| 48 | - // Check if definition set |
|
| 49 | - if ($this->getSwagger()->hasDefinition($definition)) { |
|
| 50 | - $this->type = new Type\ReferenceObjectType($this, $definition); |
|
| 51 | - } else { |
|
| 52 | - $this->type = Type\AbstractType::typeFactory($this, $definition); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $this->description = $description; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param string $command |
|
| 60 | - * @param string $data |
|
| 61 | - * @return AbstractObject|boolean |
|
| 62 | - * @throws Exception |
|
| 63 | - */ |
|
| 64 | - public function handleCommand($command, $data = null) |
|
| 65 | - { |
|
| 66 | - // Pass through to Type |
|
| 67 | - if ($this->type && $this->type->handleCommand($command, $data)) { |
|
| 68 | - return $this; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // handle all the rest manually |
|
| 72 | - switch (strtolower($command)) { |
|
| 73 | - case 'description': |
|
| 74 | - $this->description = $data; |
|
| 75 | - return $this; |
|
| 76 | - |
|
| 77 | - case 'title': |
|
| 78 | - $this->title = $data; |
|
| 79 | - return $this; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return parent::handleCommand($command, $data); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function toArray(): array |
|
| 86 | - { |
|
| 87 | - return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
|
| 88 | - 'title' => empty($this->title) ? null : $this->title, |
|
| 89 | - 'description' => empty($this->description) ? null : $this->description, |
|
| 90 | - 'readOnly' => $this->readOnly |
|
| 91 | - ), parent::toArray())); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - public function __toString() |
|
| 95 | - { |
|
| 96 | - return __CLASS__; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function setReadOnly(): void |
|
| 100 | - { |
|
| 101 | - $this->readOnly = true; |
|
| 102 | - } |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + private $description; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + private $title; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var bool |
|
| 32 | + */ |
|
| 33 | + private $readOnly; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var AbstractType |
|
| 37 | + */ |
|
| 38 | + private $type; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param string $description |
|
| 42 | + * @throws Exception |
|
| 43 | + */ |
|
| 44 | + public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
|
| 45 | + { |
|
| 46 | + parent::__construct($parent); |
|
| 47 | + |
|
| 48 | + // Check if definition set |
|
| 49 | + if ($this->getSwagger()->hasDefinition($definition)) { |
|
| 50 | + $this->type = new Type\ReferenceObjectType($this, $definition); |
|
| 51 | + } else { |
|
| 52 | + $this->type = Type\AbstractType::typeFactory($this, $definition); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $this->description = $description; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param string $command |
|
| 60 | + * @param string $data |
|
| 61 | + * @return AbstractObject|boolean |
|
| 62 | + * @throws Exception |
|
| 63 | + */ |
|
| 64 | + public function handleCommand($command, $data = null) |
|
| 65 | + { |
|
| 66 | + // Pass through to Type |
|
| 67 | + if ($this->type && $this->type->handleCommand($command, $data)) { |
|
| 68 | + return $this; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // handle all the rest manually |
|
| 72 | + switch (strtolower($command)) { |
|
| 73 | + case 'description': |
|
| 74 | + $this->description = $data; |
|
| 75 | + return $this; |
|
| 76 | + |
|
| 77 | + case 'title': |
|
| 78 | + $this->title = $data; |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return parent::handleCommand($command, $data); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function toArray(): array |
|
| 86 | + { |
|
| 87 | + return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
|
| 88 | + 'title' => empty($this->title) ? null : $this->title, |
|
| 89 | + 'description' => empty($this->description) ? null : $this->description, |
|
| 90 | + 'readOnly' => $this->readOnly |
|
| 91 | + ), parent::toArray())); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + public function __toString() |
|
| 95 | + { |
|
| 96 | + return __CLASS__; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function setReadOnly(): void |
|
| 100 | + { |
|
| 101 | + $this->readOnly = true; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | 104 | } |
@@ -16,230 +16,230 @@ |
||
| 16 | 16 | class Operation extends AbstractDocumentableObject |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private $tags = []; |
|
| 20 | - private $summary; |
|
| 21 | - private $description; |
|
| 22 | - private $consumes = []; |
|
| 23 | - private $produces = []; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var IParameter[] |
|
| 27 | - */ |
|
| 28 | - private $parameters = []; |
|
| 29 | - private $responses = []; |
|
| 30 | - private $schemes = []; |
|
| 31 | - private $deprecated = false; |
|
| 32 | - private $security = []; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $operationId; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param string $summary |
|
| 41 | - */ |
|
| 42 | - public function __construct(AbstractObject $parent, $summary = null, ?Tag $tag = null) |
|
| 43 | - { |
|
| 44 | - parent::__construct($parent); |
|
| 45 | - $this->summary = $summary; |
|
| 46 | - if ($tag) { |
|
| 47 | - $this->tags[] = $tag->getName(); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function getConsumes(): array |
|
| 52 | - { |
|
| 53 | - return $this->consumes; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param string $command |
|
| 58 | - * @param string $data |
|
| 59 | - * @return AbstractObject|boolean |
|
| 60 | - * @throws Exception |
|
| 61 | - * @throws Exception |
|
| 62 | - * @throws Exception |
|
| 63 | - * @throws Exception |
|
| 64 | - * @throws Exception |
|
| 65 | - * @throws Exception |
|
| 66 | - * @throws Exception |
|
| 67 | - */ |
|
| 68 | - public function handleCommand($command, $data = null) |
|
| 69 | - { |
|
| 70 | - switch (strtolower($command)) { |
|
| 71 | - // string |
|
| 72 | - case 'summary': |
|
| 73 | - case 'description': |
|
| 74 | - $this->$command = $data; |
|
| 75 | - return $this; |
|
| 76 | - |
|
| 77 | - // string[] |
|
| 78 | - case 'tags': |
|
| 79 | - case 'schemes': |
|
| 80 | - $this->$command = array_merge($this->$command, self::wordSplit($data)); |
|
| 81 | - return $this; |
|
| 82 | - |
|
| 83 | - // MIME[] |
|
| 84 | - case 'consumes': |
|
| 85 | - case 'produces': |
|
| 86 | - $this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data))); |
|
| 87 | - return $this; |
|
| 88 | - |
|
| 89 | - // boolean |
|
| 90 | - case 'deprecated': |
|
| 91 | - $this->deprecated = true; |
|
| 92 | - return $this; |
|
| 93 | - |
|
| 94 | - case 'error': |
|
| 95 | - $code = self::wordShift($data); |
|
| 96 | - $reasoncode = Response::getCode($code); |
|
| 97 | - if ($reasoncode === null) { |
|
| 98 | - throw new Exception("Invalid error code: '$code'"); |
|
| 99 | - } |
|
| 100 | - $description = $data; |
|
| 101 | - $Error = new Error($this, $reasoncode, $description); |
|
| 102 | - $this->responses[$reasoncode] = $Error; |
|
| 103 | - return $Error; |
|
| 104 | - |
|
| 105 | - case 'errors': |
|
| 106 | - foreach (self::wordSplit($data) as $code) { |
|
| 107 | - $reasoncode = Response::getCode($code); |
|
| 108 | - if ($reasoncode === null) { |
|
| 109 | - throw new Exception("Invalid error code: '$code'"); |
|
| 110 | - } |
|
| 111 | - $this->responses[$reasoncode] = new Error($this, $reasoncode); |
|
| 112 | - } |
|
| 113 | - return $this; |
|
| 114 | - |
|
| 115 | - case 'path': |
|
| 116 | - case 'query': |
|
| 117 | - case 'query?': |
|
| 118 | - case 'header': |
|
| 119 | - case 'header?': |
|
| 120 | - case 'form': |
|
| 121 | - case 'form?': |
|
| 122 | - $in = rtrim($command, '?'); |
|
| 123 | - $parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
| 124 | - $this->parameters[$parameter->getName()] = $parameter; |
|
| 125 | - return $parameter; |
|
| 126 | - |
|
| 127 | - case 'body': |
|
| 128 | - case 'body?': |
|
| 129 | - $parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
| 130 | - $this->parameters[$parameter->getName()] = $parameter; |
|
| 131 | - return $parameter; |
|
| 132 | - |
|
| 133 | - case 'param': |
|
| 134 | - case 'parameter': |
|
| 135 | - $parameter = new ParameterReference($this, $data); |
|
| 136 | - $this->parameters[$parameter->getName()] = $parameter; |
|
| 137 | - return $this; |
|
| 138 | - |
|
| 139 | - case 'response': |
|
| 140 | - $code = self::wordShift($data); |
|
| 141 | - $reasoncode = Response::getCode($code); |
|
| 142 | - if ($reasoncode === null) { |
|
| 143 | - $reference = $code; |
|
| 144 | - $code = self::wordShift($data); |
|
| 145 | - $reasoncode = Response::getCode($code); |
|
| 146 | - if ($reasoncode === null) { |
|
| 147 | - throw new Exception("Invalid response code: '$reference'"); |
|
| 148 | - } |
|
| 149 | - $this->responses[$reasoncode] = new ResponseReference($this, $reference); |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $definition = self::wordShift($data); |
|
| 154 | - $description = $data; |
|
| 155 | - $Response = new Response($this, $reasoncode, $definition, $description); |
|
| 156 | - $this->responses[$reasoncode] = $Response; |
|
| 157 | - return $Response; |
|
| 158 | - |
|
| 159 | - case 'require': |
|
| 160 | - $name = self::wordShift($data); |
|
| 161 | - if (empty($name)) { |
|
| 162 | - throw new Exception('Empty security requirement name'); |
|
| 163 | - } |
|
| 164 | - $scopes = self::wordSplit($data); |
|
| 165 | - sort($scopes); |
|
| 166 | - $this->security[] = array( |
|
| 167 | - $name => empty($scopes) ? [] : $scopes, |
|
| 168 | - ); |
|
| 169 | - return $this; |
|
| 170 | - |
|
| 171 | - case 'id': |
|
| 172 | - $operationId = self::trim($data); |
|
| 173 | - if ($this->getSwagger()->hasOperationId($operationId)) { |
|
| 174 | - throw new Exception("Duplicate operation id '{$operationId}'"); |
|
| 175 | - } |
|
| 176 | - $this->operationId = $operationId; |
|
| 177 | - return $this; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return parent::handleCommand($command, $data); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @throws Exception |
|
| 185 | - */ |
|
| 186 | - public function toArray(): array |
|
| 187 | - { |
|
| 188 | - if (empty($this->responses)) { |
|
| 189 | - throw new Exception('No response defined for operation'); |
|
| 190 | - } |
|
| 191 | - ksort($this->responses); |
|
| 192 | - |
|
| 193 | - $tags = array_unique($this->tags); |
|
| 194 | - sort($tags); |
|
| 195 | - |
|
| 196 | - $schemes = array_unique($this->schemes); |
|
| 197 | - sort($schemes); |
|
| 198 | - |
|
| 199 | - $consumes = array_unique($this->consumes); |
|
| 200 | - sort($consumes); |
|
| 201 | - |
|
| 202 | - $produces = array_unique($this->produces); |
|
| 203 | - sort($produces); |
|
| 204 | - |
|
| 205 | - foreach ($this->security as $security) { |
|
| 206 | - foreach ($security as $name => $scope) { |
|
| 207 | - if ($this->getSwagger()->getSecurity($name) === false) { |
|
| 208 | - throw new Exception("Required security scheme not defined: '{$name}'"); |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $parameters = $this->parameters ? array_values($this->parameters) : null; |
|
| 214 | - |
|
| 215 | - return self::arrayFilterNull(array_merge(array( |
|
| 216 | - 'deprecated' => $this->deprecated ? true : null, |
|
| 217 | - 'tags' => $tags, |
|
| 218 | - 'summary' => empty($this->summary) ? null : $this->summary, |
|
| 219 | - 'description' => empty($this->description) ? null : $this->description, |
|
| 220 | - 'operationId' => $this->operationId, |
|
| 221 | - 'consumes' => $consumes, |
|
| 222 | - 'produces' => $produces, |
|
| 223 | - 'parameters' => $parameters ? self::objectsToArray($parameters) : null, |
|
| 224 | - 'schemes' => $schemes, |
|
| 225 | - 'responses' => $this->responses ? self::objectsToArray($this->responses) : null, |
|
| 226 | - 'security' => $this->security, |
|
| 227 | - ), parent::toArray())); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Return the operation ID |
|
| 232 | - * |
|
| 233 | - * @return string|null |
|
| 234 | - */ |
|
| 235 | - public function getId(): ?string |
|
| 236 | - { |
|
| 237 | - return $this->operationId; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - public function __toString() |
|
| 241 | - { |
|
| 242 | - return __CLASS__ . ' ' . $this->summary; |
|
| 243 | - } |
|
| 19 | + private $tags = []; |
|
| 20 | + private $summary; |
|
| 21 | + private $description; |
|
| 22 | + private $consumes = []; |
|
| 23 | + private $produces = []; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var IParameter[] |
|
| 27 | + */ |
|
| 28 | + private $parameters = []; |
|
| 29 | + private $responses = []; |
|
| 30 | + private $schemes = []; |
|
| 31 | + private $deprecated = false; |
|
| 32 | + private $security = []; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $operationId; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param string $summary |
|
| 41 | + */ |
|
| 42 | + public function __construct(AbstractObject $parent, $summary = null, ?Tag $tag = null) |
|
| 43 | + { |
|
| 44 | + parent::__construct($parent); |
|
| 45 | + $this->summary = $summary; |
|
| 46 | + if ($tag) { |
|
| 47 | + $this->tags[] = $tag->getName(); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function getConsumes(): array |
|
| 52 | + { |
|
| 53 | + return $this->consumes; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param string $command |
|
| 58 | + * @param string $data |
|
| 59 | + * @return AbstractObject|boolean |
|
| 60 | + * @throws Exception |
|
| 61 | + * @throws Exception |
|
| 62 | + * @throws Exception |
|
| 63 | + * @throws Exception |
|
| 64 | + * @throws Exception |
|
| 65 | + * @throws Exception |
|
| 66 | + * @throws Exception |
|
| 67 | + */ |
|
| 68 | + public function handleCommand($command, $data = null) |
|
| 69 | + { |
|
| 70 | + switch (strtolower($command)) { |
|
| 71 | + // string |
|
| 72 | + case 'summary': |
|
| 73 | + case 'description': |
|
| 74 | + $this->$command = $data; |
|
| 75 | + return $this; |
|
| 76 | + |
|
| 77 | + // string[] |
|
| 78 | + case 'tags': |
|
| 79 | + case 'schemes': |
|
| 80 | + $this->$command = array_merge($this->$command, self::wordSplit($data)); |
|
| 81 | + return $this; |
|
| 82 | + |
|
| 83 | + // MIME[] |
|
| 84 | + case 'consumes': |
|
| 85 | + case 'produces': |
|
| 86 | + $this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data))); |
|
| 87 | + return $this; |
|
| 88 | + |
|
| 89 | + // boolean |
|
| 90 | + case 'deprecated': |
|
| 91 | + $this->deprecated = true; |
|
| 92 | + return $this; |
|
| 93 | + |
|
| 94 | + case 'error': |
|
| 95 | + $code = self::wordShift($data); |
|
| 96 | + $reasoncode = Response::getCode($code); |
|
| 97 | + if ($reasoncode === null) { |
|
| 98 | + throw new Exception("Invalid error code: '$code'"); |
|
| 99 | + } |
|
| 100 | + $description = $data; |
|
| 101 | + $Error = new Error($this, $reasoncode, $description); |
|
| 102 | + $this->responses[$reasoncode] = $Error; |
|
| 103 | + return $Error; |
|
| 104 | + |
|
| 105 | + case 'errors': |
|
| 106 | + foreach (self::wordSplit($data) as $code) { |
|
| 107 | + $reasoncode = Response::getCode($code); |
|
| 108 | + if ($reasoncode === null) { |
|
| 109 | + throw new Exception("Invalid error code: '$code'"); |
|
| 110 | + } |
|
| 111 | + $this->responses[$reasoncode] = new Error($this, $reasoncode); |
|
| 112 | + } |
|
| 113 | + return $this; |
|
| 114 | + |
|
| 115 | + case 'path': |
|
| 116 | + case 'query': |
|
| 117 | + case 'query?': |
|
| 118 | + case 'header': |
|
| 119 | + case 'header?': |
|
| 120 | + case 'form': |
|
| 121 | + case 'form?': |
|
| 122 | + $in = rtrim($command, '?'); |
|
| 123 | + $parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
| 124 | + $this->parameters[$parameter->getName()] = $parameter; |
|
| 125 | + return $parameter; |
|
| 126 | + |
|
| 127 | + case 'body': |
|
| 128 | + case 'body?': |
|
| 129 | + $parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
| 130 | + $this->parameters[$parameter->getName()] = $parameter; |
|
| 131 | + return $parameter; |
|
| 132 | + |
|
| 133 | + case 'param': |
|
| 134 | + case 'parameter': |
|
| 135 | + $parameter = new ParameterReference($this, $data); |
|
| 136 | + $this->parameters[$parameter->getName()] = $parameter; |
|
| 137 | + return $this; |
|
| 138 | + |
|
| 139 | + case 'response': |
|
| 140 | + $code = self::wordShift($data); |
|
| 141 | + $reasoncode = Response::getCode($code); |
|
| 142 | + if ($reasoncode === null) { |
|
| 143 | + $reference = $code; |
|
| 144 | + $code = self::wordShift($data); |
|
| 145 | + $reasoncode = Response::getCode($code); |
|
| 146 | + if ($reasoncode === null) { |
|
| 147 | + throw new Exception("Invalid response code: '$reference'"); |
|
| 148 | + } |
|
| 149 | + $this->responses[$reasoncode] = new ResponseReference($this, $reference); |
|
| 150 | + return $this; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $definition = self::wordShift($data); |
|
| 154 | + $description = $data; |
|
| 155 | + $Response = new Response($this, $reasoncode, $definition, $description); |
|
| 156 | + $this->responses[$reasoncode] = $Response; |
|
| 157 | + return $Response; |
|
| 158 | + |
|
| 159 | + case 'require': |
|
| 160 | + $name = self::wordShift($data); |
|
| 161 | + if (empty($name)) { |
|
| 162 | + throw new Exception('Empty security requirement name'); |
|
| 163 | + } |
|
| 164 | + $scopes = self::wordSplit($data); |
|
| 165 | + sort($scopes); |
|
| 166 | + $this->security[] = array( |
|
| 167 | + $name => empty($scopes) ? [] : $scopes, |
|
| 168 | + ); |
|
| 169 | + return $this; |
|
| 170 | + |
|
| 171 | + case 'id': |
|
| 172 | + $operationId = self::trim($data); |
|
| 173 | + if ($this->getSwagger()->hasOperationId($operationId)) { |
|
| 174 | + throw new Exception("Duplicate operation id '{$operationId}'"); |
|
| 175 | + } |
|
| 176 | + $this->operationId = $operationId; |
|
| 177 | + return $this; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return parent::handleCommand($command, $data); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @throws Exception |
|
| 185 | + */ |
|
| 186 | + public function toArray(): array |
|
| 187 | + { |
|
| 188 | + if (empty($this->responses)) { |
|
| 189 | + throw new Exception('No response defined for operation'); |
|
| 190 | + } |
|
| 191 | + ksort($this->responses); |
|
| 192 | + |
|
| 193 | + $tags = array_unique($this->tags); |
|
| 194 | + sort($tags); |
|
| 195 | + |
|
| 196 | + $schemes = array_unique($this->schemes); |
|
| 197 | + sort($schemes); |
|
| 198 | + |
|
| 199 | + $consumes = array_unique($this->consumes); |
|
| 200 | + sort($consumes); |
|
| 201 | + |
|
| 202 | + $produces = array_unique($this->produces); |
|
| 203 | + sort($produces); |
|
| 204 | + |
|
| 205 | + foreach ($this->security as $security) { |
|
| 206 | + foreach ($security as $name => $scope) { |
|
| 207 | + if ($this->getSwagger()->getSecurity($name) === false) { |
|
| 208 | + throw new Exception("Required security scheme not defined: '{$name}'"); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $parameters = $this->parameters ? array_values($this->parameters) : null; |
|
| 214 | + |
|
| 215 | + return self::arrayFilterNull(array_merge(array( |
|
| 216 | + 'deprecated' => $this->deprecated ? true : null, |
|
| 217 | + 'tags' => $tags, |
|
| 218 | + 'summary' => empty($this->summary) ? null : $this->summary, |
|
| 219 | + 'description' => empty($this->description) ? null : $this->description, |
|
| 220 | + 'operationId' => $this->operationId, |
|
| 221 | + 'consumes' => $consumes, |
|
| 222 | + 'produces' => $produces, |
|
| 223 | + 'parameters' => $parameters ? self::objectsToArray($parameters) : null, |
|
| 224 | + 'schemes' => $schemes, |
|
| 225 | + 'responses' => $this->responses ? self::objectsToArray($this->responses) : null, |
|
| 226 | + 'security' => $this->security, |
|
| 227 | + ), parent::toArray())); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Return the operation ID |
|
| 232 | + * |
|
| 233 | + * @return string|null |
|
| 234 | + */ |
|
| 235 | + public function getId(): ?string |
|
| 236 | + { |
|
| 237 | + return $this->operationId; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + public function __toString() |
|
| 241 | + { |
|
| 242 | + return __CLASS__ . ' ' . $this->summary; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | 245 | } |
@@ -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; |
|
| 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 | - * Return all consumes |
|
| 89 | - * |
|
| 90 | - * @return array |
|
| 91 | - * @todo Deprecate in favour of a getConsume($name); |
|
| 92 | - */ |
|
| 93 | - public function getConsumes(): array |
|
| 94 | - { |
|
| 95 | - return $this->consumes; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Return the named security if it exists, otherwise return FALSE |
|
| 100 | - * |
|
| 101 | - * @param string $name |
|
| 102 | - * |
|
| 103 | - * @return boolean|SecurityScheme |
|
| 104 | - */ |
|
| 105 | - public function getSecurity($name) |
|
| 106 | - { |
|
| 107 | - return $this->securityDefinitions[$name] ?? false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param string $command |
|
| 112 | - * @param string $data |
|
| 113 | - * |
|
| 114 | - * @return AbstractObject|boolean |
|
| 115 | - * @throws Exception |
|
| 116 | - * @throws Exception |
|
| 117 | - * @throws Exception |
|
| 118 | - * @throws Exception |
|
| 119 | - * @throws Exception |
|
| 120 | - * @throws Exception |
|
| 121 | - * @throws Exception |
|
| 122 | - * @throws Exception |
|
| 123 | - * @throws Exception |
|
| 124 | - */ |
|
| 125 | - public function handleCommand($command, $data = null) |
|
| 126 | - { |
|
| 127 | - switch (strtolower($command)) { |
|
| 128 | - // pass to Info |
|
| 129 | - case 'title': |
|
| 130 | - case 'description': |
|
| 131 | - case 'version': |
|
| 132 | - case 'terms': // alias |
|
| 133 | - case 'tos': // alias |
|
| 134 | - case 'termsofservice': |
|
| 135 | - case 'contact': |
|
| 136 | - case 'license': |
|
| 137 | - return $this->info->handleCommand($command, $data); |
|
| 138 | - |
|
| 139 | - // string[] |
|
| 140 | - case 'scheme': |
|
| 141 | - case 'schemes': |
|
| 142 | - $this->schemes = array_unique(array_merge($this->schemes, self::wordSplit($data))); |
|
| 143 | - |
|
| 144 | - return $this; |
|
| 145 | - |
|
| 146 | - // MIME[] |
|
| 147 | - case 'consume': |
|
| 148 | - case 'consumes': |
|
| 149 | - $this->consumes = array_merge($this->consumes, self::translateMimeTypes(self::wordSplit($data))); |
|
| 150 | - |
|
| 151 | - return $this; |
|
| 152 | - |
|
| 153 | - case 'produce': |
|
| 154 | - case 'produces': |
|
| 155 | - $this->produces = array_merge($this->produces, self::translateMimeTypes(self::wordSplit($data))); |
|
| 156 | - |
|
| 157 | - return $this; |
|
| 158 | - |
|
| 159 | - case 'model': |
|
| 160 | - case 'model!': |
|
| 161 | - case 'definition': |
|
| 162 | - case 'definition!': |
|
| 163 | - $name = self::wordShift($data); |
|
| 164 | - if (empty($name)) { |
|
| 165 | - throw new Exception('Missing definition name'); |
|
| 166 | - } |
|
| 167 | - $typeDef = self::wordShift($data); |
|
| 168 | - if (empty($typeDef)) { |
|
| 169 | - $typeDef = 'object'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $definition = new Schema($this, $typeDef); |
|
| 173 | - if (str_ends_with($command, '!')) { |
|
| 174 | - $definition->setReadOnly(); |
|
| 175 | - } |
|
| 176 | - $this->definitions[$name] = $definition; |
|
| 177 | - |
|
| 178 | - return $definition; |
|
| 179 | - |
|
| 180 | - case 'path': |
|
| 181 | - case 'query': |
|
| 182 | - case 'query?': |
|
| 183 | - case 'header': |
|
| 184 | - case 'header?': |
|
| 185 | - case 'form': |
|
| 186 | - case 'form?': |
|
| 187 | - $in = rtrim($command, '?'); |
|
| 188 | - $Parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
| 189 | - $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 190 | - |
|
| 191 | - return $Parameter; |
|
| 192 | - |
|
| 193 | - case 'body': |
|
| 194 | - case 'body?': |
|
| 195 | - $Parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
| 196 | - $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 197 | - |
|
| 198 | - return $Parameter; |
|
| 199 | - |
|
| 200 | - case 'response': |
|
| 201 | - $name = self::wordShift($data); |
|
| 202 | - $definition = self::wordShift($data); |
|
| 203 | - $description = $data; |
|
| 204 | - if (empty($description)) { |
|
| 205 | - throw new Exception('Response definition missing description'); |
|
| 206 | - } |
|
| 207 | - $Response = new Response($this, $name, $definition === 'null' ? null : $definition, $description); |
|
| 208 | - $this->responses[$name] = $Response; |
|
| 209 | - |
|
| 210 | - return $Response; |
|
| 211 | - |
|
| 212 | - case 'api': // alias |
|
| 213 | - case 'tag': |
|
| 214 | - $tagname = self::wordShift($data); |
|
| 215 | - if (empty($tagname)) { |
|
| 216 | - throw new Exception('Missing tag name'); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $Tag = null; |
|
| 220 | - foreach ($this->tags as $T) { |
|
| 221 | - if ($T->getName() === $tagname) { |
|
| 222 | - $Tag = $T; |
|
| 223 | - break; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - if (!$Tag) { |
|
| 227 | - $Tag = new Tag($this, $tagname, $data); |
|
| 228 | - $this->tags[] = $Tag; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // backwards compatibility |
|
| 232 | - if ($command === 'api') { |
|
| 233 | - $this->defaultTag = $Tag; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $Tag; |
|
| 237 | - |
|
| 238 | - case 'endpoint': |
|
| 239 | - $path = self::wordShift($data); |
|
| 240 | - if (false === $path) { |
|
| 241 | - $path = '/'; |
|
| 242 | - } else if ($path[0] !== '/') { |
|
| 243 | - $path = '/' . $path; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $Tag = null; |
|
| 247 | - if (($tagname = self::wordShift($data)) !== false) { |
|
| 248 | - foreach ($this->tags as $T) { |
|
| 249 | - if (strtolower($T->getName()) === strtolower($tagname)) { |
|
| 250 | - $Tag = $T; |
|
| 251 | - break; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - if (!$Tag) { |
|
| 255 | - $Tag = new Tag($this, $tagname, $data); |
|
| 256 | - $this->tags[] = $Tag; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - if (!isset($this->paths[$path])) { |
|
| 261 | - $this->paths[$path] = new Path($this, $Tag ?: $this->defaultTag); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return $this->paths[$path]; |
|
| 265 | - |
|
| 266 | - case 'security': |
|
| 267 | - $name = self::wordShift($data); |
|
| 268 | - if (empty($name)) { |
|
| 269 | - throw new Exception('Missing security name'); |
|
| 270 | - } |
|
| 271 | - $type = self::wordShift($data); |
|
| 272 | - if (empty($type)) { |
|
| 273 | - throw new Exception('Missing security type'); |
|
| 274 | - } |
|
| 275 | - $SecurityScheme = new SecurityScheme($this, $type, $data); |
|
| 276 | - $this->securityDefinitions[$name] = $SecurityScheme; |
|
| 277 | - |
|
| 278 | - return $SecurityScheme; |
|
| 279 | - |
|
| 280 | - case 'require': |
|
| 281 | - $name = self::wordShift($data); |
|
| 282 | - if (empty($name)) { |
|
| 283 | - throw new Exception('Missing require name'); |
|
| 284 | - } |
|
| 285 | - $scopes = self::wordSplit($data); |
|
| 286 | - sort($scopes); |
|
| 287 | - $this->security[] = [ |
|
| 288 | - $name => empty($scopes) ? [] : $scopes, |
|
| 289 | - ]; |
|
| 290 | - |
|
| 291 | - return $this; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return parent::handleCommand($command, $data); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @inheritDoc |
|
| 299 | - * @throws Exception |
|
| 300 | - * @throws Exception |
|
| 301 | - */ |
|
| 302 | - public function toArray(): array |
|
| 303 | - { |
|
| 304 | - if (empty($this->paths)) { |
|
| 305 | - throw new Exception('No path defined'); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $schemes = array_unique($this->schemes); |
|
| 309 | - sort($schemes); |
|
| 310 | - |
|
| 311 | - $consumes = array_unique($this->consumes); |
|
| 312 | - sort($consumes); |
|
| 313 | - |
|
| 314 | - $produces = array_unique($this->produces); |
|
| 315 | - sort($produces); |
|
| 316 | - |
|
| 317 | - foreach ($this->security as $security) { |
|
| 318 | - foreach ($security as $name => $scopes) { |
|
| 319 | - if (!isset($this->securityDefinitions[$name])) { |
|
| 320 | - throw new Exception('Required security scheme not defined: \'' . $name . '\''); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - return self::arrayFilterNull(array_merge([ |
|
| 326 | - 'swagger' => $this->swagger, |
|
| 327 | - 'info' => $this->info->toArray(), |
|
| 328 | - 'host' => empty($this->host) ? null : $this->host, |
|
| 329 | - 'basePath' => empty($this->basePath) ? null : $this->basePath, |
|
| 330 | - 'consumes' => $consumes, |
|
| 331 | - 'produces' => $produces, |
|
| 332 | - 'schemes' => $schemes, |
|
| 333 | - 'paths' => self::objectsToArray($this->paths), |
|
| 334 | - 'definitions' => self::objectsToArray($this->definitions), |
|
| 335 | - 'parameters' => self::objectsToArray($this->parameters), |
|
| 336 | - 'responses' => self::objectsToArray($this->responses), |
|
| 337 | - 'securityDefinitions' => self::objectsToArray($this->securityDefinitions), |
|
| 338 | - 'security' => $this->security, |
|
| 339 | - 'tags' => self::objectsToArray($this->tags), |
|
| 340 | - ], parent::toArray())); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - public function __toString() |
|
| 344 | - { |
|
| 345 | - return __CLASS__; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Check if an operation with the given id exists. |
|
| 350 | - * |
|
| 351 | - * @param string $operationId |
|
| 352 | - * |
|
| 353 | - * @return boolean |
|
| 354 | - */ |
|
| 355 | - public function hasOperationId($operationId): bool |
|
| 356 | - { |
|
| 357 | - foreach ($this->paths as $path) { |
|
| 358 | - if ($path->hasOperationId($operationId)) { |
|
| 359 | - return true; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - return false; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Check if a definition with the given name exists |
|
| 368 | - * |
|
| 369 | - * @param string $name |
|
| 370 | - * |
|
| 371 | - * @return boolean |
|
| 372 | - */ |
|
| 373 | - public function hasDefinition($name): bool |
|
| 374 | - { |
|
| 375 | - return isset($this->definitions[$name]); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * @inheritDoc |
|
| 380 | - */ |
|
| 381 | - protected function getSwagger(): Swagger |
|
| 382 | - { |
|
| 383 | - return $this; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * @inheritDoc |
|
| 388 | - */ |
|
| 389 | - protected function getTypeRegistry(): TypeRegistry |
|
| 390 | - { |
|
| 391 | - return $this->typeRegistry; |
|
| 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; |
|
| 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 | + * Return all consumes |
|
| 89 | + * |
|
| 90 | + * @return array |
|
| 91 | + * @todo Deprecate in favour of a getConsume($name); |
|
| 92 | + */ |
|
| 93 | + public function getConsumes(): array |
|
| 94 | + { |
|
| 95 | + return $this->consumes; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Return the named security if it exists, otherwise return FALSE |
|
| 100 | + * |
|
| 101 | + * @param string $name |
|
| 102 | + * |
|
| 103 | + * @return boolean|SecurityScheme |
|
| 104 | + */ |
|
| 105 | + public function getSecurity($name) |
|
| 106 | + { |
|
| 107 | + return $this->securityDefinitions[$name] ?? false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param string $command |
|
| 112 | + * @param string $data |
|
| 113 | + * |
|
| 114 | + * @return AbstractObject|boolean |
|
| 115 | + * @throws Exception |
|
| 116 | + * @throws Exception |
|
| 117 | + * @throws Exception |
|
| 118 | + * @throws Exception |
|
| 119 | + * @throws Exception |
|
| 120 | + * @throws Exception |
|
| 121 | + * @throws Exception |
|
| 122 | + * @throws Exception |
|
| 123 | + * @throws Exception |
|
| 124 | + */ |
|
| 125 | + public function handleCommand($command, $data = null) |
|
| 126 | + { |
|
| 127 | + switch (strtolower($command)) { |
|
| 128 | + // pass to Info |
|
| 129 | + case 'title': |
|
| 130 | + case 'description': |
|
| 131 | + case 'version': |
|
| 132 | + case 'terms': // alias |
|
| 133 | + case 'tos': // alias |
|
| 134 | + case 'termsofservice': |
|
| 135 | + case 'contact': |
|
| 136 | + case 'license': |
|
| 137 | + return $this->info->handleCommand($command, $data); |
|
| 138 | + |
|
| 139 | + // string[] |
|
| 140 | + case 'scheme': |
|
| 141 | + case 'schemes': |
|
| 142 | + $this->schemes = array_unique(array_merge($this->schemes, self::wordSplit($data))); |
|
| 143 | + |
|
| 144 | + return $this; |
|
| 145 | + |
|
| 146 | + // MIME[] |
|
| 147 | + case 'consume': |
|
| 148 | + case 'consumes': |
|
| 149 | + $this->consumes = array_merge($this->consumes, self::translateMimeTypes(self::wordSplit($data))); |
|
| 150 | + |
|
| 151 | + return $this; |
|
| 152 | + |
|
| 153 | + case 'produce': |
|
| 154 | + case 'produces': |
|
| 155 | + $this->produces = array_merge($this->produces, self::translateMimeTypes(self::wordSplit($data))); |
|
| 156 | + |
|
| 157 | + return $this; |
|
| 158 | + |
|
| 159 | + case 'model': |
|
| 160 | + case 'model!': |
|
| 161 | + case 'definition': |
|
| 162 | + case 'definition!': |
|
| 163 | + $name = self::wordShift($data); |
|
| 164 | + if (empty($name)) { |
|
| 165 | + throw new Exception('Missing definition name'); |
|
| 166 | + } |
|
| 167 | + $typeDef = self::wordShift($data); |
|
| 168 | + if (empty($typeDef)) { |
|
| 169 | + $typeDef = 'object'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $definition = new Schema($this, $typeDef); |
|
| 173 | + if (str_ends_with($command, '!')) { |
|
| 174 | + $definition->setReadOnly(); |
|
| 175 | + } |
|
| 176 | + $this->definitions[$name] = $definition; |
|
| 177 | + |
|
| 178 | + return $definition; |
|
| 179 | + |
|
| 180 | + case 'path': |
|
| 181 | + case 'query': |
|
| 182 | + case 'query?': |
|
| 183 | + case 'header': |
|
| 184 | + case 'header?': |
|
| 185 | + case 'form': |
|
| 186 | + case 'form?': |
|
| 187 | + $in = rtrim($command, '?'); |
|
| 188 | + $Parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
| 189 | + $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 190 | + |
|
| 191 | + return $Parameter; |
|
| 192 | + |
|
| 193 | + case 'body': |
|
| 194 | + case 'body?': |
|
| 195 | + $Parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
| 196 | + $this->parameters[$Parameter->getName()] = $Parameter; |
|
| 197 | + |
|
| 198 | + return $Parameter; |
|
| 199 | + |
|
| 200 | + case 'response': |
|
| 201 | + $name = self::wordShift($data); |
|
| 202 | + $definition = self::wordShift($data); |
|
| 203 | + $description = $data; |
|
| 204 | + if (empty($description)) { |
|
| 205 | + throw new Exception('Response definition missing description'); |
|
| 206 | + } |
|
| 207 | + $Response = new Response($this, $name, $definition === 'null' ? null : $definition, $description); |
|
| 208 | + $this->responses[$name] = $Response; |
|
| 209 | + |
|
| 210 | + return $Response; |
|
| 211 | + |
|
| 212 | + case 'api': // alias |
|
| 213 | + case 'tag': |
|
| 214 | + $tagname = self::wordShift($data); |
|
| 215 | + if (empty($tagname)) { |
|
| 216 | + throw new Exception('Missing tag name'); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $Tag = null; |
|
| 220 | + foreach ($this->tags as $T) { |
|
| 221 | + if ($T->getName() === $tagname) { |
|
| 222 | + $Tag = $T; |
|
| 223 | + break; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + if (!$Tag) { |
|
| 227 | + $Tag = new Tag($this, $tagname, $data); |
|
| 228 | + $this->tags[] = $Tag; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // backwards compatibility |
|
| 232 | + if ($command === 'api') { |
|
| 233 | + $this->defaultTag = $Tag; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return $Tag; |
|
| 237 | + |
|
| 238 | + case 'endpoint': |
|
| 239 | + $path = self::wordShift($data); |
|
| 240 | + if (false === $path) { |
|
| 241 | + $path = '/'; |
|
| 242 | + } else if ($path[0] !== '/') { |
|
| 243 | + $path = '/' . $path; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $Tag = null; |
|
| 247 | + if (($tagname = self::wordShift($data)) !== false) { |
|
| 248 | + foreach ($this->tags as $T) { |
|
| 249 | + if (strtolower($T->getName()) === strtolower($tagname)) { |
|
| 250 | + $Tag = $T; |
|
| 251 | + break; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + if (!$Tag) { |
|
| 255 | + $Tag = new Tag($this, $tagname, $data); |
|
| 256 | + $this->tags[] = $Tag; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + if (!isset($this->paths[$path])) { |
|
| 261 | + $this->paths[$path] = new Path($this, $Tag ?: $this->defaultTag); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return $this->paths[$path]; |
|
| 265 | + |
|
| 266 | + case 'security': |
|
| 267 | + $name = self::wordShift($data); |
|
| 268 | + if (empty($name)) { |
|
| 269 | + throw new Exception('Missing security name'); |
|
| 270 | + } |
|
| 271 | + $type = self::wordShift($data); |
|
| 272 | + if (empty($type)) { |
|
| 273 | + throw new Exception('Missing security type'); |
|
| 274 | + } |
|
| 275 | + $SecurityScheme = new SecurityScheme($this, $type, $data); |
|
| 276 | + $this->securityDefinitions[$name] = $SecurityScheme; |
|
| 277 | + |
|
| 278 | + return $SecurityScheme; |
|
| 279 | + |
|
| 280 | + case 'require': |
|
| 281 | + $name = self::wordShift($data); |
|
| 282 | + if (empty($name)) { |
|
| 283 | + throw new Exception('Missing require name'); |
|
| 284 | + } |
|
| 285 | + $scopes = self::wordSplit($data); |
|
| 286 | + sort($scopes); |
|
| 287 | + $this->security[] = [ |
|
| 288 | + $name => empty($scopes) ? [] : $scopes, |
|
| 289 | + ]; |
|
| 290 | + |
|
| 291 | + return $this; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return parent::handleCommand($command, $data); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @inheritDoc |
|
| 299 | + * @throws Exception |
|
| 300 | + * @throws Exception |
|
| 301 | + */ |
|
| 302 | + public function toArray(): array |
|
| 303 | + { |
|
| 304 | + if (empty($this->paths)) { |
|
| 305 | + throw new Exception('No path defined'); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $schemes = array_unique($this->schemes); |
|
| 309 | + sort($schemes); |
|
| 310 | + |
|
| 311 | + $consumes = array_unique($this->consumes); |
|
| 312 | + sort($consumes); |
|
| 313 | + |
|
| 314 | + $produces = array_unique($this->produces); |
|
| 315 | + sort($produces); |
|
| 316 | + |
|
| 317 | + foreach ($this->security as $security) { |
|
| 318 | + foreach ($security as $name => $scopes) { |
|
| 319 | + if (!isset($this->securityDefinitions[$name])) { |
|
| 320 | + throw new Exception('Required security scheme not defined: \'' . $name . '\''); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + return self::arrayFilterNull(array_merge([ |
|
| 326 | + 'swagger' => $this->swagger, |
|
| 327 | + 'info' => $this->info->toArray(), |
|
| 328 | + 'host' => empty($this->host) ? null : $this->host, |
|
| 329 | + 'basePath' => empty($this->basePath) ? null : $this->basePath, |
|
| 330 | + 'consumes' => $consumes, |
|
| 331 | + 'produces' => $produces, |
|
| 332 | + 'schemes' => $schemes, |
|
| 333 | + 'paths' => self::objectsToArray($this->paths), |
|
| 334 | + 'definitions' => self::objectsToArray($this->definitions), |
|
| 335 | + 'parameters' => self::objectsToArray($this->parameters), |
|
| 336 | + 'responses' => self::objectsToArray($this->responses), |
|
| 337 | + 'securityDefinitions' => self::objectsToArray($this->securityDefinitions), |
|
| 338 | + 'security' => $this->security, |
|
| 339 | + 'tags' => self::objectsToArray($this->tags), |
|
| 340 | + ], parent::toArray())); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + public function __toString() |
|
| 344 | + { |
|
| 345 | + return __CLASS__; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Check if an operation with the given id exists. |
|
| 350 | + * |
|
| 351 | + * @param string $operationId |
|
| 352 | + * |
|
| 353 | + * @return boolean |
|
| 354 | + */ |
|
| 355 | + public function hasOperationId($operationId): bool |
|
| 356 | + { |
|
| 357 | + foreach ($this->paths as $path) { |
|
| 358 | + if ($path->hasOperationId($operationId)) { |
|
| 359 | + return true; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + return false; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Check if a definition with the given name exists |
|
| 368 | + * |
|
| 369 | + * @param string $name |
|
| 370 | + * |
|
| 371 | + * @return boolean |
|
| 372 | + */ |
|
| 373 | + public function hasDefinition($name): bool |
|
| 374 | + { |
|
| 375 | + return isset($this->definitions[$name]); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * @inheritDoc |
|
| 380 | + */ |
|
| 381 | + protected function getSwagger(): Swagger |
|
| 382 | + { |
|
| 383 | + return $this; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * @inheritDoc |
|
| 388 | + */ |
|
| 389 | + protected function getTypeRegistry(): TypeRegistry |
|
| 390 | + { |
|
| 391 | + return $this->typeRegistry; |
|
| 392 | + } |
|
| 393 | 393 | |
| 394 | 394 | } |
@@ -15,48 +15,48 @@ |
||
| 15 | 15 | class ReferenceObjectType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - private $reference; |
|
| 19 | - |
|
| 20 | - public function toArray(): array |
|
| 21 | - { |
|
| 22 | - return self::arrayFilterNull(array_merge(array( |
|
| 23 | - '$ref' => '#/definitions/' . $this->reference, |
|
| 24 | - ), parent::toArray())); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function __toString() |
|
| 28 | - { |
|
| 29 | - return __CLASS__ . ' ' . $this->reference; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @throws Exception |
|
| 34 | - */ |
|
| 35 | - protected function parseDefinition($definition): void |
|
| 36 | - { |
|
| 37 | - $definition = self::trim($definition); |
|
| 38 | - |
|
| 39 | - $match = []; |
|
| 40 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 41 | - throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $type = strtolower($match[1]); |
|
| 45 | - |
|
| 46 | - $reference = null; |
|
| 47 | - if ($type === 'refobject') { |
|
| 48 | - if (isset($match[2])) { |
|
| 49 | - $reference = $match[2]; |
|
| 50 | - } |
|
| 51 | - } else { |
|
| 52 | - $reference = $match[1]; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - if (empty($reference)) { |
|
| 56 | - throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - $this->reference = $reference; |
|
| 60 | - } |
|
| 18 | + private $reference; |
|
| 19 | + |
|
| 20 | + public function toArray(): array |
|
| 21 | + { |
|
| 22 | + return self::arrayFilterNull(array_merge(array( |
|
| 23 | + '$ref' => '#/definitions/' . $this->reference, |
|
| 24 | + ), parent::toArray())); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function __toString() |
|
| 28 | + { |
|
| 29 | + return __CLASS__ . ' ' . $this->reference; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @throws Exception |
|
| 34 | + */ |
|
| 35 | + protected function parseDefinition($definition): void |
|
| 36 | + { |
|
| 37 | + $definition = self::trim($definition); |
|
| 38 | + |
|
| 39 | + $match = []; |
|
| 40 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 41 | + throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $type = strtolower($match[1]); |
|
| 45 | + |
|
| 46 | + $reference = null; |
|
| 47 | + if ($type === 'refobject') { |
|
| 48 | + if (isset($match[2])) { |
|
| 49 | + $reference = $match[2]; |
|
| 50 | + } |
|
| 51 | + } else { |
|
| 52 | + $reference = $match[1]; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + if (empty($reference)) { |
|
| 56 | + throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + $this->reference = $reference; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -15,286 +15,286 @@ |
||
| 15 | 15 | class ObjectType extends AbstractType |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | - private const REGEX_OBJECT_CONTENT = '(?:(\{)(.*)\})?'; |
|
| 20 | - public const REGEX_PROP_START = '/^'; |
|
| 21 | - private const REGEX_PROP_NAME = '([^?!:]+)'; |
|
| 22 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 23 | - private const REGEX_PROP_REQUIRED = '([\?!])?'; |
|
| 24 | - private const REGEX_PROP_ASSIGN = ':'; |
|
| 25 | - private const REGEX_PROP_DEFINITION = '(.+)'; |
|
| 26 | - /** @noinspection PhpRegExpUnsupportedModifierInspection |
|
| 27 | - * @noinspection PhpRegExpInvalidDelimiterInspection |
|
| 28 | - */ |
|
| 29 | - private const REGEX_PROP_ADDITIONAL = '\.\.\.(!|.+)?'; |
|
| 30 | - public const REGEX_PROP_END = '$/'; |
|
| 18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 19 | + private const REGEX_OBJECT_CONTENT = '(?:(\{)(.*)\})?'; |
|
| 20 | + public const REGEX_PROP_START = '/^'; |
|
| 21 | + private const REGEX_PROP_NAME = '([^?!:]+)'; |
|
| 22 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
| 23 | + private const REGEX_PROP_REQUIRED = '([\?!])?'; |
|
| 24 | + private const REGEX_PROP_ASSIGN = ':'; |
|
| 25 | + private const REGEX_PROP_DEFINITION = '(.+)'; |
|
| 26 | + /** @noinspection PhpRegExpUnsupportedModifierInspection |
|
| 27 | + * @noinspection PhpRegExpInvalidDelimiterInspection |
|
| 28 | + */ |
|
| 29 | + private const REGEX_PROP_ADDITIONAL = '\.\.\.(!|.+)?'; |
|
| 30 | + public const REGEX_PROP_END = '$/'; |
|
| 31 | 31 | |
| 32 | - private $minProperties; |
|
| 33 | - private $maxProperties; |
|
| 34 | - private $discriminator; |
|
| 35 | - private $additionalProperties; |
|
| 36 | - private $required = []; |
|
| 32 | + private $minProperties; |
|
| 33 | + private $maxProperties; |
|
| 34 | + private $discriminator; |
|
| 35 | + private $additionalProperties; |
|
| 36 | + private $required = []; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @var Property[] |
|
| 40 | - */ |
|
| 41 | - private $properties = []; |
|
| 38 | + /** |
|
| 39 | + * @var Property[] |
|
| 40 | + */ |
|
| 41 | + private $properties = []; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var Property |
|
| 45 | - */ |
|
| 46 | - private $mostRecentProperty; |
|
| 43 | + /** |
|
| 44 | + * @var Property |
|
| 45 | + */ |
|
| 46 | + private $mostRecentProperty; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @param string $command The comment command |
|
| 50 | - * @param string $data Any data added after the command |
|
| 51 | - * @return AbstractType|boolean |
|
| 52 | - * @throws Exception |
|
| 53 | - * @throws Exception |
|
| 54 | - * @throws Exception |
|
| 55 | - * @throws Exception |
|
| 56 | - * @throws Exception |
|
| 57 | - * @throws Exception |
|
| 58 | - * @throws Exception |
|
| 59 | - * @throws Exception |
|
| 60 | - * @throws Exception |
|
| 61 | - * @throws Exception |
|
| 62 | - * @throws Exception |
|
| 63 | - * @throws Exception |
|
| 64 | - */ |
|
| 65 | - public function handleCommand($command, $data = null) |
|
| 66 | - { |
|
| 67 | - switch (strtolower($command)) { |
|
| 68 | - // type name description... |
|
| 69 | - case 'additionalproperties': |
|
| 70 | - $value = self::wordShift($data); |
|
| 71 | - if (is_bool($value)) { |
|
| 72 | - $type = $value; |
|
| 73 | - } else if ($value === 'false') { |
|
| 74 | - $type = false; |
|
| 75 | - } else if ($value === 'true') { |
|
| 76 | - $type = true; |
|
| 77 | - } else { |
|
| 78 | - $type = self::typeFactory($this, $value, "Unparseable additional properties definition: '%s'"); |
|
| 79 | - } |
|
| 80 | - $this->setAdditionalProperties($type); |
|
| 81 | - return $this; |
|
| 82 | - case 'discriminator': |
|
| 83 | - $discriminator = self::wordShift($data); |
|
| 84 | - $this->setDiscriminator($discriminator); |
|
| 85 | - return $this; |
|
| 86 | - case 'property': |
|
| 87 | - case 'property?': |
|
| 88 | - case 'property!': |
|
| 89 | - $definition = self::wordShift($data); |
|
| 90 | - if (empty($definition)) { |
|
| 91 | - throw new Exception("Missing property definition"); |
|
| 92 | - } |
|
| 48 | + /** |
|
| 49 | + * @param string $command The comment command |
|
| 50 | + * @param string $data Any data added after the command |
|
| 51 | + * @return AbstractType|boolean |
|
| 52 | + * @throws Exception |
|
| 53 | + * @throws Exception |
|
| 54 | + * @throws Exception |
|
| 55 | + * @throws Exception |
|
| 56 | + * @throws Exception |
|
| 57 | + * @throws Exception |
|
| 58 | + * @throws Exception |
|
| 59 | + * @throws Exception |
|
| 60 | + * @throws Exception |
|
| 61 | + * @throws Exception |
|
| 62 | + * @throws Exception |
|
| 63 | + * @throws Exception |
|
| 64 | + */ |
|
| 65 | + public function handleCommand($command, $data = null) |
|
| 66 | + { |
|
| 67 | + switch (strtolower($command)) { |
|
| 68 | + // type name description... |
|
| 69 | + case 'additionalproperties': |
|
| 70 | + $value = self::wordShift($data); |
|
| 71 | + if (is_bool($value)) { |
|
| 72 | + $type = $value; |
|
| 73 | + } else if ($value === 'false') { |
|
| 74 | + $type = false; |
|
| 75 | + } else if ($value === 'true') { |
|
| 76 | + $type = true; |
|
| 77 | + } else { |
|
| 78 | + $type = self::typeFactory($this, $value, "Unparseable additional properties definition: '%s'"); |
|
| 79 | + } |
|
| 80 | + $this->setAdditionalProperties($type); |
|
| 81 | + return $this; |
|
| 82 | + case 'discriminator': |
|
| 83 | + $discriminator = self::wordShift($data); |
|
| 84 | + $this->setDiscriminator($discriminator); |
|
| 85 | + return $this; |
|
| 86 | + case 'property': |
|
| 87 | + case 'property?': |
|
| 88 | + case 'property!': |
|
| 89 | + $definition = self::wordShift($data); |
|
| 90 | + if (empty($definition)) { |
|
| 91 | + throw new Exception("Missing property definition"); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - $name = self::wordShift($data); |
|
| 95 | - if (empty($name)) { |
|
| 96 | - throw new Exception("Missing property name: '{$definition}'"); |
|
| 97 | - } |
|
| 94 | + $name = self::wordShift($data); |
|
| 95 | + if (empty($name)) { |
|
| 96 | + throw new Exception("Missing property name: '{$definition}'"); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - $readOnly = null; |
|
| 100 | - $required = false; |
|
| 101 | - $propertySuffix = substr($command, -1); |
|
| 102 | - if ($propertySuffix === '!') { |
|
| 103 | - $readOnly = true; |
|
| 104 | - } else if ($propertySuffix !== '?') { |
|
| 105 | - $required = true; |
|
| 106 | - } |
|
| 99 | + $readOnly = null; |
|
| 100 | + $required = false; |
|
| 101 | + $propertySuffix = substr($command, -1); |
|
| 102 | + if ($propertySuffix === '!') { |
|
| 103 | + $readOnly = true; |
|
| 104 | + } else if ($propertySuffix !== '?') { |
|
| 105 | + $required = true; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - if (($name === $this->discriminator) && !$required) { |
|
| 109 | - throw new Exception("Discriminator must be a required property, " |
|
| 110 | - . "property '{$name}' is not required"); |
|
| 111 | - } |
|
| 108 | + if (($name === $this->discriminator) && !$required) { |
|
| 109 | + throw new Exception("Discriminator must be a required property, " |
|
| 110 | + . "property '{$name}' is not required"); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - unset($this->required[$name]); |
|
| 114 | - if ($required) { |
|
| 115 | - $this->required[$name] = true; |
|
| 116 | - } |
|
| 113 | + unset($this->required[$name]); |
|
| 114 | + if ($required) { |
|
| 115 | + $this->required[$name] = true; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - $this->mostRecentProperty = new Property($this, $definition, $data, $readOnly); |
|
| 119 | - $this->properties[$name] = $this->mostRecentProperty; |
|
| 120 | - return $this; |
|
| 118 | + $this->mostRecentProperty = new Property($this, $definition, $data, $readOnly); |
|
| 119 | + $this->properties[$name] = $this->mostRecentProperty; |
|
| 120 | + return $this; |
|
| 121 | 121 | |
| 122 | - case 'min': |
|
| 123 | - $this->minProperties = (int)$data; |
|
| 124 | - if ($this->minProperties < 0) { |
|
| 125 | - throw new Exception("Minimum less than zero: '{$data}'"); |
|
| 126 | - } |
|
| 127 | - if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) { |
|
| 128 | - throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
| 129 | - } |
|
| 130 | - $this->minProperties = (int)$data; |
|
| 131 | - return $this; |
|
| 122 | + case 'min': |
|
| 123 | + $this->minProperties = (int)$data; |
|
| 124 | + if ($this->minProperties < 0) { |
|
| 125 | + throw new Exception("Minimum less than zero: '{$data}'"); |
|
| 126 | + } |
|
| 127 | + if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) { |
|
| 128 | + throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
| 129 | + } |
|
| 130 | + $this->minProperties = (int)$data; |
|
| 131 | + return $this; |
|
| 132 | 132 | |
| 133 | - case 'max': |
|
| 134 | - $this->maxProperties = (int)$data; |
|
| 135 | - if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) { |
|
| 136 | - throw new Exception("Maximum less than minimum: '{$data}'"); |
|
| 137 | - } |
|
| 138 | - if ($this->maxProperties < 0) { |
|
| 139 | - throw new Exception("Maximum less than zero: '{$data}'"); |
|
| 140 | - } |
|
| 141 | - return $this; |
|
| 142 | - } |
|
| 133 | + case 'max': |
|
| 134 | + $this->maxProperties = (int)$data; |
|
| 135 | + if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) { |
|
| 136 | + throw new Exception("Maximum less than minimum: '{$data}'"); |
|
| 137 | + } |
|
| 138 | + if ($this->maxProperties < 0) { |
|
| 139 | + throw new Exception("Maximum less than zero: '{$data}'"); |
|
| 140 | + } |
|
| 141 | + return $this; |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - // Pass through to most recent Property |
|
| 145 | - if ($this->mostRecentProperty && $this->mostRecentProperty->handleCommand($command, $data)) { |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 144 | + // Pass through to most recent Property |
|
| 145 | + if ($this->mostRecentProperty && $this->mostRecentProperty->handleCommand($command, $data)) { |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - return parent::handleCommand($command, $data); |
|
| 150 | - } |
|
| 149 | + return parent::handleCommand($command, $data); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * @param AbstractType|bool $type |
|
| 154 | - * @return void |
|
| 155 | - * @throws Exception |
|
| 156 | - */ |
|
| 157 | - private function setAdditionalProperties($type): void |
|
| 158 | - { |
|
| 159 | - if ($this->additionalProperties !== null) { |
|
| 160 | - throw new Exception('Additional properties may only be set once'); |
|
| 161 | - } |
|
| 162 | - $this->additionalProperties = $type; |
|
| 163 | - } |
|
| 152 | + /** |
|
| 153 | + * @param AbstractType|bool $type |
|
| 154 | + * @return void |
|
| 155 | + * @throws Exception |
|
| 156 | + */ |
|
| 157 | + private function setAdditionalProperties($type): void |
|
| 158 | + { |
|
| 159 | + if ($this->additionalProperties !== null) { |
|
| 160 | + throw new Exception('Additional properties may only be set once'); |
|
| 161 | + } |
|
| 162 | + $this->additionalProperties = $type; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * @param string|false $discriminator |
|
| 167 | - * @throws Exception |
|
| 168 | - * @throws Exception |
|
| 169 | - */ |
|
| 170 | - private function setDiscriminator($discriminator): void |
|
| 171 | - { |
|
| 172 | - if (!empty($this->discriminator)) { |
|
| 173 | - throw new Exception("Discriminator may only be set once, " |
|
| 174 | - . "trying to change it " |
|
| 175 | - . "from '{$this->discriminator}' " |
|
| 176 | - . "to '{$discriminator}'"); |
|
| 177 | - } |
|
| 178 | - if (isset($this->properties[$discriminator]) && empty($this->required[$discriminator])) { |
|
| 179 | - throw new Exception("Discriminator must be a required property, " |
|
| 180 | - . "property '{$discriminator}' is not required"); |
|
| 181 | - } |
|
| 182 | - $this->discriminator = $discriminator; |
|
| 183 | - } |
|
| 165 | + /** |
|
| 166 | + * @param string|false $discriminator |
|
| 167 | + * @throws Exception |
|
| 168 | + * @throws Exception |
|
| 169 | + */ |
|
| 170 | + private function setDiscriminator($discriminator): void |
|
| 171 | + { |
|
| 172 | + if (!empty($this->discriminator)) { |
|
| 173 | + throw new Exception("Discriminator may only be set once, " |
|
| 174 | + . "trying to change it " |
|
| 175 | + . "from '{$this->discriminator}' " |
|
| 176 | + . "to '{$discriminator}'"); |
|
| 177 | + } |
|
| 178 | + if (isset($this->properties[$discriminator]) && empty($this->required[$discriminator])) { |
|
| 179 | + throw new Exception("Discriminator must be a required property, " |
|
| 180 | + . "property '{$discriminator}' is not required"); |
|
| 181 | + } |
|
| 182 | + $this->discriminator = $discriminator; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - public function toArray(): array |
|
| 186 | - { |
|
| 187 | - return self::arrayFilterNull(array_merge(array( |
|
| 188 | - 'type' => 'object', |
|
| 189 | - 'required' => array_keys($this->required), |
|
| 190 | - 'properties' => self::objectsToArray($this->properties), |
|
| 191 | - 'minProperties' => $this->minProperties, |
|
| 192 | - 'maxProperties' => $this->maxProperties, |
|
| 193 | - 'discriminator' => $this->discriminator, |
|
| 194 | - 'additionalProperties' => $this->additionalProperties instanceof AbstractType ? $this->additionalProperties->toArray() : $this->additionalProperties, |
|
| 195 | - ), parent::toArray())); |
|
| 196 | - } |
|
| 185 | + public function toArray(): array |
|
| 186 | + { |
|
| 187 | + return self::arrayFilterNull(array_merge(array( |
|
| 188 | + 'type' => 'object', |
|
| 189 | + 'required' => array_keys($this->required), |
|
| 190 | + 'properties' => self::objectsToArray($this->properties), |
|
| 191 | + 'minProperties' => $this->minProperties, |
|
| 192 | + 'maxProperties' => $this->maxProperties, |
|
| 193 | + 'discriminator' => $this->discriminator, |
|
| 194 | + 'additionalProperties' => $this->additionalProperties instanceof AbstractType ? $this->additionalProperties->toArray() : $this->additionalProperties, |
|
| 195 | + ), parent::toArray())); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - public function __toString() |
|
| 199 | - { |
|
| 200 | - return __CLASS__; |
|
| 201 | - } |
|
| 198 | + public function __toString() |
|
| 199 | + { |
|
| 200 | + return __CLASS__; |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * @throws Exception |
|
| 205 | - */ |
|
| 206 | - protected function parseDefinition($definition): void |
|
| 207 | - { |
|
| 208 | - $definition = self::trim($definition); |
|
| 203 | + /** |
|
| 204 | + * @throws Exception |
|
| 205 | + */ |
|
| 206 | + protected function parseDefinition($definition): void |
|
| 207 | + { |
|
| 208 | + $definition = self::trim($definition); |
|
| 209 | 209 | |
| 210 | - $match = []; |
|
| 211 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 212 | - $match[1] = strtolower($match[1]); |
|
| 213 | - } elseif (preg_match(self::REGEX_START . self::REGEX_OBJECT_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 214 | - $match[1] = 'object'; |
|
| 215 | - } else { |
|
| 216 | - throw new Exception('Unparseable object definition: \'' . $definition . '\''); |
|
| 217 | - } |
|
| 210 | + $match = []; |
|
| 211 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 212 | + $match[1] = strtolower($match[1]); |
|
| 213 | + } elseif (preg_match(self::REGEX_START . self::REGEX_OBJECT_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
| 214 | + $match[1] = 'object'; |
|
| 215 | + } else { |
|
| 216 | + throw new Exception('Unparseable object definition: \'' . $definition . '\''); |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - $this->parseFormat($definition, $match); |
|
| 220 | - $this->parseProperties($definition, $match); |
|
| 221 | - $this->parseRange($definition, $match); |
|
| 222 | - } |
|
| 219 | + $this->parseFormat($definition, $match); |
|
| 220 | + $this->parseProperties($definition, $match); |
|
| 221 | + $this->parseRange($definition, $match); |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - /** |
|
| 225 | - * @param string $definition |
|
| 226 | - * @param string[] $match |
|
| 227 | - * @throws Exception |
|
| 228 | - */ |
|
| 229 | - private function parseFormat($definition, $match): void |
|
| 230 | - { |
|
| 231 | - if (strtolower($match[1]) !== 'object') { |
|
| 232 | - throw new Exception("Not an object: '{$definition}'"); |
|
| 233 | - } |
|
| 234 | - } |
|
| 224 | + /** |
|
| 225 | + * @param string $definition |
|
| 226 | + * @param string[] $match |
|
| 227 | + * @throws Exception |
|
| 228 | + */ |
|
| 229 | + private function parseFormat($definition, $match): void |
|
| 230 | + { |
|
| 231 | + if (strtolower($match[1]) !== 'object') { |
|
| 232 | + throw new Exception("Not an object: '{$definition}'"); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * @param string $definition |
|
| 238 | - * @param string[] $match |
|
| 239 | - * @throws Exception |
|
| 240 | - * @throws Exception |
|
| 241 | - * @throws Exception |
|
| 242 | - * @throws Exception |
|
| 243 | - * @throws Exception |
|
| 244 | - * @throws Exception |
|
| 245 | - */ |
|
| 246 | - private function parseProperties($definition, $match): void |
|
| 247 | - { |
|
| 248 | - if (empty($match[2])) { |
|
| 249 | - return; |
|
| 250 | - } |
|
| 236 | + /** |
|
| 237 | + * @param string $definition |
|
| 238 | + * @param string[] $match |
|
| 239 | + * @throws Exception |
|
| 240 | + * @throws Exception |
|
| 241 | + * @throws Exception |
|
| 242 | + * @throws Exception |
|
| 243 | + * @throws Exception |
|
| 244 | + * @throws Exception |
|
| 245 | + */ |
|
| 246 | + private function parseProperties($definition, $match): void |
|
| 247 | + { |
|
| 248 | + if (empty($match[2])) { |
|
| 249 | + return; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - while (($property = self::parseListItem($match[2])) !== '') { |
|
| 253 | - $prop_match = []; |
|
| 254 | - if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_ADDITIONAL . self::REGEX_PROP_END, $property, $prop_match) === 1) { |
|
| 255 | - if (empty($prop_match[1])) { |
|
| 256 | - $this->setAdditionalProperties(true); |
|
| 257 | - } else if ($prop_match[1] === '!') { |
|
| 258 | - $this->setAdditionalProperties(false); |
|
| 259 | - } else { |
|
| 260 | - $this->setAdditionalProperties(self::typeFactory($this, $prop_match[1], "Unparseable additional properties definition: '...%s'")); |
|
| 261 | - } |
|
| 262 | - continue; |
|
| 263 | - } |
|
| 264 | - if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_NAME . self::REGEX_PROP_REQUIRED . self::REGEX_PROP_ASSIGN . self::REGEX_PROP_DEFINITION . self::REGEX_PROP_END, $property, $prop_match) !== 1) { |
|
| 265 | - throw new Exception("Unparseable property definition: '{$property}'"); |
|
| 266 | - } |
|
| 267 | - $this->properties[$prop_match[1]] = new Property($this, $prop_match[3]); |
|
| 268 | - if ($prop_match[2] !== '!' && $prop_match[2] !== '?') { |
|
| 269 | - $this->required[$prop_match[1]] = true; |
|
| 270 | - } |
|
| 271 | - } |
|
| 252 | + while (($property = self::parseListItem($match[2])) !== '') { |
|
| 253 | + $prop_match = []; |
|
| 254 | + if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_ADDITIONAL . self::REGEX_PROP_END, $property, $prop_match) === 1) { |
|
| 255 | + if (empty($prop_match[1])) { |
|
| 256 | + $this->setAdditionalProperties(true); |
|
| 257 | + } else if ($prop_match[1] === '!') { |
|
| 258 | + $this->setAdditionalProperties(false); |
|
| 259 | + } else { |
|
| 260 | + $this->setAdditionalProperties(self::typeFactory($this, $prop_match[1], "Unparseable additional properties definition: '...%s'")); |
|
| 261 | + } |
|
| 262 | + continue; |
|
| 263 | + } |
|
| 264 | + if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_NAME . self::REGEX_PROP_REQUIRED . self::REGEX_PROP_ASSIGN . self::REGEX_PROP_DEFINITION . self::REGEX_PROP_END, $property, $prop_match) !== 1) { |
|
| 265 | + throw new Exception("Unparseable property definition: '{$property}'"); |
|
| 266 | + } |
|
| 267 | + $this->properties[$prop_match[1]] = new Property($this, $prop_match[3]); |
|
| 268 | + if ($prop_match[2] !== '!' && $prop_match[2] !== '?') { |
|
| 269 | + $this->required[$prop_match[1]] = true; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - } |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - /** |
|
| 276 | - * @param string $definition |
|
| 277 | - * @param string[] $match |
|
| 278 | - * @throws Exception |
|
| 279 | - */ |
|
| 280 | - private function parseRange($definition, $match): void |
|
| 281 | - { |
|
| 282 | - if (!empty($match[3])) { |
|
| 283 | - if ($match[4] === '' && $match[5] === '') { |
|
| 284 | - throw new Exception("Empty object range: '{$definition}'"); |
|
| 285 | - } |
|
| 275 | + /** |
|
| 276 | + * @param string $definition |
|
| 277 | + * @param string[] $match |
|
| 278 | + * @throws Exception |
|
| 279 | + */ |
|
| 280 | + private function parseRange($definition, $match): void |
|
| 281 | + { |
|
| 282 | + if (!empty($match[3])) { |
|
| 283 | + if ($match[4] === '' && $match[5] === '') { |
|
| 284 | + throw new Exception("Empty object range: '{$definition}'"); |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - $exclusiveMinimum = $match[3] === '<'; |
|
| 288 | - $this->minProperties = $match[4] === '' ? null : (int)$match[4]; |
|
| 289 | - $this->maxProperties = $match[5] === '' ? null : (int)$match[5]; |
|
| 290 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
| 291 | - if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) { |
|
| 292 | - self::swap($this->minProperties, $this->maxProperties); |
|
| 293 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
| 294 | - } |
|
| 295 | - $this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties); |
|
| 296 | - $this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties); |
|
| 297 | - } |
|
| 298 | - } |
|
| 287 | + $exclusiveMinimum = $match[3] === '<'; |
|
| 288 | + $this->minProperties = $match[4] === '' ? null : (int)$match[4]; |
|
| 289 | + $this->maxProperties = $match[5] === '' ? null : (int)$match[5]; |
|
| 290 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
| 291 | + if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) { |
|
| 292 | + self::swap($this->minProperties, $this->maxProperties); |
|
| 293 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
| 294 | + } |
|
| 295 | + $this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties); |
|
| 296 | + $this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties); |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | 300 | } |
@@ -120,18 +120,18 @@ discard block |
||
| 120 | 120 | return $this; |
| 121 | 121 | |
| 122 | 122 | case 'min': |
| 123 | - $this->minProperties = (int)$data; |
|
| 123 | + $this->minProperties = (int) $data; |
|
| 124 | 124 | if ($this->minProperties < 0) { |
| 125 | 125 | throw new Exception("Minimum less than zero: '{$data}'"); |
| 126 | 126 | } |
| 127 | 127 | if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) { |
| 128 | 128 | throw new Exception("Minimum greater than maximum: '{$data}'"); |
| 129 | 129 | } |
| 130 | - $this->minProperties = (int)$data; |
|
| 130 | + $this->minProperties = (int) $data; |
|
| 131 | 131 | return $this; |
| 132 | 132 | |
| 133 | 133 | case 'max': |
| 134 | - $this->maxProperties = (int)$data; |
|
| 134 | + $this->maxProperties = (int) $data; |
|
| 135 | 135 | if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) { |
| 136 | 136 | throw new Exception("Maximum less than minimum: '{$data}'"); |
| 137 | 137 | } |
@@ -285,8 +285,8 @@ discard block |
||
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | $exclusiveMinimum = $match[3] === '<'; |
| 288 | - $this->minProperties = $match[4] === '' ? null : (int)$match[4]; |
|
| 289 | - $this->maxProperties = $match[5] === '' ? null : (int)$match[5]; |
|
| 288 | + $this->minProperties = $match[4] === '' ? null : (int) $match[4]; |
|
| 289 | + $this->maxProperties = $match[5] === '' ? null : (int) $match[5]; |
|
| 290 | 290 | $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
| 291 | 291 | if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) { |
| 292 | 292 | self::swap($this->minProperties, $this->maxProperties); |
@@ -14,52 +14,52 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class AllOfType extends AbstractType |
| 16 | 16 | { |
| 17 | - private $allOfItems = []; |
|
| 18 | - private $mostRecentItem; |
|
| 17 | + private $allOfItems = []; |
|
| 18 | + private $mostRecentItem; |
|
| 19 | 19 | |
| 20 | - public function toArray(): array |
|
| 21 | - { |
|
| 22 | - $allOf = []; |
|
| 23 | - foreach ($this->allOfItems as $item) { |
|
| 24 | - $allOf[] = $item->toArray(); |
|
| 25 | - } |
|
| 26 | - return self::arrayFilterNull(array_merge(array( |
|
| 27 | - 'allOf' => $allOf, |
|
| 28 | - ), parent::toArray())); |
|
| 29 | - } |
|
| 20 | + public function toArray(): array |
|
| 21 | + { |
|
| 22 | + $allOf = []; |
|
| 23 | + foreach ($this->allOfItems as $item) { |
|
| 24 | + $allOf[] = $item->toArray(); |
|
| 25 | + } |
|
| 26 | + return self::arrayFilterNull(array_merge(array( |
|
| 27 | + 'allOf' => $allOf, |
|
| 28 | + ), parent::toArray())); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @throws Exception |
|
| 33 | - */ |
|
| 34 | - protected function parseDefinition($definition): void |
|
| 35 | - { |
|
| 36 | - $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
| 37 | - $inlineDef = ''; |
|
| 38 | - if (preg_match($pattern, $definition, $matches) |
|
| 39 | - && isset($matches[1])) { |
|
| 40 | - $inlineDef = $matches[1]; |
|
| 41 | - } |
|
| 42 | - if ($inlineDef) { |
|
| 43 | - foreach (self::parseList($inlineDef) as $item) { |
|
| 44 | - $this->handleCommand('item', $item); |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - } |
|
| 31 | + /** |
|
| 32 | + * @throws Exception |
|
| 33 | + */ |
|
| 34 | + protected function parseDefinition($definition): void |
|
| 35 | + { |
|
| 36 | + $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
| 37 | + $inlineDef = ''; |
|
| 38 | + if (preg_match($pattern, $definition, $matches) |
|
| 39 | + && isset($matches[1])) { |
|
| 40 | + $inlineDef = $matches[1]; |
|
| 41 | + } |
|
| 42 | + if ($inlineDef) { |
|
| 43 | + foreach (self::parseList($inlineDef) as $item) { |
|
| 44 | + $this->handleCommand('item', $item); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @throws Exception |
|
| 51 | - */ |
|
| 52 | - public function handleCommand($command, $data = null) |
|
| 53 | - { |
|
| 54 | - if (strtolower($command) === 'item') { |
|
| 55 | - $this->mostRecentItem = self::typeFactory($this, $data); |
|
| 56 | - $this->allOfItems[] = $this->mostRecentItem; |
|
| 57 | - return $this; |
|
| 58 | - } |
|
| 59 | - if (isset($this->mostRecentItem) |
|
| 60 | - && $this->mostRecentItem->handleCommand($command, $data)) { |
|
| 61 | - return $this; |
|
| 62 | - } |
|
| 63 | - return parent::handleCommand($command, $data); |
|
| 64 | - } |
|
| 49 | + /** |
|
| 50 | + * @throws Exception |
|
| 51 | + */ |
|
| 52 | + public function handleCommand($command, $data = null) |
|
| 53 | + { |
|
| 54 | + if (strtolower($command) === 'item') { |
|
| 55 | + $this->mostRecentItem = self::typeFactory($this, $data); |
|
| 56 | + $this->allOfItems[] = $this->mostRecentItem; |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | + if (isset($this->mostRecentItem) |
|
| 60 | + && $this->mostRecentItem->handleCommand($command, $data)) { |
|
| 61 | + return $this; |
|
| 62 | + } |
|
| 63 | + return parent::handleCommand($command, $data); |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -17,77 +17,77 @@ |
||
| 17 | 17 | class EmailType extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - public const PATTERN = '\A[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * List of formats recognized by this class |
|
| 24 | - * @var string[] |
|
| 25 | - */ |
|
| 26 | - private static $formats = array('email'); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Construct and set up the regular expression for this type |
|
| 30 | - * |
|
| 31 | - * @param AbstractObject $parent |
|
| 32 | - * @param string $definition |
|
| 33 | - */ |
|
| 34 | - public function __construct(AbstractObject $parent, $definition) |
|
| 35 | - { |
|
| 36 | - $this->pattern = self::PATTERN; |
|
| 37 | - |
|
| 38 | - parent::__construct($parent, $definition); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public static function getFormats(): array |
|
| 42 | - { |
|
| 43 | - return self::$formats; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function setFormats(array $formats): void |
|
| 47 | - { |
|
| 48 | - self::$formats = $formats; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Parse a type definition string, assuming it belongs to this type |
|
| 53 | - * |
|
| 54 | - * @param string $definition |
|
| 55 | - * @throws Exception |
|
| 56 | - */ |
|
| 57 | - protected function parseDefinition($definition): void |
|
| 58 | - { |
|
| 59 | - $definition = self::trim($definition); |
|
| 60 | - |
|
| 61 | - $match = []; |
|
| 62 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | - throw new Exception("Unparseable email definition: '{$definition}'"); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | - throw new Exception("Not an email: '{$definition}'"); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Check (and optionally reformat) a default value |
|
| 75 | - * |
|
| 76 | - * @param string $value |
|
| 77 | - * @return string |
|
| 78 | - * @throws Exception |
|
| 79 | - */ |
|
| 80 | - protected function validateDefault($value): string |
|
| 81 | - { |
|
| 82 | - if (empty($value)) { |
|
| 83 | - throw new Exception("Empty email default"); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { |
|
| 87 | - throw new Exception("Invalid email default value: '{$value}'"); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $value; |
|
| 91 | - } |
|
| 20 | + public const PATTERN = '\A[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * List of formats recognized by this class |
|
| 24 | + * @var string[] |
|
| 25 | + */ |
|
| 26 | + private static $formats = array('email'); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Construct and set up the regular expression for this type |
|
| 30 | + * |
|
| 31 | + * @param AbstractObject $parent |
|
| 32 | + * @param string $definition |
|
| 33 | + */ |
|
| 34 | + public function __construct(AbstractObject $parent, $definition) |
|
| 35 | + { |
|
| 36 | + $this->pattern = self::PATTERN; |
|
| 37 | + |
|
| 38 | + parent::__construct($parent, $definition); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public static function getFormats(): array |
|
| 42 | + { |
|
| 43 | + return self::$formats; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function setFormats(array $formats): void |
|
| 47 | + { |
|
| 48 | + self::$formats = $formats; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Parse a type definition string, assuming it belongs to this type |
|
| 53 | + * |
|
| 54 | + * @param string $definition |
|
| 55 | + * @throws Exception |
|
| 56 | + */ |
|
| 57 | + protected function parseDefinition($definition): void |
|
| 58 | + { |
|
| 59 | + $definition = self::trim($definition); |
|
| 60 | + |
|
| 61 | + $match = []; |
|
| 62 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 63 | + throw new Exception("Unparseable email definition: '{$definition}'"); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 67 | + throw new Exception("Not an email: '{$definition}'"); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Check (and optionally reformat) a default value |
|
| 75 | + * |
|
| 76 | + * @param string $value |
|
| 77 | + * @return string |
|
| 78 | + * @throws Exception |
|
| 79 | + */ |
|
| 80 | + protected function validateDefault($value): string |
|
| 81 | + { |
|
| 82 | + if (empty($value)) { |
|
| 83 | + throw new Exception("Empty email default"); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { |
|
| 87 | + throw new Exception("Invalid email default value: '{$value}'"); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $value; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -16,77 +16,77 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class MacType extends StringType implements ICustomType |
| 18 | 18 | { |
| 19 | - public const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$'; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * List of formats recognized by this class |
|
| 23 | - * @var string[] |
|
| 24 | - */ |
|
| 25 | - private static $formats = array('mac'); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Construct and set up the regular expression for this type |
|
| 29 | - * |
|
| 30 | - * @param AbstractObject $parent |
|
| 31 | - * @param string $definition |
|
| 32 | - */ |
|
| 33 | - public function __construct(AbstractObject $parent, $definition) |
|
| 34 | - { |
|
| 35 | - $this->pattern = self::PATTERN; |
|
| 36 | - |
|
| 37 | - parent::__construct($parent, $definition); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public static function getFormats(): array |
|
| 41 | - { |
|
| 42 | - return self::$formats; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public static function setFormats(array $formats): void |
|
| 46 | - { |
|
| 47 | - self::$formats = $formats; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Parse a type definition string, assuming it belongs to this type |
|
| 52 | - * |
|
| 53 | - * @param string $definition |
|
| 54 | - * @throws Exception |
|
| 55 | - */ |
|
| 56 | - protected function parseDefinition($definition): void |
|
| 57 | - { |
|
| 58 | - $definition = self::trim($definition); |
|
| 59 | - |
|
| 60 | - $match = []; |
|
| 61 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 62 | - throw new Exception("Unparseable MAC definition: '{$definition}'"); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 66 | - throw new Exception("Not a MAC: '{$definition}'"); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Check (and optionally reformat) a default value |
|
| 74 | - * |
|
| 75 | - * @param string $value |
|
| 76 | - * @return string |
|
| 77 | - * @throws Exception |
|
| 78 | - */ |
|
| 79 | - protected function validateDefault($value): string |
|
| 80 | - { |
|
| 81 | - if (empty($value)) { |
|
| 82 | - throw new Exception("Empty MAC default"); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) { |
|
| 86 | - throw new Exception("Invalid MAC default value: '{$value}'"); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return $value; |
|
| 90 | - } |
|
| 19 | + public const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$'; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * List of formats recognized by this class |
|
| 23 | + * @var string[] |
|
| 24 | + */ |
|
| 25 | + private static $formats = array('mac'); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Construct and set up the regular expression for this type |
|
| 29 | + * |
|
| 30 | + * @param AbstractObject $parent |
|
| 31 | + * @param string $definition |
|
| 32 | + */ |
|
| 33 | + public function __construct(AbstractObject $parent, $definition) |
|
| 34 | + { |
|
| 35 | + $this->pattern = self::PATTERN; |
|
| 36 | + |
|
| 37 | + parent::__construct($parent, $definition); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public static function getFormats(): array |
|
| 41 | + { |
|
| 42 | + return self::$formats; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public static function setFormats(array $formats): void |
|
| 46 | + { |
|
| 47 | + self::$formats = $formats; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Parse a type definition string, assuming it belongs to this type |
|
| 52 | + * |
|
| 53 | + * @param string $definition |
|
| 54 | + * @throws Exception |
|
| 55 | + */ |
|
| 56 | + protected function parseDefinition($definition): void |
|
| 57 | + { |
|
| 58 | + $definition = self::trim($definition); |
|
| 59 | + |
|
| 60 | + $match = []; |
|
| 61 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
| 62 | + throw new Exception("Unparseable MAC definition: '{$definition}'"); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if (!in_array(strtolower($match[1]), self::$formats)) { |
|
| 66 | + throw new Exception("Not a MAC: '{$definition}'"); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Check (and optionally reformat) a default value |
|
| 74 | + * |
|
| 75 | + * @param string $value |
|
| 76 | + * @return string |
|
| 77 | + * @throws Exception |
|
| 78 | + */ |
|
| 79 | + protected function validateDefault($value): string |
|
| 80 | + { |
|
| 81 | + if (empty($value)) { |
|
| 82 | + throw new Exception("Empty MAC default"); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) { |
|
| 86 | + throw new Exception("Invalid MAC default value: '{$value}'"); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return $value; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | } |