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\router\schema\response\content\media_type;
|
|
|
20 |
use core\router\schema\response\content\payload_response_type;
|
|
|
21 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Routing request body for validation.
|
|
|
25 |
*
|
|
|
26 |
* https://spec.openapis.org/oas/v3.1.0#request-body-object
|
|
|
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 request_body extends openapi_base {
|
|
|
33 |
/**
|
|
|
34 |
* Create a new request body.
|
|
|
35 |
*
|
|
|
36 |
* @param string $description A brief description of the request body.
|
|
|
37 |
* @param payload_response_type|payload_response_type[] $content The content of the request body.
|
|
|
38 |
* @param bool $required Whether the request body is required
|
|
|
39 |
* @param mixed ...$args Extra args for future compatibility.
|
|
|
40 |
* @throws \coding_exception if the content is not an instance of media_type.
|
|
|
41 |
*/
|
|
|
42 |
public function __construct(
|
|
|
43 |
/**
|
|
|
44 |
* A brief description of the request body.
|
|
|
45 |
*
|
|
|
46 |
* This could contain examples of use. CommonMark syntax MAY be used for rich text representation.
|
|
|
47 |
* @var string
|
|
|
48 |
*/
|
|
|
49 |
protected string $description = '',
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* The content of the request body.
|
|
|
53 |
*
|
|
|
54 |
* @var payload_response_type|media_type[]
|
|
|
55 |
*/
|
|
|
56 |
protected array|payload_response_type $content = [],
|
|
|
57 |
|
|
|
58 |
/** @var bool Whether the request body is required */
|
|
|
59 |
protected bool $required = false,
|
|
|
60 |
...$args,
|
|
|
61 |
) {
|
|
|
62 |
if (!empty($content)) {
|
|
|
63 |
if (is_array($content)) {
|
|
|
64 |
foreach ($content as $contentitem) {
|
|
|
65 |
if (!($contentitem instanceof media_type)) {
|
|
|
66 |
throw new \coding_exception('Content must be an instance of media_type.');
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
parent::__construct(...$args);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
#[\Override]
|
|
|
75 |
public function get_openapi_description(
|
|
|
76 |
specification $api,
|
|
|
77 |
?string $path = null,
|
|
|
78 |
): ?\stdClass {
|
|
|
79 |
$data = (object) [
|
|
|
80 |
'description' => $this->description,
|
|
|
81 |
'required' => $this->required,
|
|
|
82 |
'content' => [],
|
|
|
83 |
];
|
|
|
84 |
|
|
|
85 |
if ($this->content instanceof response\content\payload_response_type) {
|
|
|
86 |
$data->content = $this->content->get_openapi_schema(
|
|
|
87 |
api: $api,
|
|
|
88 |
);
|
|
|
89 |
return $data;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
foreach ($this->content as $content) {
|
|
|
93 |
$data->content[$content->get_encoding()] = $content->get_openapi_schema(
|
|
|
94 |
api: $api,
|
|
|
95 |
);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return $data;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Get the relevant body for the specified request.
|
|
|
103 |
*
|
|
|
104 |
* Request bodies can be different for different content-types, as noted in the request.
|
|
|
105 |
*
|
|
|
106 |
* @param ServerRequestInterface $request
|
|
|
107 |
* @return media_type
|
|
|
108 |
* @throws \invalid_parameter_exception
|
|
|
109 |
*/
|
|
|
110 |
public function get_body_for_request(
|
|
|
111 |
ServerRequestInterface $request,
|
|
|
112 |
): media_type {
|
|
|
113 |
if ($this->content instanceof payload_response_type) {
|
|
|
114 |
$content = $this->content->get_media_type_instance(
|
|
|
115 |
mimetype: $request->getHeaderLine('Content-Type'),
|
|
|
116 |
required: $this->is_required(),
|
|
|
117 |
);
|
|
|
118 |
|
|
|
119 |
if ($content) {
|
|
|
120 |
return $content;
|
|
|
121 |
}
|
|
|
122 |
} else {
|
|
|
123 |
foreach ($this->content as $content) {
|
|
|
124 |
if ($content::get_encoding() === $request->getHeaderLine('Content-Type')) {
|
|
|
125 |
return $content;
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
throw new \invalid_parameter_exception('No matching content type found.');
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Whether this query parameter is required.
|
|
|
135 |
*
|
|
|
136 |
* @return bool
|
|
|
137 |
*/
|
|
|
138 |
public function is_required(): bool {
|
|
|
139 |
return $this->required;
|
|
|
140 |
}
|
|
|
141 |
}
|