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\parameters;
18
 
19
use core\exception\invalid_parameter_exception;
20
use core\param;
21
use core\router\schema\parameter;
22
use Psr\Http\Message\ServerRequestInterface;
23
 
24
/**
25
 * A Header Object.
26
 *
27
 * https://spec.openapis.org/oas/v3.1.0#headerObject
28
 *
29
 * The Header Object follows the structure of the Parameter Object with the following changes:
30
 *
31
 * - name MUST NOT be specified, it is given in the corresponding headers map.
32
 * - in MUST NOT be specified, it is implicitly in header.
33
 * - All traits that are affected by the location MUST be applicable to a location of header (for example, style).
34
 *
35
 * @package    core
36
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class header_object extends parameter {
40
    /**
41
     * Create a new header object.
42
     *
43
     * @param bool $multiple Whether this parameter can be specified multiple times.
44
     * @param mixed ...$extra Header arguments to pass to the parameter constructor.
45
     */
46
    public function __construct(
47
        /** @var bool Whether multiple instances of this header are supported */
48
        protected bool $multiple = false,
49
        ...$extra,
50
    ) {
51
        $extra['in'] = parameter::IN_HEADER;
52
        parent::__construct(...$extra);
53
    }
54
 
55
    /**
56
     * Validate the parameter.
57
     *
58
     * @param ServerRequestInterface $request The request to validate.
59
     * @return ServerRequestInterface The request with the validated parameter.
60
     * @throws invalid_parameter_exception If the parameter is invalid.
61
     */
62
    public function validate(
63
        ServerRequestInterface $request,
64
    ): ServerRequestInterface {
65
        if ($request->hasHeader($this->name)) {
66
            $headervalues = $request->getHeader($this->name);
67
 
68
            if (!$this->multiple && count($headervalues) > 1) {
69
                throw new invalid_parameter_exception(
70
                    "The parameter {$this->name} was specified multiple times, but it can only be specified once",
71
                );
72
            }
73
 
74
            // This parameter was specified. Validate it.
75
            if ($this->get_type() === param::BOOL) {
76
                $headervalues = array_map(fn ($headervalue) => match ($headervalue) {
77
                    'true' => 1,
78
                    'false' => 0,
79
                    default => throw new \ValueError('Invalid boolean value.'),
80
                }, $headervalues);
81
                return $request->withHeader($this->name, $headervalues);
82
            }
83
 
84
            foreach ($headervalues as $headervalue) {
85
                $this->type->validate_param($headervalue);
86
            }
87
 
88
            return $request;
89
        }
90
 
91
        if ($this->required) {
92
            throw new invalid_parameter_exception(
93
                "A required parameter {$this->name} was not provided and must be specified",
94
            );
95
        }
96
 
97
        if ($this->default !== null) {
98
            // This parameter is optional. Fill the default.
99
            return $request->withHeader($this->name, $this->default);
100
        }
101
 
102
        // This parameter is optional and there is no default.
103
        // Fill a null value.
104
        return $request->withHeader($this->name, null);
105
    }
106
}