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\router\route;
20
use core\router\schema\parameter;
21
use core\router\schema\specification;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Slim\Routing\Route as RoutingRoute;
24
use stdClass;
25
 
26
/**
27
 * Routing parameter for validation.
28
 *
29
 * @package    core
30
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class path_parameter extends parameter {
34
    /**
35
     * Create an instance of a new path parameter.
36
     *
37
     * @param mixed ...$extra Additional parameters for the parameter
38
     */
39
    public function __construct(
40
        ...$extra,
41
    ) {
42
        $extra['in'] = parameter::IN_PATH;
43
        parent::__construct(
44
            ...$extra,
45
        );
46
    }
47
 
48
    /**
49
     * Validate the path parameter.
50
     *
51
     * @param ServerRequestInterface $request
52
     * @param RoutingRoute $route
53
     * @return ServerRequestInterface The modified request with parameters updated
54
     */
55
    public function validate(
56
        ServerRequestInterface $request,
57
        RoutingRoute $route,
58
    ): ServerRequestInterface {
59
        $args = $route->getArguments();
60
 
61
        $value = $route->getArgument($this->name);
62
 
63
        $this->type->validate_param(
64
            param: $value,
65
            allownull: NULL_ALLOWED,
66
        );
67
 
68
        if (is_a($this, mapped_property_parameter::class)) {
69
            // Unfortunately args must be a string, but mapped properties can be an object.
70
            // Remove the argument, and instead provide the mapped property as an attribute.
71
            unset($args[$this->name]);
72
            $route->setArguments($args);
73
            $request = $this->add_attributes_for_parameter_value($request, $value);
74
        }
75
 
76
        return $request;
77
    }
78
 
79
    #[\Override]
80
    final public function get_openapi_description(
81
        specification $api,
82
        ?string $path = null,
83
    ): ?stdClass {
84
        if ($path && !str_contains($path, "{{$this->name}}")) {
85
            // In OpenAPI, Path parameters can never be optional.
86
            return null;
87
        }
88
        $data = parent::get_openapi_description(
89
            api: $api,
90
            path: $path,
91
        );
92
        $data->required = true;
93
 
94
        return $data;
95
    }
96
 
97
    /**
98
     * Check whether this parameter is required for the given route.
99
     *
100
     * @param route $route
101
     * @return bool
102
     */
103
    public function is_required(route $route): bool {
104
        $path = $route->get_path();
105
 
106
        // Find the parameter in the path.
107
        // Search for `{value` with an optional : followed by anything except a closing `}`, and then a closing `}`.
108
        // ~^(?<match>.*\{{$this->name}(?:\:[^}]*)?\})~
109
        // ~                                          ~ => Delimiters
110
        //  ^(?<match>                               )  => Named capture group
111
        //            .*\{                              => Any character, any number of times, followed by {
112
        //                {$this->name}                 => The parameter name
113
        //                             (?:       )?     => Optional non-capturing group
114
        //                                \:[^}]*       => : followed by anything except }
115
        //                                         \}   => Closing }
116
        // If the parameter is not found in the path, then it is not required.
117
        $matchesfound = preg_match(
118
            "~^(?<match>.*\{{$this->name}(?:\:[^}]*)?\})~",
119
            $path,
120
            $matches,
121
        );
122
 
123
        if ($matchesfound === 0) {
124
            // Parameter not found in the path.
125
            return false;
126
        }
127
 
128
        // If _any_ part of the path before the parameter contains a '[' character, then this _must_ be optional.
129
        // A required parameter cannot follow an optional parameter.
130
        return str_contains($matches['match'], '[') === false;
131
    }
132
}