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\objects;
18
 
19
use core\param;
20
use core\router\schema\specification;
21
 
22
/**
23
 * A schema to describe an array of things. These could be any type, including other schema definitions.
24
 *
25
 * See https://spec.openapis.org/oas/v3.0.0#model-with-map-dictionary-properties for relevant documentation.
26
 *
27
 * @package    core
28
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class array_of_things extends type_base {
32
    /**
33
     * An array of things.
34
     *
35
     * @param string|type_base|param|null $thingtype The OpenAPI type, or null if any type is allowed.
36
     * @param mixed[] ...$extra
37
     */
38
    public function __construct(
39
        /** @var string|type_base|param|null The child item type */
40
        protected string|type_base|param|null $thingtype = null,
41
        ...$extra,
42
    ) {
43
        parent::__construct(...$extra);
44
    }
45
 
46
    #[\Override]
47
    public function get_openapi_description(
48
        specification $api,
49
        ?string $path = null,
50
    ): ?\stdClass {
51
        return $this->get_schema();
52
    }
53
 
54
    /**
55
     * Get the OpenAPI schema for this object.
56
     *
57
     * @return \stdClass
58
     */
59
    public function get_schema(): \stdClass {
60
        return (object) [
61
            'type' => 'object',
62
            'additionalProperties' => $this->get_additional_properties($this->thingtype),
63
        ];
64
    }
65
 
66
    #[\Override]
67
    public function validate_data(mixed $data) {
68
        if (!is_array($data)) {
69
            throw new \invalid_parameter_exception('Invalid data type, expected array.');
70
        }
71
 
72
        if ($this->thingtype === null) {
73
            return $data;
74
        }
75
 
76
        if (is_a($this->thingtype, type_base::class)) {
77
            $validator = fn ($value) => $this->thingtype->validate_data($value);
78
        } else {
79
            if (is_string($this->thingtype)) {
80
                $param = param::from($this->thingtype);
81
            } else {
82
                $param = $this->thingtype;
83
            }
84
 
85
            $validator = fn ($value) => $param->validate_param($value);
86
        }
87
 
88
        foreach ($data as $value) {
89
            $validator($value);
90
        }
91
        return $data;
92
    }
93
}