aboutsummaryrefslogtreecommitdiff
path: root/Psso/XMLResponse.php
blob: 1b176603d4906ca8c466f8208993c1220d803ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Psso;

class XMLResponse {
    public \SimpleXMLElement $doc;
    private static array $stylesheets;
    private static array $runFirst;

    static function addStylesheet(string $path) {
        $sheet = \Dom\XMLDocument::createFromFile($path);
        self::$stylesheets[] = $sheet;
    }

    static function addPreamble(callable $preamble) {
        self::$runFirst[] = $preamble;
    }

    function __construct(string $rootElement = 'page') {
        $this->doc = new \SimpleXMLElement("<$rootElement/>");
        foreach (self::$runFirst as $fn) {
            $fn($this->doc);
        }
    }

    function sendRaw() {
        header('Content-Type: application/xml');
        echo $this->doc->asXML();
    }

    function send() {
        if (isset($_GET['RawXML'])) {
            $this->sendRaw();
            return;
        }

        //header('Content-Type: application/xhtml+xml');
        $processor = new \XSLTProcessor;
        foreach (self::$stylesheets as $sheet) {
            $processor->importStylesheet($sheet);
        }
        $root = \Dom\import_simplexml($this->doc);
        echo $processor->transformToXml($root);
    }
}