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\response;
18
 
19
use Psr\Http\Message\ResponseFactoryInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
 
23
/**
24
 * A Payload Response for a Routed request.
25
 *
26
 * This response is a container for a response which contains a set of data.
27
 * It is used to pass data from a controller to the routing engine, where it will be formatted into the
28
 * response type requested by the client.
29
 *
30
 * This approach is inspired and based upon slim-routing https://github.com/juliangut/slim-routing
31
 * We only need a fraction of this functionality.
32
 *
33
 * @package    core
34
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class payload_response extends abstract_response {
38
    /**
39
     * Create a new payload response.
40
     *
41
     * @param array $payload The payload
42
     * @param ServerRequestInterface $request The request
43
     * @param ResponseInterface|null $response The response
44
     */
45
    public function __construct(
46
        /** @var array The payload */
47
        public readonly array $payload,
48
        ServerRequestInterface $request,
49
        ?ResponseInterface $response = null,
50
    ) {
51
        parent::__construct($request, $response);
52
    }
53
 
54
    #[\Override]
55
    public function get_response(
56
        ResponseFactoryInterface $responsefactory,
57
    ): ?ResponseInterface {
58
        $response = parent::get_response($responsefactory);
59
 
60
        $response->getBody()->write((string) json_encode(
61
            $this->payload,
62
            $this->get_json_flags(),
63
        ));
64
        return $response->withHeader('Content-Type', 'application/json; charset=utf-8');
65
    }
66
 
67
    /**
68
     * Get the flags to use when encoding JSON.
69
     *
70
     * @return int
71
     */
72
    private function get_json_flags(): int {
73
        global $CFG;
74
 
75
        $flags = \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION;
76
 
77
        if ($CFG->debugdeveloper) {
78
            $flags |= \JSON_PRETTY_PRINT;
79
        }
80
 
81
        return $flags;
82
    }
83
}