Passed
Push — master ( 417605...635760 )
by Divine Niiquaye
11:55
created

SelfCloseNode::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\UI\Html\Node;
19
20
use Biurad\UI\Exceptions\ParserException;
21
22
class SelfCloseNode extends AbstractNode
23
{
24
    /** @var string @read-only */
25
    public $name;
26
27
    /** @var array<int,AttributeNode> */
28
    public $attributes;
29
30
    /** @var self|null */
31
    public $next;
32
33
    /** @var ElementNode|null */
34
    public $parent;
35
36
    /**
37
     * @param array<int,AttributeNode> $attributes
38
     */
39 1
    public function __construct(string $name, array $attributes = [])
40
    {
41 1
        $this->name = $name;
42 1
        $this->attributes = $attributes;
43
    }
44
45 1
    public function __toString(): string
46
    {
47 1
        $attrHtml = '';
48
49 1
        foreach ($this->attributes as $attribute) {
50 1
            $attrHtml .= (string) $attribute;
51
        }
52
53 1
        return '<' . $this->name . $attrHtml . '>';
54
    }
55
56
    /**
57
     * Checks if attribute(s) exists.
58
     *
59
     * @param array<int,string> $attrNames
60
     *
61
     * @throws ParserException if a duplicated attribute if found
62
     */
63
    public function has(array $attrNames, AttributeNode &$attrNode = null): bool
64
    {
65
        $attrName = null;
66
67
        foreach ($this->attributes as $attribute) {
68
            if (\in_array($attribute->name, $attrNames, true)) {
69
                if (null !== $attrName) {
70
                    throw new ParserException(\sprintf('Found a duplicated attribute "%s", expected one.', $attrName));
0 ignored issues
show
Bug introduced by
$attrName of type void is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                    throw new ParserException(\sprintf('Found a duplicated attribute "%s", expected one.', /** @scrutinizer ignore-type */ $attrName));
Loading history...
71
                }
72
73
                $attrName = $attribute->name;
74
75
                if (\array_key_exists(1, \func_get_args())) {
76
                    $attrNode = $attribute;
77
                }
78
            }
79
        }
80
81
        return null !== $attrName;
82
    }
83
84
    /**
85
     * Checks if attribute(s) exists in parent's children nodes.
86
     *
87
     * @param array<int,string> $attrNames
88
     * @throws ParserException if a duplicated attribute if found
89
     */
90
    public function parentHas(array $attrNames, AbstractNode $tagNode = null): bool
91
    {
92
        if (null !== $tagParent = $this->parent) {
93
            foreach ($tagParent->children as $childNode) {
94
                if (!$childNode instanceof SelfCloseNode) {
95
                    continue;
96
                }
97
98
                if ($childNode->has($attrNames)) {
99
                    if (\array_key_exists(1, \func_get_args())) {
100
                        $tagNode = $childNode;
0 ignored issues
show
Unused Code introduced by
The assignment to $tagNode is dead and can be removed.
Loading history...
101
                    }
102
103
                    return true;
104
                }
105
            }
106
        }
107
108
        return false;
109
    }
110
}
111