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;
18
 
19
use core\exception\coding_exception;
20
 
21
/**
22
 * A Response Example Object.
23
 *
24
 * https://spec.openapis.org/oas/v3.1.0#example-object
25
 *
26
 * @package    core
27
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class example extends openapi_base {
31
    /**
32
     * Create a new example.
33
     *
34
     * @param string $name The name of the example.
35
     * @param string|null $summary A summary of the example.
36
     * @param string|null $description A long description fo the example. CommonMark syntax may be used.
37
     * @param mixed $value Embedded literal example.
38
     * @param string|null $externalvalue A URI that points to the literal example.
39
     * @param mixed ...$extra
40
     * @throws coding_exception if both the value and externalvalue are null
41
     */
42
    public function __construct(
43
        /** @var string The name of the example */
44
        protected string $name,
45
        /** @var string|null A summary of the example */
46
        protected ?string $summary = null,
47
        /** @var string|null A long description fo the example. CommonMark syntax may be used */
48
        protected ?string $description = null,
49
        /**
50
         * Embedded literal example.
51
         *
52
         * The value field and externalValue field are mutually exclusive.
53
         * To represent examples of media types that cannot naturally represented in JSON or YAML,
54
         * use a string value to contain the example, escaping where necessary.
55
         *
56
         * @var mixed
57
         */
58
        protected mixed $value = null,
59
        /**
60
         * A URI that points to the literal example.
61
         *
62
         * This provides the capability to reference examples that cannot easily be included in JSON or YAML documents.
63
         * The value field and externalValue field are mutually exclusive. See the rules for resolving Relative References.
64
         *
65
         * @var string|null
66
         */
67
        protected ?string $externalvalue = null,
68
        ...$extra,
69
    ) {
70
        if (!($value === null || $externalvalue === null)) {
71
            throw new coding_exception('Only one of value or externalvalue can be specified.');
72
        }
73
 
74
        parent::__construct(...$extra);
75
    }
76
 
77
    /**
78
     * Get the name of this example.
79
     *
80
     * @return string
81
     */
82
    public function get_name(): string {
83
        return $this->name;
84
    }
85
 
86
    #[\Override]
87
    public function get_openapi_description(
88
        specification $api,
89
        ?string $path = null,
90
    ): ?\stdClass {
91
        $data = (object) [];
92
 
93
        if ($this->summary !== null) {
94
            $data->summary = $this->summary;
95
        }
96
 
97
        if ($this->description !== null) {
98
            $data->description = $this->description;
99
        }
100
 
101
        if ($this->value !== null) {
102
            $data->value = $this->value;
103
        } else if ($this->externalvalue !== null) {
104
            $data->externalValue = $this->externalvalue;
105
        }
106
 
107
        return $data;
108
    }
109
}