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
|
|
|
/** Determines whether $element matches the pseudo rule such as nth-child() or [attribute="value"] */ |
9
|
|
|
class PseudoMatcher { |
10
|
|
|
private $pseudo; |
11
|
|
|
private $functions = []; |
12
|
|
|
|
13
|
|
|
public function __construct($pseudo) { |
14
|
|
|
$this->pseudo = $pseudo; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function registerFunction(\Transphporm\Pseudo $pseudo) { |
18
|
|
|
$this->functions[] = $pseudo; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function matches($element) { |
22
|
|
|
$matches = true; |
23
|
|
|
|
24
|
|
|
foreach ($this->pseudo as $pseudo) { |
25
|
|
|
foreach ($this->functions as $function) { |
26
|
|
|
$matches = $matches && $function->match($pseudo, $element); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return $matches; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function hasFunction($name) { |
33
|
|
|
foreach ($this->pseudo as $pseudo) { |
34
|
|
|
if (strpos($pseudo, $name) === 0) return true; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getFuncArgs($name) { |
39
|
|
|
foreach ($this->pseudo as $pseudo) { |
40
|
|
|
if (strpos($pseudo, $name) === 0) { |
41
|
|
|
$brackets = $this->getBracketType($pseudo); |
42
|
|
|
$bracketMatcher = new \Transphporm\Parser\BracketMatcher($pseudo); |
43
|
|
|
$ret = $bracketMatcher->match($brackets[0], $brackets[1]); |
44
|
|
|
return $ret; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getBracketType($pseudo) { |
50
|
|
|
$parenthesis = strpos($pseudo, '('); |
51
|
|
|
$square = strpos($pseudo, ']'); |
52
|
|
|
|
53
|
|
|
if ($parenthesis === false) $parenthesis = 999; |
54
|
|
|
if ($square === false) $square = 999; |
55
|
|
|
|
56
|
|
|
if ($parenthesis < $square) return ['(', ')']; |
57
|
|
|
else if ($parenthesis > $square) return ['[', ']']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getPseudo() { |
61
|
|
|
return $this->pseudo; |
62
|
|
|
} |
63
|
|
|
} |