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\parameters;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use core\param;
|
|
|
21 |
use core\router\schema\parameter;
|
|
|
22 |
use core\router\schema\specification;
|
|
|
23 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Routing query parameter for validation.
|
|
|
27 |
*
|
|
|
28 |
* @package core
|
|
|
29 |
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class query_parameter extends parameter {
|
|
|
33 |
/**
|
|
|
34 |
* Query parameter constructor to override the location of the parameter.
|
|
|
35 |
*
|
|
|
36 |
* @param bool|null $allowreserved Determines whether the parameter value SHOULD allow reserved characters.
|
|
|
37 |
* @param array ...$extra
|
|
|
38 |
*/
|
|
|
39 |
public function __construct(
|
|
|
40 |
/**
|
|
|
41 |
* Determines whether the parameter value SHOULD allow reserved characters.
|
|
|
42 |
*
|
|
|
43 |
* As defined by [RFC3986], these characters are :/?#[]@!$&'()*+,;= to be included without percent-encoding.
|
|
|
44 |
* This property only applies to parameters with an in value of query. The default value is false.
|
|
|
45 |
*
|
|
|
46 |
* @var bool|null
|
|
|
47 |
*/
|
|
|
48 |
protected ?bool $allowreserved = null,
|
|
|
49 |
...$extra,
|
|
|
50 |
) {
|
|
|
51 |
$extra['in'] = parameter::IN_QUERY;
|
|
|
52 |
parent::__construct(...$extra);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Validate query parameters.
|
|
|
57 |
*
|
|
|
58 |
* @param ServerRequestInterface $request
|
|
|
59 |
* @param array $params
|
|
|
60 |
* @return ServerRequestInterface
|
|
|
61 |
* @throws coding_exception
|
|
|
62 |
* @throws \ValueError
|
|
|
63 |
*/
|
|
|
64 |
public function validate(
|
|
|
65 |
ServerRequestInterface $request,
|
|
|
66 |
array $params,
|
|
|
67 |
): ServerRequestInterface {
|
|
|
68 |
if (array_key_exists($this->name, $params)) {
|
|
|
69 |
// This parameter was specified. Validate it.
|
|
|
70 |
if ($this->get_type() === param::BOOL) {
|
|
|
71 |
match ($params[$this->name]) {
|
|
|
72 |
'true' => $params[$this->name] = 1,
|
|
|
73 |
'false' => $params[$this->name] = 0,
|
|
|
74 |
default => throw new \ValueError('Invalid boolean value.'),
|
|
|
75 |
};
|
|
|
76 |
}
|
|
|
77 |
$this->type->validate_param($params[$this->name]);
|
|
|
78 |
|
|
|
79 |
return $this->update_request_params(
|
|
|
80 |
$request,
|
|
|
81 |
array_merge(
|
|
|
82 |
$params,
|
|
|
83 |
[$this->name => $params[$this->name]],
|
|
|
84 |
),
|
|
|
85 |
);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if ($this->required) {
|
|
|
89 |
throw new coding_exception(
|
|
|
90 |
"A required parameter {$this->name} was not provided and must be specified",
|
|
|
91 |
);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if ($this->default !== null) {
|
|
|
95 |
// This parameter is optional. Fill the default.
|
|
|
96 |
return $this->update_request_params(
|
|
|
97 |
$request,
|
|
|
98 |
array_merge(
|
|
|
99 |
$params,
|
|
|
100 |
[$this->name => $this->default],
|
|
|
101 |
),
|
|
|
102 |
);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
// This parameter is optional and there is no default.
|
|
|
106 |
// Fill a null value.
|
|
|
107 |
return $this->update_request_params(
|
|
|
108 |
$request,
|
|
|
109 |
array_merge(
|
|
|
110 |
$params,
|
|
|
111 |
[$this->name => null],
|
|
|
112 |
),
|
|
|
113 |
);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Update the request parameters.
|
|
|
118 |
*
|
|
|
119 |
* @param ServerRequestInterface $request
|
|
|
120 |
* @param array $params
|
|
|
121 |
* @return ServerRequestInterface
|
|
|
122 |
*/
|
|
|
123 |
protected function update_request_params(
|
|
|
124 |
ServerRequestInterface $request,
|
|
|
125 |
array $params,
|
|
|
126 |
): ServerRequestInterface {
|
|
|
127 |
return $request->withQueryParams($params);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
#[\Override]
|
|
|
131 |
final public function get_openapi_description(
|
|
|
132 |
specification $api,
|
|
|
133 |
?string $path = null,
|
|
|
134 |
): ?\stdClass {
|
|
|
135 |
$data = parent::get_openapi_description($api, $path);
|
|
|
136 |
|
|
|
137 |
if ($this->allowreserved) {
|
|
|
138 |
// Determines whether the parameter value SHOULD allow reserved characters, as defined by [RFC3986]
|
|
|
139 |
// :/?#[]@!$&'()*+,;=
|
|
|
140 |
// to be included without percent-encoding.
|
|
|
141 |
// This property only applies to parameters with an in value of query. The default value is false.
|
|
|
142 |
$data->allowReserved = $this->allowreserved;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
return $data;
|
|
|
146 |
}
|
|
|
147 |
}
|