Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DomTree | |
0.00% |
0 / 16 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace pvc\html\dom; |
| 4 | |
| 5 | use pvc\html\text_node\TextElement; |
| 6 | use pvc\interfaces\html\dom\DomCollectionInterface; |
| 7 | use pvc\interfaces\html\dom\DomNodeFactoryInterface; |
| 8 | use pvc\interfaces\html\dom\DomNodeInterface; |
| 9 | use pvc\interfaces\intl\LocaleInterface; |
| 10 | use pvc\interfaces\struct\tree\node\TreenodeInterface; |
| 11 | use pvc\interfaces\struct\treesearch\VisitStatus; |
| 12 | use pvc\struct\tree\tree\Tree; |
| 13 | |
| 14 | /** |
| 15 | * @phpstan-import-type TreenodeDtoShape from TreenodeInterface |
| 16 | * @extends Tree<DomNodeInterface, DomCollectionInterface> |
| 17 | */ |
| 18 | class DomTree extends Tree |
| 19 | { |
| 20 | /** |
| 21 | * @param DomNodeFactoryInterface $domNodeFactory |
| 22 | * @param DomRenderSearch $search |
| 23 | */ |
| 24 | public function __construct( |
| 25 | DomNodeFactoryInterface $domNodeFactory, |
| 26 | protected(set) DomRenderSearch $search, |
| 27 | ) { |
| 28 | parent::__construct($domNodeFactory); |
| 29 | } |
| 30 | |
| 31 | public function render(LocaleInterface $locale): string |
| 32 | { |
| 33 | if ($this->isEmpty()) return ''; |
| 34 | /** |
| 35 | * type checker cannot infer that root is not null from the isEmpty test above |
| 36 | */ |
| 37 | assert(($this->getRoot()) !== null); |
| 38 | $this->search->setStartNode($this->getRoot()); |
| 39 | |
| 40 | $z = ''; |
| 41 | foreach ($this->search as $domNode) { |
| 42 | /** |
| 43 | * the first time we visit the node, its status will be |
| 44 | * partially visited because the status changes just before we |
| 45 | * stop moving. The last time we visit a node, its status will |
| 46 | * be fully visited. |
| 47 | */ |
| 48 | switch ($domNode->getVisitStatus()) { |
| 49 | case VisitStatus::PARTIALLY_VISITED: |
| 50 | /** |
| 51 | * only need to set the locale on a text node the first time |
| 52 | * we visit it..... |
| 53 | */ |
| 54 | if ($domNode instanceof TextElement) { |
| 55 | $domNode->setLocale($locale); |
| 56 | } |
| 57 | $z .= $domNode->getDomElement()->renderFirstVisit(); |
| 58 | break; |
| 59 | |
| 60 | case VisitStatus::FULLY_VISITED: |
| 61 | default: |
| 62 | $z .= $domNode->getDomElement()->renderLastVisit(); |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | return $z; |
| 67 | } |
| 68 | } |