Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DomTree
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace pvc\html\dom;
4
5use pvc\html\text_node\TextElement;
6use pvc\interfaces\html\dom\DomCollectionInterface;
7use pvc\interfaces\html\dom\DomNodeFactoryInterface;
8use pvc\interfaces\html\dom\DomNodeInterface;
9use pvc\interfaces\intl\LocaleInterface;
10use pvc\interfaces\struct\tree\node\TreenodeInterface;
11use pvc\interfaces\struct\treesearch\VisitStatus;
12use pvc\struct\tree\tree\Tree;
13
14/**
15 * @phpstan-import-type TreenodeDtoShape from TreenodeInterface
16 * @extends Tree<DomNodeInterface, DomCollectionInterface>
17 */
18class 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}