1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace dlindberg\BlobChunk\Manager; |
6
|
|
|
|
7
|
|
|
abstract class NodeCheck |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Config |
11
|
|
|
*/ |
12
|
|
|
protected $config; |
13
|
|
|
|
14
|
|
|
public function __construct(Config $config) |
15
|
|
|
{ |
16
|
|
|
$this->config = $config; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function nameNodeType(\DOMNode $input): string |
20
|
|
|
{ |
21
|
|
|
if ($this->test($input, 'parent')) { |
22
|
|
|
return 'parent'; |
23
|
|
|
} |
24
|
|
|
if ($this->test($input, 'special')) { |
25
|
|
|
return 'special'; |
26
|
|
|
} |
27
|
|
|
if ($this->test($input, 'splitWhat')) { |
28
|
|
|
return 'split'; |
29
|
|
|
} |
30
|
|
|
if ($this->test($input, 'recursionParent')) { |
31
|
|
|
return 'recursive'; |
32
|
|
|
} |
33
|
|
|
if ($this->test($input, 'rowColParent')) { |
34
|
|
|
return 'rowCol'; |
35
|
|
|
} |
36
|
|
|
if ($this->test($input, 'pairParents')) { |
37
|
|
|
return 'pairs'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return 'default'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function get(\DOMNode $parent, array $in): array |
44
|
|
|
{ |
45
|
|
|
return \array_merge(...\array_filter($in, function ($set) use ($parent) { |
46
|
|
|
return $this->testFlat($parent, $set['parent']); |
47
|
|
|
})); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function test(\DOMNode $input, string $what): bool |
51
|
|
|
{ |
52
|
|
|
$tags = $this->config->convertKey($what, 'tags'); |
53
|
|
|
$attributes = $this->config->convertKey($what, 'attributes'); |
54
|
|
|
|
55
|
|
|
return $this->testTag($input, $this->config->$tags) || $this->testAttribute($input, $this->config->$attributes); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function testFlat(\DOMNode $input, array $definition): bool |
59
|
|
|
{ |
60
|
|
|
if (\array_key_exists('type', $definition)) { |
61
|
|
|
if ('tag' === $definition['type']) { |
62
|
|
|
return $definition['value'] === $input->nodeName; |
63
|
|
|
} elseif ('attribute' === $definition['type'] && $input->hasAttributes()) { |
64
|
|
|
return $this->walkAttributes($input->attributes, [$definition]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function trimmedDelimiters(): array |
72
|
|
|
{ |
73
|
|
|
return \array_map(function (string $delimiter) { |
74
|
|
|
return trim($delimiter); |
75
|
|
|
}, $this->config->splitOn); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function testTag(\DOMNode $node, array $tags): bool |
79
|
|
|
{ |
80
|
|
|
return \in_array($node->nodeName, $tags); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function testAttribute(\DOMNode $node, array $attributes): bool |
84
|
|
|
{ |
85
|
|
|
return $node->hasAttributes() && 0 !== \count($attributes) ? |
86
|
|
|
$this->walkAttributes($node->attributes, $attributes) : false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function walkAttributes(\DOMNamedNodeMap $nodeMap, array $attributes): bool |
90
|
|
|
{ |
91
|
|
|
$attribute = \array_shift($attributes); |
92
|
|
|
if ($this->getNodeValue($nodeMap, $attribute['name']) === \mb_strtolower($attribute['value'])) { |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return 0 !== \count($attributes) ? $this->walkAttributes($nodeMap, $attributes) : false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getNodeValue(\DOMNamedNodeMap $nodeMap, string $what): ?string |
100
|
|
|
{ |
101
|
|
|
return $nodeMap->getNamedItem(\mb_strtolower($what)) instanceof \DOMNode ? |
102
|
|
|
\mb_strtolower($nodeMap->getNamedItem(\mb_strtolower($what))->nodeValue) : null; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|