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\exception\coding_exception;
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.1.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 schema_object extends type_base {
32
    /**
33
     * An array of things.
34
     *
35
     * @param type_base[] $content The child content
36
     * @param mixed[] ...$extra
37
     * @throws coding_exception
38
     */
39
    public function __construct(
40
        /** @var type_base[] The child content */
41
        protected array $content,
42
 
43
        ...$extra,
44
    ) {
45
        foreach ($content as $child) {
46
            if (!$child instanceof type_base) {
47
                throw new coding_exception('Content must be an array of type_base objects');
48
            }
49
        }
50
 
51
        parent::__construct(...$extra);
52
    }
53
 
54
    /**
55
     * Whether this schema object has this key as a type.
56
     *
57
     * @param string $key
58
     * @return bool
59
     */
60
    public function has(string $key): bool {
61
        return isset($this->content[$key]);
62
    }
63
 
64
    /**
65
     * Get the schema object for this key.
66
     *
67
     * @param string $key
68
     * @return type_base
69
     */
70
    public function get(string $key): type_base {
71
        return $this->content[$key];
72
    }
73
 
74
    #[\Override]
75
    public function validate_data(mixed $data) {
76
        foreach ($data as $key => $values) {
77
            if (!$this->has($key)) {
78
                // We do not know about this one.
79
                // Remove it from the params array.
80
                $data = array_diff_key(
81
                    $data,
82
                    [$key => $values],
83
                );
84
                continue;
85
            }
86
 
87
            // Validate this parameter.
88
            $child = $this->content[$key];
89
            $data[$key] = $child->validate_data($values);
90
        }
91
 
92
        return $data;
93
    }
94
 
95
    #[\Override]
96
    public function get_openapi_description(
97
        specification $api,
98
        ?string $path = null,
99
    ): ?\stdClass {
100
        $data = (object) [
101
            'type' => 'object',
102
            'properties' => (object) [],
103
        ];
104
 
105
        foreach ($this->content as $name => $content) {
106
            $data->properties->{$name} = $content->get_openapi_schema(
107
                $api,
108
            );
109
        }
110
 
111
        return $data;
112
    }
113
}