1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\router\schema;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use core\param;
|
|
|
21 |
use core\router\route;
|
|
|
22 |
use core\router\schema\objects\type_base;
|
|
|
23 |
use stdClass;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* OpenAPI parameter.
|
|
|
27 |
*
|
|
|
28 |
* https://spec.openapis.org/oas/v3.1.0#parameter-object
|
|
|
29 |
*
|
|
|
30 |
* @package core
|
|
|
31 |
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class parameter extends openapi_base {
|
|
|
35 |
/** @var string A query parameter */
|
|
|
36 |
public const IN_QUERY = 'query';
|
|
|
37 |
|
|
|
38 |
/** @var string A header parameter */
|
|
|
39 |
public const IN_HEADER = 'header';
|
|
|
40 |
|
|
|
41 |
/** @var string A URI path parameter */
|
|
|
42 |
public const IN_PATH = 'path';
|
|
|
43 |
|
|
|
44 |
/** @var string A cookie parameter */
|
|
|
45 |
public const IN_COOKIE = 'cookie';
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Constructor for a Parameter Object.
|
|
|
49 |
*
|
|
|
50 |
* @param string $name The name of the parameter. Parameter names are case sensitive.
|
|
|
51 |
* - If in is "path", the name field MUST correspond to a template expression occurring within the
|
|
|
52 |
* path field in the Paths Object.
|
|
|
53 |
* See Path Templating for further information.
|
|
|
54 |
* - If in is "header" and the name field is "Accept", "Content-Type" or "Authorization",
|
|
|
55 |
* the parameter definition SHALL be ignored.
|
|
|
56 |
* - For all other cases, the name corresponds to the parameter name used by the in property.
|
|
|
57 |
* @param string $in The location of the parameter. Possible values are "query", "header", "path" or "cookie".
|
|
|
58 |
* @param null|string $description
|
|
|
59 |
* @param null|bool $required
|
|
|
60 |
* @param null|bool $deprecated Specifies that a parameter is deprecated and SHOULD be transitioned out of usage.
|
|
|
61 |
* @param null|param $type A Moodle parameter type, which can be used instead of a schema.
|
|
|
62 |
* @param mixed $default The default value
|
|
|
63 |
* @param null|type_base $schema
|
|
|
64 |
* @param null|example $example
|
|
|
65 |
* @param example[] $examples
|
|
|
66 |
* @param mixed[] ...$extra
|
|
|
67 |
* @throws coding_exception
|
|
|
68 |
*/
|
|
|
69 |
public function __construct(
|
|
|
70 |
/** @var string The name of the parameter. Parameter names are case sensitive */
|
|
|
71 |
protected string $name,
|
|
|
72 |
/** @var string The location of the parameter */
|
|
|
73 |
protected string $in,
|
|
|
74 |
/** @var string|null A description of the parameter */
|
|
|
75 |
protected ?string $description = null,
|
|
|
76 |
/** @var bool|null Whether the parameter is required */
|
|
|
77 |
protected ?bool $required = null,
|
|
|
78 |
/** @var bool|null Whether the parameter is deprecated */
|
|
|
79 |
protected ?bool $deprecated = false,
|
|
|
80 |
/** @var param|null A Moodle parameter type */
|
|
|
81 |
protected ?param $type = null,
|
|
|
82 |
/** @var mixed|null The default value of the parameter */
|
|
|
83 |
protected mixed $default = null,
|
|
|
84 |
/** @var type_base|null The schema */
|
|
|
85 |
protected ?type_base $schema = null,
|
|
|
86 |
/** @var example|null An example */
|
|
|
87 |
protected ?example $example = null,
|
|
|
88 |
/** @var example[] An array of examples */
|
|
|
89 |
protected array $examples = [],
|
|
|
90 |
...$extra,
|
|
|
91 |
) {
|
|
|
92 |
if ($example) {
|
|
|
93 |
if (count($examples)) {
|
|
|
94 |
throw new coding_exception('Only one of example or examples can be specified.');
|
|
|
95 |
}
|
|
|
96 |
$this->examples[$example->get_name()] = $example;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if ($required === true && $default !== null) {
|
|
|
100 |
throw new coding_exception('A parameter cannot be required and have a default value.');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
parent::__construct(...$extra);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
#[\Override]
|
|
|
107 |
public function get_openapi_description(
|
|
|
108 |
specification $api,
|
|
|
109 |
?string $path = null,
|
|
|
110 |
): ?stdClass {
|
|
|
111 |
$data = (object) [
|
|
|
112 |
// The `name`, and `in` values are required.
|
|
|
113 |
'name' => $this->name,
|
|
|
114 |
'in' => $this->in,
|
|
|
115 |
];
|
|
|
116 |
|
|
|
117 |
if ($this->description !== null) {
|
|
|
118 |
$data->description = $this->description;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Allow another schema to be passed.
|
|
|
122 |
if ($this->schema !== null) {
|
|
|
123 |
$data->schema = $this->schema->get_openapi_schema($api, $path);
|
|
|
124 |
} else {
|
|
|
125 |
$data->schema = $this->get_schema_from_type($this->type);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if (count($this->examples) > 0) {
|
|
|
129 |
$data->examples = [];
|
|
|
130 |
foreach ($this->examples as $example) {
|
|
|
131 |
$data->examples[$example->get_name()] = $example->get_openapi_schema(
|
|
|
132 |
api: $api,
|
|
|
133 |
);
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
return $data;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get the OpenAPI 'in' property.
|
|
|
142 |
*
|
|
|
143 |
* @return string
|
|
|
144 |
*/
|
|
|
145 |
public function get_in(): string {
|
|
|
146 |
return $this->in;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Fetch the underlying param.
|
|
|
151 |
*
|
|
|
152 |
* @return param
|
|
|
153 |
*/
|
|
|
154 |
public function get_type(): param {
|
|
|
155 |
return $this->type;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Whether this property is required.
|
|
|
160 |
*
|
|
|
161 |
* @param route $route
|
|
|
162 |
* @return bool
|
|
|
163 |
*/
|
|
|
164 |
public function is_required(route $route): bool {
|
|
|
165 |
return $this->required ?? false;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Get the name of the parameter.
|
|
|
170 |
*
|
|
|
171 |
* @return string
|
|
|
172 |
*/
|
|
|
173 |
public function get_name(): string {
|
|
|
174 |
return $this->name;
|
|
|
175 |
}
|
|
|
176 |
}
|