Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
| AbstractNode | |
100.00% |
27 / 27 |
|
100.00% |
13 / 13 |
17 | |
100.00% |
1 / 1 |
| getIndent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setIndent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setParent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addChild | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| addChildren | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| hasChildren | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasChildNodes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getChild | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getChildren | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getChildNodes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeChild | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| removeChildren | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Dom; |
| 16 | |
| 17 | use InvalidArgumentException; |
| 18 | |
| 19 | /** |
| 20 | * Abstract node class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Dom |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 5.0.0 |
| 28 | */ |
| 29 | abstract class AbstractNode implements NodeInterface |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Object child nodes |
| 34 | * @var array |
| 35 | */ |
| 36 | protected array $childNodes = []; |
| 37 | |
| 38 | /** |
| 39 | * Indentation for formatting purposes |
| 40 | * @var ?string |
| 41 | */ |
| 42 | protected ?string $indent = null; |
| 43 | |
| 44 | /** |
| 45 | * Child output |
| 46 | * @var ?string |
| 47 | */ |
| 48 | protected ?string $output = null; |
| 49 | |
| 50 | /** |
| 51 | * Parent node |
| 52 | * @var ?AbstractNode |
| 53 | */ |
| 54 | protected ?AbstractNode $parent = null; |
| 55 | |
| 56 | /** |
| 57 | * Return the indent |
| 58 | * |
| 59 | * @return ?string |
| 60 | */ |
| 61 | public function getIndent(): ?string |
| 62 | { |
| 63 | return $this->indent; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set the indent |
| 68 | * |
| 69 | * @param string $indent |
| 70 | * @return AbstractNode |
| 71 | */ |
| 72 | public function setIndent(string $indent): AbstractNode |
| 73 | { |
| 74 | $this->indent = $indent; |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return the parent node |
| 80 | * |
| 81 | * @return AbstractNode|null |
| 82 | */ |
| 83 | public function getParent(): AbstractNode|null |
| 84 | { |
| 85 | return $this->parent; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Set the parent node |
| 90 | * |
| 91 | * @param AbstractNode $parent |
| 92 | * @return AbstractNode |
| 93 | */ |
| 94 | public function setParent(NodeInterface $parent): AbstractNode |
| 95 | { |
| 96 | $this->parent = $parent; |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Add a child to the object |
| 102 | * |
| 103 | * @param Child $c |
| 104 | * @throws InvalidArgumentException |
| 105 | * @return AbstractNode |
| 106 | */ |
| 107 | public function addChild(Child $c): AbstractNode |
| 108 | { |
| 109 | $c->setParent($this); |
| 110 | $this->childNodes[] = $c; |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Add children to the object |
| 116 | * |
| 117 | * @param mixed $children |
| 118 | * @throws Exception |
| 119 | * @return AbstractNode |
| 120 | */ |
| 121 | public function addChildren(mixed $children): AbstractNode |
| 122 | { |
| 123 | if (is_array($children)) { |
| 124 | foreach ($children as $child) { |
| 125 | $this->addChild($child); |
| 126 | } |
| 127 | } else if ($children instanceof Child) { |
| 128 | $this->addChild($children); |
| 129 | } else { |
| 130 | throw new Exception( |
| 131 | 'Error: The parameter passed must be an instance of Pop\Dom\Child or an array of Pop\Dom\Child instances.' |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | return $this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get whether or not the child object has children |
| 140 | * |
| 141 | * @return bool |
| 142 | */ |
| 143 | public function hasChildren(): bool |
| 144 | { |
| 145 | return (count($this->childNodes) > 0); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get whether or not the child object has children (alias) |
| 150 | * |
| 151 | * @return bool |
| 152 | */ |
| 153 | public function hasChildNodes(): bool |
| 154 | { |
| 155 | return (count($this->childNodes) > 0); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the child nodes of the object |
| 160 | * |
| 161 | * @param int $i |
| 162 | * @return Child|null |
| 163 | */ |
| 164 | public function getChild(int $i): Child|null |
| 165 | { |
| 166 | return $this->childNodes[(int)$i] ?? null; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the child nodes of the object |
| 171 | * |
| 172 | * @return array |
| 173 | */ |
| 174 | public function getChildren(): array |
| 175 | { |
| 176 | return $this->childNodes; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get the child nodes of the object (alias) |
| 181 | * |
| 182 | * @return array |
| 183 | */ |
| 184 | public function getChildNodes(): array |
| 185 | { |
| 186 | return $this->childNodes; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Remove all child nodes from the object |
| 191 | * |
| 192 | * @param int $i |
| 193 | * @return void |
| 194 | */ |
| 195 | public function removeChild(int $i): void |
| 196 | { |
| 197 | if (isset($this->childNodes[$i])) { |
| 198 | unset($this->childNodes[$i]); |
| 199 | $this->childNodes = array_values($this->childNodes); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Remove all child nodes from the object |
| 205 | * |
| 206 | * @return void |
| 207 | */ |
| 208 | public function removeChildren(): void |
| 209 | { |
| 210 | $this->childNodes = []; |
| 211 | } |
| 212 | |
| 213 | } |