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\Parser; |
8
|
|
|
class CssToXpath { |
9
|
|
|
private $specialChars = [' ', '.', '>', '~', '#', ':', '[', ']']; |
10
|
|
|
private $translators = []; |
11
|
|
|
private $css; |
12
|
|
|
private $depth; |
13
|
|
|
private static $instances = []; |
14
|
|
|
|
15
|
|
|
public function __construct($css, Value $valueParser, $prefix = '') { |
16
|
|
|
$hash = $this->registerInstance(); |
17
|
|
|
$this->valueParser = $valueParser; |
|
|
|
|
18
|
|
|
$this->css = str_replace([' >', '> '],['>', '>'], trim($css)); |
19
|
|
|
$this->translators = [ |
20
|
|
|
' ' => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
21
|
|
|
'' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
22
|
|
|
'>' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
23
|
|
|
'#' => function($string) { return '[@id=\'' . $string . '\']'; }, |
24
|
|
|
'.' => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
25
|
|
|
'[' => function($string, $xpath) use($hash) { return '[' .'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \'' . $string . '\', ., "' . $hash . '")' . ']'; }, |
|
|
|
|
26
|
|
|
']' => function() { return ''; } |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function __destruct() { |
31
|
|
|
unset(self::$instances[spl_object_hash($this)]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function registerInstance() { |
35
|
|
|
$hash = spl_object_hash($this); |
36
|
|
|
self::$instances[$hash] = $this; |
37
|
|
|
return $hash; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function createSelector() { |
41
|
|
|
$selector = new \stdclass; |
42
|
|
|
$selector->type = ''; |
43
|
|
|
$selector->string = ''; |
44
|
|
|
return $selector; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
//XPath only allows registering of static functions... this is a hacky workaround for that |
48
|
|
|
public static function processAttr($attr, $element, $hash) { |
49
|
|
|
$comparators = ['!=', '=']; |
50
|
|
|
$valueParser = self::$instances[$hash]->valueParser; |
51
|
|
|
foreach ($comparators as $comparator) { |
52
|
|
|
if (strpos($attr, $comparator) !== false) { |
53
|
|
|
$parts = explode($comparator, $attr); |
54
|
|
|
$parts = array_map(function($val) use ($valueParser, $element) { |
55
|
|
|
return $valueParser->parse($val, $element[0])[0]; |
56
|
|
|
}, $parts); |
57
|
|
|
|
58
|
|
|
return self::compare($comparator, $element[0]->getAttribute($parts[0]), $parts[1]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return $attr; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private static function compare($comparator, $a, $b) { |
65
|
|
|
if ($comparator == '=') return $a == $b; |
66
|
|
|
else if ($comparator == '!=') return $a != $b; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//split the css into indivudal functions |
70
|
|
|
private function split($css) { |
71
|
|
|
$selectors = []; |
72
|
|
|
$selector = $this->createSelector(); |
73
|
|
|
$selectors[] = $selector; |
74
|
|
|
|
75
|
|
|
for ($i = 0; $i < strlen($css); $i++) { |
76
|
|
|
if (in_array($css[$i], $this->specialChars)) { |
77
|
|
|
$selector = $this->createSelector(); |
78
|
|
|
$selector->type = $css[$i]; |
79
|
|
|
$selectors[] = $selector; |
80
|
|
|
} |
81
|
|
|
else $selector->string .= $css[$i]; |
82
|
|
|
} |
83
|
|
|
return $selectors; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getXpath() { |
87
|
|
|
$css = explode(':', $this->css)[0]; |
88
|
|
|
$selectors = $this->split($css); |
89
|
|
|
$this->depth = count($selectors); |
90
|
|
|
$xpath = '/'; |
91
|
|
|
foreach ($selectors as $selector) { |
92
|
|
|
if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$xpath = str_replace('/[', '/*[', $xpath); |
96
|
|
|
|
97
|
|
|
return $xpath; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getDepth() { |
101
|
|
|
return $this->depth; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getPseudo() { |
105
|
|
|
$parts = explode(':', $this->css); |
106
|
|
|
array_shift($parts); |
107
|
|
|
return $parts; |
108
|
|
|
} |
109
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: