|
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 AbstractNode|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)); |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|