1
|
|
|
<?php |
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
3
|
|
|
* @author Tom Butler [email protected] * |
4
|
|
|
* @copyright 2017 Tom Butler <[email protected]> | https://r.je/ * |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
6
|
|
|
* @version 1.2 */ |
7
|
|
|
namespace Transphporm; |
8
|
|
|
use Transphporm\Parser\Tokenizer; |
9
|
|
|
class TSSValidator { |
10
|
|
|
private $error; |
11
|
|
|
|
12
|
|
|
public function validate($tss) { |
13
|
|
|
$this->error = null; |
14
|
|
|
$tokens = $this->tokenize($tss); |
15
|
|
|
|
16
|
|
|
foreach ($tokens as $token) |
17
|
|
|
if (!$this->validateRule($token)) return false; |
18
|
|
|
|
19
|
|
|
return true; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
private function validateRule($token) { |
23
|
|
|
if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
24
|
|
|
|
25
|
|
|
return $this->checkBraces($token) && $this->checkSemicolons($token) |
26
|
|
|
&& $this->checkParenthesis($token); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function checkBraces($token) { |
30
|
|
|
return strpos($token['string'], '{') === false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function checkSemicolons($braceToken) { |
34
|
|
|
$splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
35
|
|
|
array_shift($splitTokens); array_pop($splitTokens); |
36
|
|
|
foreach ($splitTokens as $tokens) |
37
|
|
|
if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
38
|
|
|
|
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function checkParenthesis($token) { |
43
|
|
|
return substr_count($token['string'], '(') === substr_count($token['string'], ')'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function tokenize($tss) { |
47
|
|
|
if (is_file($tss)) $tss = file_get_contents($tss); |
48
|
|
|
return (new Parser\Tokenizer($tss))->getTokens(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|