Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\response;
18
 
19
use core\exception\coding_exception;
20
use core\router\schema\openapi_base;
21
use core\router\schema\response\content\media_type;
22
use core\router\schema\specification;
23
use core\router\schema\response\content\payload_response_type;
24
use Psr\Http\Message\ResponseInterface;
25
 
26
/**
27
 * An OpenAPI Response.
28
 *
29
 * https://spec.openapis.org/oas/v3.1.0#response-object
30
 *
31
 * @package    core
32
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class response extends openapi_base {
36
    /**
37
     * Create a new described response.
38
     *
39
     * @param int $statuscode The status code for this response
40
     * @param string $description A description of this response
41
     * @param array $headers The headers associated with this response
42
     * @param array|payload_response_type $content The content of this response
43
     * @param mixed ...$extra Any extra data to store
44
     * @throws coding_exception
45
     */
46
    public function __construct(
47
        /** @var int The status code for this response */
48
        public readonly int $statuscode = 200,
49
        /** @var string A description of this response */
50
        public readonly string $description = '',
51
        /** @var array The headers associated with this response */
52
        private readonly array $headers = [],
53
        /** @var array|payload_response_type The content of this response */
54
        public readonly array|payload_response_type $content = [],
55
        ...$extra,
56
    ) {
57
        if (is_array($content)) {
58
            foreach ($content as $contentitem) {
59
                if (!$contentitem instanceof media_type) {
60
                    throw new coding_exception('Content must be an array of payload response types');
61
                }
62
            }
63
        }
64
 
65
        parent::__construct(...$extra);
66
    }
67
 
68
    /**
69
     * Validate the response.
70
     *
71
     * @param ResponseInterface $response The response to validate
72
     */
73
    public function validate(
74
        ResponseInterface $response,
75
    ): void {
76
        $response;
77
    }
78
 
79
    /**
80
     * Get the description for this response.
81
     *
82
     * @return string
83
     */
84
    protected function get_description(): string {
85
        if ($this->description !== '') {
86
            return $this->description;
87
        }
88
 
89
        return match ($this->statuscode) {
90
            200 => 'OK',
91
            default => '',
92
        };
93
    }
94
 
95
    #[\Override]
96
    public function get_openapi_description(
97
        specification $api,
98
        ?string $path = null,
99
    ): ?\stdClass {
100
        $data = (object) [
101
            'description' => $this->get_description(),
102
        ];
103
 
104
        if (count($this->headers)) {
105
            foreach ($this->headers as $header) {
106
                $data->headers[$header->get_name()] = $header->get_openapi_schema(
107
                    api: $api,
108
                );
109
            }
110
        }
111
 
112
        if ($this->content instanceof content\payload_response_type) {
113
            $data->content = $this->content->get_openapi_schema(
114
                api: $api,
115
            );
116
        } else if (count($this->content)) {
117
            foreach ($this->content as $body) {
118
                $data->content[$body->get_mimetype()] = $body->get_openapi_schema(
119
                    api: $api,
120
                );
121
            }
122
        }
123
 
124
        return $data;
125
    }
126
 
127
    /**
128
     * Get the status code for this response.
129
     *
130
     * @return int
131
     */
132
    public function get_status_code(): int {
133
        return $this->statuscode;
134
    }
135
}