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
|
|
|
|