Completed
Push — master ( 3269d2...e97281 )
by Tom
02:21
created

PseudoMatcher   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 67
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerFunction() 0 3 1
A matches() 0 9 4
A match() 0 10 3
A getFuncParts() 0 14 4
A getFuncName() 0 4 2
A hasFunction() 0 5 3
A getFuncArgs() 0 6 3
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 $functions = [];
14
15
	public function __construct($pseudo, \Transphporm\Parser\Value $valueParser) {
16
		$this->pseudo = $pseudo;
17
		$this->valueParser = $valueParser;
18
	}
19
20
	public function registerFunction(\Transphporm\Pseudo $pseudo) {
21
		$this->functions[] = $pseudo;
22
	}
23
24
	public function matches($element) {
25
		foreach ($this->pseudo as $tokens) {
26
			foreach ($this->functions as $function) {
27
				$matches = $this->match($tokens, $function, $element);
28
				if ($matches === false) return false;
29
			}
30
		}
31
		return true;
32
	}
33
34
	private function match($tokens, $function, $element) {
35
		try {
36
			$parts = $this->getFuncParts($tokens);
37
			$matches = $function->match($parts['name'], $parts['args'], $element);
38
			if ($matches === false) return false;
39
		}
40
		catch (\Exception $e) {
41
			throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e);
42
		}
43
	}
44
	private function getFuncParts($tokens) {
45
		$parts = [];
46
		$parts['name'] = $this->getFuncName($tokens);
47
		if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) {
48
			$parts['args'] = $this->valueParser->parseTokens($tokens);
49
		}
50
		else if (count($tokens) > 1) {
51
			$tokens->rewind();
52
			$tokens->next();
53
			$parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']);
54
		}
55
		else $parts['args'] = [['']];
56
		return $parts;
57
	}
58
59
	private function getFuncName($tokens) {
60
		if ($tokens->type() === Tokenizer::NAME) return $tokens->read();
61
		return null;
62
	}
63
64
	public function hasFunction($name) {
65
		foreach ($this->pseudo as $tokens) {
66
			if ($name === $this->getFuncName($tokens)) return true;
67
		}
68
	}
69
70
	public function getFuncArgs($name) {
71
		foreach ($this->pseudo as $tokens) {
72
			$parts = $this->getFuncParts($tokens);
73
			if ($name === $parts['name']) return $parts['args'];
74
		}
75
	}
76
}
77