Skip to content

Commit c97a84c

Browse files
committed
Refactoring
1 parent b44c872 commit c97a84c

File tree

1 file changed

+100
-60
lines changed

1 file changed

+100
-60
lines changed

src/PHPHtmlParser/Selector/Selector.php

Lines changed: 100 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -129,67 +129,18 @@ protected function seek(array $nodes, array $rule, array $options): array
129129
// wild card, grab all
130130
if ($rule['tag'] == '*' && is_null($rule['key'])) {
131131
$return[] = $child;
132-
try {
133-
$child = $node->nextChild($child->id());
134-
} catch (ChildNotFoundException $e) {
135-
// no more children
136-
$child = null;
137-
}
132+
$child = $this->getNextChild($node, $child);
138133
continue;
139134
}
140135

141-
$pass = true;
142-
// check tag
143-
if ( ! empty($rule['tag']) && $rule['tag'] != $child->getTag()->name() &&
144-
$rule['tag'] != '*'
145-
) {
146-
// child failed tag check
147-
$pass = false;
148-
}
149-
150-
// check key
136+
$pass = $this->checkTag($rule, $child);
151137
if ($pass && ! is_null($rule['key'])) {
152-
if ($rule['noKey']) {
153-
if ( ! is_null($child->getAttribute($rule['key']))) {
154-
$pass = false;
155-
}
156-
} else {
157-
if ($rule['key'] != 'plaintext' && !$child->hasAttribute($rule['key'])) {
158-
$pass = false;
159-
}
160-
}
138+
$pass = $this->checkKey($rule, $child);
161139
}
162-
163-
// compare values
164140
if ($pass && ! is_null($rule['key']) &&
165141
! is_null($rule['value']) && $rule['value'] != '*'
166142
) {
167-
if ($rule['key'] == 'plaintext') {
168-
// plaintext search
169-
$nodeValue = $child->text();
170-
} else {
171-
// normal search
172-
$nodeValue = $child->getAttribute($rule['key']);
173-
}
174-
175-
$check = $this->match($rule['operator'], $rule['value'], $nodeValue);
176-
177-
// handle multiple classes
178-
if ( ! $check && $rule['key'] == 'class') {
179-
$childClasses = explode(' ', $child->getAttribute('class'));
180-
foreach ($childClasses as $class) {
181-
if ( ! empty($class)) {
182-
$check = $this->match($rule['operator'], $rule['value'], $class);
183-
}
184-
if ($check) {
185-
break;
186-
}
187-
}
188-
}
189-
190-
if ( ! $check) {
191-
$pass = false;
192-
}
143+
$pass = $this->checkComparison($rule, $child);
193144
}
194145

195146
if ($pass) {
@@ -205,13 +156,7 @@ protected function seek(array $nodes, array $rule, array $options): array
205156
}
206157
}
207158

208-
try {
209-
// get next child
210-
$child = $node->nextChild($child->id());
211-
} catch (ChildNotFoundException $e) {
212-
// no more children
213-
$child = null;
214-
}
159+
$child = $this->getNextChild($node, $child);
215160
}
216161

217162
if (( ! isset($options['checkGrandChildren']) ||
@@ -295,4 +240,99 @@ protected function flattenOptions(array $optionsArray)
295240

296241
return $options;
297242
}
243+
244+
/**
245+
* Returns the next child or null if no more children.
246+
*
247+
* @param AbstractNode $node
248+
* @param AbstractNode $currentChild
249+
* @return AbstractNode|null
250+
*/
251+
protected function getNextChild(AbstractNode $node, AbstractNode $currentChild)
252+
{
253+
try {
254+
// get next child
255+
$child = $node->nextChild($currentChild->id());
256+
} catch (ChildNotFoundException $e) {
257+
// no more children
258+
$child = null;
259+
}
260+
261+
return $child;
262+
}
263+
264+
/**
265+
* Checks tag condition from rules against node.
266+
*
267+
* @param array $rule
268+
* @param AbstractNode $node
269+
* @return bool
270+
*/
271+
protected function checkTag(array $rule, AbstractNode $node): bool
272+
{
273+
if ( ! empty($rule['tag']) && $rule['tag'] != $node->getTag()->name() &&
274+
$rule['tag'] != '*'
275+
) {
276+
return false;
277+
}
278+
279+
return true;
280+
}
281+
282+
/**
283+
* Checks key condition from rules against node.
284+
*
285+
* @param array $rule
286+
* @param AbstractNode $node
287+
* @return bool
288+
*/
289+
protected function checkKey(array $rule, AbstractNode $node): bool
290+
{
291+
if ($rule['noKey']) {
292+
if ( ! is_null($node->getAttribute($rule['key']))) {
293+
return false;
294+
}
295+
} else {
296+
if ($rule['key'] != 'plaintext' && !$node->hasAttribute($rule['key'])) {
297+
return false;
298+
}
299+
}
300+
301+
return true;
302+
}
303+
304+
/**
305+
* Checks comparison condition from rules against node.
306+
*
307+
* @param array $rule
308+
* @param AbstractNode $node
309+
* @return bool
310+
*/
311+
public function checkComparison(array $rule, AbstractNode $node): bool
312+
{
313+
if ($rule['key'] == 'plaintext') {
314+
// plaintext search
315+
$nodeValue = $node->text();
316+
} else {
317+
// normal search
318+
$nodeValue = $node->getAttribute($rule['key']);
319+
}
320+
321+
$check = $this->match($rule['operator'], $rule['value'], $nodeValue);
322+
323+
// handle multiple classes
324+
if ( ! $check && $rule['key'] == 'class') {
325+
$nodeClasses = explode(' ', $node->getAttribute('class'));
326+
foreach ($nodeClasses as $class) {
327+
if ( ! empty($class)) {
328+
$check = $this->match($rule['operator'], $rule['value'], $class);
329+
}
330+
if ($check) {
331+
break;
332+
}
333+
}
334+
}
335+
336+
return $check;
337+
}
298338
}

0 commit comments

Comments
 (0)