Completed
Pull Request — master (#116)
by Richard
02:24
created

PseudoMatcher::getFuncParts()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.2
c 3
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Hook;
8
use \Transphporm\Parser\Tokenizer;
9
/** Determines whether $element matches the pseudo rule such as nth-child() or [attribute="value"] */
10
class PseudoMatcher {
11
	private $pseudo;
12
	private $valueParser;
13
	private $functionSet;
0 ignored issues
show
Unused Code introduced by
The property $functionSet is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
	private $functions = [];
15
16
	public function __construct($pseudo, \Transphporm\Parser\Value $valueParser) {
17
		$this->pseudo = $pseudo;
18
		$this->valueParser = $valueParser;
19
	}
20
21
	public function registerFunction(\Transphporm\Pseudo $pseudo) {
22
		$this->functions[] = $pseudo;
23
	}
24
25
	public function matches($element) {
26
		$matches = true;
27
28
		foreach ($this->pseudo as $tokens) {
29
			foreach ($this->functions as $function) {
30
				$parts = $this->getFuncParts($tokens);
31
				$matches = $matches && $function->match($parts['name'], $parts['args'], $element);
32
			}
33
		}
34
		return $matches;
35
	}
36
37
	private function getFuncParts($tokens) {
38
		$parts = [];
39
		$parts['name'] = $this->getFuncName($tokens);
40
		if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) {
41
			$parts['args'] = $this->valueParser->parseTokens($tokens);
42
		}
43
		elseif (isset($tokens[1])) $parts['args'] = $this->valueParser->parseTokens($tokens[1]['value']);
44
		else $parts['args'] = [['']];
45
		return $parts;
46
	}
47
48
	private function getFuncName($tokens) {
49
		if ($tokens[0]['type'] === Tokenizer::NAME) return $tokens[0]['value'];
50
		return null;
51
	}
52
53
	public function hasFunction($name) {
54
		foreach ($this->pseudo as $tokens) {
55
			if ($name === $this->getFuncName($tokens)) return true;
56
		}
57
	}
58
59
	public function getFuncArgs($name) {
60
		foreach ($this->pseudo as $tokens) {
61
			$parts = $this->getFuncParts($tokens);
62
			if ($name === $parts['name']) return $parts['args'];
63
		}
64
	}
65
}
66