|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gt\CssXPath; |
| 4 | + |
| 5 | +class NotSelectorConditionBuilder { |
| 6 | + private ThreadMatcher $threadMatcher; |
| 7 | + private AttributeSelectorConverter $attributeSelectorConverter; |
| 8 | + |
| 9 | + public function __construct( |
| 10 | + ?ThreadMatcher $threadMatcher = null, |
| 11 | + ?AttributeSelectorConverter $attributeSelectorConverter = null, |
| 12 | + ) { |
| 13 | + $this->threadMatcher = $threadMatcher ?? new ThreadMatcher(); |
| 14 | + $this->attributeSelectorConverter = $attributeSelectorConverter |
| 15 | + ?? new AttributeSelectorConverter(); |
| 16 | + } |
| 17 | + |
| 18 | + public function build(string $selector, bool $htmlMode):?string { |
| 19 | + $selector = trim($selector); |
| 20 | + if($selector === "") { |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + $thread = array_values( |
| 25 | + $this->threadMatcher->collate(Translator::CSS_REGEX, $selector) |
| 26 | + ); |
| 27 | + if(!$this->isSupportedThread($thread)) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + $token = $thread[0]; |
| 32 | + $next = $thread[1] ?? null; |
| 33 | + return $this->buildConditionFromToken($token, $next, $htmlMode); |
| 34 | + } |
| 35 | + |
| 36 | + /** @param array<int, array<string, mixed>> $thread */ |
| 37 | + private function isSupportedThread(array $thread):bool { |
| 38 | + if(empty($thread) || count($thread) > 2) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + foreach($thread as $token) { |
| 43 | + if($this->isAxisToken((string)$token["type"])) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + private function isAxisToken(string $type):bool { |
| 52 | + return in_array($type, [ |
| 53 | + "descendant", |
| 54 | + "child", |
| 55 | + "sibling", |
| 56 | + "subsequentsibling", |
| 57 | + ], true); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param array<string, mixed> $token |
| 62 | + * @param array<string, mixed>|null $next |
| 63 | + */ |
| 64 | + private function buildConditionFromToken( |
| 65 | + array $token, |
| 66 | + ?array $next, |
| 67 | + bool $htmlMode |
| 68 | + ):?string { |
| 69 | + return match($token["type"]) { |
| 70 | + "element", "star" => $this->buildElementCondition( |
| 71 | + (string)$token["content"], |
| 72 | + $htmlMode |
| 73 | + ), |
| 74 | + "id" => "@id='" . $token["content"] . "'", |
| 75 | + "class" => "" |
| 76 | + . "contains(concat(' ',normalize-space(@class),' ')," |
| 77 | + . "' " . $token["content"] . " ')", |
| 78 | + "attribute" => $this |
| 79 | + ->attributeSelectorConverter |
| 80 | + ->buildConditionFromToken($token, $htmlMode), |
| 81 | + "pseudo" => $this->buildPseudoCondition($token, $next), |
| 82 | + default => null, |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param array<string, mixed> $token |
| 88 | + * @param array<string, mixed>|null $next |
| 89 | + */ |
| 90 | + private function buildPseudoCondition(array $token, ?array $next):?string { |
| 91 | + $pseudo = (string)$token["content"]; |
| 92 | + $specifier = $this->extractSpecifier($next); |
| 93 | + |
| 94 | + if(in_array($pseudo, ["disabled", "checked", "selected"], true)) { |
| 95 | + return "@{$pseudo}"; |
| 96 | + } |
| 97 | + |
| 98 | + return match($pseudo) { |
| 99 | + "text" => '@type="text"', |
| 100 | + "contains" => $specifier !== "" |
| 101 | + ? "contains(text(),{$specifier})" |
| 102 | + : null, |
| 103 | + "first-child", "first-of-type" => "position() = 1", |
| 104 | + "nth-child", "nth-of-type" => $specifier !== "" |
| 105 | + ? "position() = {$specifier}" |
| 106 | + : null, |
| 107 | + "last-child", "last-of-type" => "position() = last()", |
| 108 | + default => null, |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + private function buildElementCondition(string $name, bool $htmlMode):string { |
| 113 | + if($name === "*") { |
| 114 | + return "self::*"; |
| 115 | + } |
| 116 | + |
| 117 | + $element = $htmlMode ? strtolower($name) : $name; |
| 118 | + return "self::{$element}"; |
| 119 | + } |
| 120 | + |
| 121 | + /** @param array<string, mixed>|null $next */ |
| 122 | + private function extractSpecifier(?array $next):string { |
| 123 | + if(!$next || $next["type"] !== "pseudospecifier") { |
| 124 | + return ""; |
| 125 | + } |
| 126 | + |
| 127 | + return (string)$next["content"]; |
| 128 | + } |
| 129 | +} |
0 commit comments