Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
BaseXmlGenerator
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 generate
n/a
0 / 0
n/a
0 / 0
0
 constructBaseElement
n/a
0 / 0
n/a
0 / 0
0
 make
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getXml
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getXmlElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 escape
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addChild
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/** @noinspection PhpComposerExtensionStubsInspection */
4
5declare(strict_types=1);
6
7namespace Hyde\Framework\Features\XmlGenerators;
8
9use Exception;
10use SimpleXMLElement;
11
12use function extension_loaded;
13use function htmlspecialchars;
14use function throw_unless;
15
16/**
17 * Defines the public API for XML generators and provides a shared codebase for common helpers.
18 *
19 * @see \Hyde\Framework\Features\XmlGenerators\RssFeedGenerator
20 * @see \Hyde\Framework\Features\XmlGenerators\SitemapGenerator
21 */
22abstract class BaseXmlGenerator
23{
24    protected SimpleXMLElement $xmlElement;
25
26    /**
27     * Generate the XML document.
28     *
29     * @return $this
30     */
31    abstract public function generate(): static;
32
33    abstract protected function constructBaseElement(): void;
34
35    /**
36     * Generate a new XML document and get the contents as a string.
37     */
38    public static function make(): string
39    {
40        return (new static)->generate()->getXML();
41    }
42
43    /**
44     * Create a new XML generator instance.
45     */
46    public function __construct()
47    {
48        throw_unless(extension_loaded('simplexml'),
49            new Exception('The SimpleXML extension is required to generate RSS feeds and sitemaps.')
50        );
51
52        $this->constructBaseElement();
53    }
54
55    /**
56     * Get the XML document as a string.
57     */
58    public function getXml(): string
59    {
60        return (string) $this->xmlElement->asXML();
61    }
62
63    /**
64     * Get the XML document as a SimpleXMLElement object.
65     */
66    public function getXmlElement(): SimpleXMLElement
67    {
68        return $this->xmlElement;
69    }
70
71    protected function escape(string $string): string
72    {
73        return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, 'UTF-8');
74    }
75
76    protected function addChild(SimpleXMLElement $element, string $name, string $value): SimpleXMLElement
77    {
78        return $element->addChild($name, $this->escape($value));
79    }
80}