1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\ApiDescriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\ApiDescriptions\Description\Standard\Raml; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\ApiDescriptions\Description\Operation; |
12
|
|
|
use KleijnWeb\ApiDescriptions\Description\Parameter; |
13
|
|
|
use KleijnWeb\ApiDescriptions\Description\Schema; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author John Kleijn <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class RamlOperation extends Operation |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Operation constructor. |
22
|
|
|
* |
23
|
|
|
* @param \stdClass $definition |
24
|
|
|
* @param string $path |
25
|
|
|
* @param string $method |
26
|
|
|
* @param array $pathParameters |
27
|
|
|
*/ |
28
|
|
|
public function __construct(\stdClass $definition, string $path, string $method, array $pathParameters = []) |
29
|
|
|
{ |
30
|
|
|
$this->path = $path; |
31
|
|
|
$this->method = $method; |
32
|
|
|
$this->parameters = array_merge($pathParameters, self::extractParameters($definition)); |
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
if (isset($definition->responses)) { |
36
|
|
|
$hasOkResponse = false; |
37
|
|
|
foreach ($definition->responses as $code => $responseDefinition) { |
38
|
|
|
$code = (int)$code; |
39
|
|
|
$this->responses[$code] = new RamlResponse($code, $responseDefinition); |
40
|
|
|
} |
41
|
|
|
if (!$hasOkResponse) { |
42
|
|
|
$this->responses[200] = new RamlResponse(200, (object)[]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$schemaDefinition = (object)[]; |
47
|
|
View Code Duplication |
if (!count($this->parameters)) { |
|
|
|
|
48
|
|
|
$schemaDefinition->type = 'null'; |
49
|
|
|
$this->requestSchema = Schema::get($schemaDefinition); |
50
|
|
|
} else { |
51
|
|
|
$schemaDefinition->type = 'object'; |
52
|
|
|
$schemaDefinition->required = []; |
53
|
|
|
$schemaDefinition->properties = (object)[]; |
54
|
|
|
|
55
|
|
|
foreach ($this->parameters as $parameter) { |
56
|
|
|
if ($parameter->isRequired()) { |
57
|
|
|
$schemaDefinition->required[] = $parameter->getName(); |
58
|
|
|
} |
59
|
|
|
$schemaDefinition->properties->{$parameter->getName()} = $parameter->getSchema()->getDefinition(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->requestSchema = Schema::get($schemaDefinition); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \stdClass $definition |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public static function extractParameters(\stdClass $definition) |
72
|
|
|
{ |
73
|
|
|
$parameters = []; |
74
|
|
|
|
75
|
|
View Code Duplication |
if (isset($definition->queryParameters)) { |
|
|
|
|
76
|
|
|
foreach ($definition->queryParameters as $name => $parameterDefinition) { |
77
|
|
|
$parameters[] = new RamlParameter($name, Parameter::IN_QUERY, $parameterDefinition); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
View Code Duplication |
if (isset($definition->uriParameters)) { |
|
|
|
|
81
|
|
|
foreach ($definition->uriParameters as $name => $parameterDefinition) { |
82
|
|
|
$parameters[] = new RamlParameter($name, Parameter::IN_PATH, $parameterDefinition); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $parameters; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..