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