Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
41 / 41
Document
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
9 / 9
21
100.00% covered (success)
100.00%
41 / 41
 __construct
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 getDoctype
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCharset
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getContentType
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setDoctype
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
21 / 21
 setCharset
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 setContentType
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 render
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 __toString
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Dom;
15
16/**
17 * Dom class
18 *
19 * @category   Pop
20 * @package    Pop\Dom
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.3.0
25 */
26class Document extends AbstractNode
27{
28
29    /**
30     * Constant to use the XML doctype
31     * @var string
32     */
33    const XML = 'XML';
34
35    /**
36     * Constant to use the HTML doctype
37     * @var string
38     */
39    const HTML = 'HTML';
40
41    /**
42     * Constant to use the XML doctype, RSS content-type
43     * @var string
44     */
45    const RSS = 'RSS';
46
47    /**
48     * Constant to use the XML doctype, Atom content-type
49     * @var string
50     */
51    const ATOM = 'ATOM';
52
53    /**
54     * Document type
55     * @var string
56     */
57    protected $doctype = 'XML';
58
59    /**
60     * Document content type
61     * @var string
62     */
63    protected $contentType = 'application/xml';
64
65    /**
66     * Document charset
67     * @var string
68     */
69    protected $charset = 'utf-8';
70
71    /**
72     * Document doctypes
73     * @var array
74     */
75    protected static $doctypes = [
76        'XML'  => "<?xml version=\"1.0\" encoding=\"[{charset}]\"?>\n",
77        'HTML' =>"<!DOCTYPE html>\n"
78    ];
79
80    /**
81     * Constructor
82     *
83     * Instantiate the document object
84     *
85     * @param  string $doctype
86     * @param  Child  $childNode
87     * @param  string $indent
88     */
89    public function __construct($doctype = 'XML', Child $childNode = null, $indent = null)
90    {
91        $this->setDoctype($doctype);
92
93        if (null !== $childNode) {
94            $this->addChild($childNode);
95        }
96        if (null !== $indent) {
97            $this->setIndent($indent);
98        }
99    }
100
101    /**
102     * Return the document type.
103     *
104     * @return string
105     */
106    public function getDoctype()
107    {
108        return str_replace('[{charset}]', $this->charset, Document::$doctypes[$this->doctype]);
109    }
110
111    /**
112     * Return the document charset
113     *
114     * @return string
115     */
116    public function getCharset()
117    {
118        return $this->charset;
119    }
120
121    /**
122     * Return the document charset
123     *
124     * @return string
125     */
126    public function getContentType()
127    {
128        return $this->contentType;
129    }
130
131    /**
132     * Set the document type
133     *
134     * @param  string $doctype
135     * @throws Exception
136     * @return Document
137     */
138    public function setDoctype($doctype)
139    {
140        $doctype = strtoupper($doctype);
141
142        if (($doctype != self::XML) && ($doctype != self::HTML) && ($doctype != self::RSS) && ($doctype != self::ATOM)) {
143            throw new Exception('Error: Incorrect document type');
144        }
145
146        switch ($doctype) {
147            case 'XML':
148                $this->doctype     = self::XML;
149                $this->contentType = 'application/xml';
150                break;
151            case 'HTML':
152                $this->doctype     = self::HTML;
153                $this->contentType = 'text/html';
154                break;
155            case 'RSS':
156                $this->doctype     = self::XML;
157                $this->contentType = 'application/rss+xml';
158                break;
159            case 'ATOM':
160                $this->doctype     = self::XML;
161                $this->contentType = 'application/atom+xml';
162                break;
163        }
164
165        return $this;
166    }
167
168    /**
169     * Set the document charset
170     *
171     * @param  string $char
172     * @return Document
173     */
174    public function setCharset($char)
175    {
176        $this->charset = $char;
177        return $this;
178    }
179
180    /**
181     * Set the document charset
182     *
183     * @param  string $content
184     * @return Document
185     */
186    public function setContentType($content)
187    {
188        $this->contentType = $content;
189        return $this;
190    }
191
192    /**
193     * Render the document and its child elements
194     *
195     * @return string
196     */
197    public function render()
198    {
199        $this->output = null;
200
201        if (null !== $this->doctype) {
202            $this->output .= str_replace('[{charset}]', $this->charset, Document::$doctypes[$this->doctype]);
203        }
204
205        foreach ($this->childNodes as $child) {
206            $this->output .= $child->render(0, $this->indent);
207        }
208
209        return $this->output;
210    }
211
212    /**
213     * Render Dom object to string
214     *
215     * @return string
216     */
217    public function __toString()
218    {
219        return $this->render();
220    }
221
222}