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