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\referenced_object;
21
 
22
/**
23
 * A schema to describe an array of strings.
24
 *
25
 * TODO: This should really take a param:: type for validation of both name and value.
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
final class array_of_strings extends array_of_things implements referenced_object {
32
    /**
33
     * Create a new array_of_strings schema.
34
     *
35
     * @param param $keyparamtype The type of the key parameter
36
     * @param param $valueparamtype The type of the value parameter
37
     * @param mixed ...$extra Additional arguments
38
     */
39
    public function __construct(
40
        /** @var param The type param type for the key */
41
        protected param $keyparamtype = param::RAW,
42
        /** @var param The type param type for the value */
43
        protected param $valueparamtype = param::RAW,
44
        ...$extra,
45
    ) {
46
        $extra['thingtype'] = 'string';
47
        parent::__construct(...$extra);
48
    }
49
 
50
    #[\Override]
51
    public function validate_data(mixed $data) {
52
        foreach ($data as $name => $value) {
53
            $this->keyparamtype->validate_param(
54
                param: $name,
55
                debuginfo: $this->get_debug_info_for_validation_failure($this->keyparamtype, $name),
56
            );
57
            $this->valueparamtype->validate_param(
58
                param: $value,
59
                debuginfo: $this->get_debug_info_for_validation_failure($this->valueparamtype, $value),
60
            );
61
        }
62
        return $data;
63
    }
64
 
65
    /**
66
     * Get the debug info for a validation failure.
67
     *
68
     * @param param $type
69
     * @param string $value
70
     * @return string
71
     */
72
    protected function get_debug_info_for_validation_failure(
73
        param $type,
74
        string $value,
75
    ): string {
76
        return  "The value '{$value}' was not of type {$type->value}.";
77
    }
78
}