Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
36.84% |
7 / 19 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Text | |
36.84% |
7 / 19 |
|
62.50% |
5 / 8 |
41.48 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| nodeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| nodeName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| textContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTextContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| splitText | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| length | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| shallowClone | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Html\Dom; |
| 6 | |
| 7 | /** |
| 8 | * Text node. Holds character data; mutable post-parse via $data. |
| 9 | */ |
| 10 | final class Text extends Node |
| 11 | { |
| 12 | public string $data; |
| 13 | |
| 14 | public function __construct(Document $ownerDocument, string $data = '') |
| 15 | { |
| 16 | parent::__construct($ownerDocument); |
| 17 | $this->data = $data; |
| 18 | } |
| 19 | |
| 20 | public function nodeType(): NodeType |
| 21 | { |
| 22 | return NodeType::Text; |
| 23 | } |
| 24 | |
| 25 | public function nodeName(): string |
| 26 | { |
| 27 | return '#text'; |
| 28 | } |
| 29 | |
| 30 | public function textContent(): string |
| 31 | { |
| 32 | return $this->data; |
| 33 | } |
| 34 | |
| 35 | public function setTextContent(string $text): void |
| 36 | { |
| 37 | $this->data = $text; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Split this text node at $offset. The current node keeps the prefix; a |
| 42 | * new sibling text node is inserted after it with the suffix, and returned. |
| 43 | * |
| 44 | * @throws \OutOfRangeException if offset is outside [0, length]. |
| 45 | */ |
| 46 | public function splitText(int $offset): Text |
| 47 | { |
| 48 | $length = strlen($this->data); |
| 49 | if ($offset < 0 || $offset > $length) { |
| 50 | throw new \OutOfRangeException(sprintf('splitText offset %d outside [0, %d]', $offset, $length)); |
| 51 | } |
| 52 | $suffix = substr($this->data, $offset); |
| 53 | $this->data = substr($this->data, 0, $offset); |
| 54 | $sibling = new self($this->ownerDocument, $suffix); |
| 55 | $parent = $this->parentNode; |
| 56 | if ($parent !== null) { |
| 57 | $parent->insertBefore($sibling, $this->nextSibling); |
| 58 | } |
| 59 | return $sibling; |
| 60 | } |
| 61 | |
| 62 | public function length(): int |
| 63 | { |
| 64 | return strlen($this->data); |
| 65 | } |
| 66 | |
| 67 | protected function shallowClone(): static |
| 68 | { |
| 69 | $copy = new self($this->ownerDocument, $this->data); |
| 70 | /** @var static $copy */ |
| 71 | return $copy; |
| 72 | } |
| 73 | } |