Completed
Push — master ( 52b075...cc4ebe )
by Tom
03:34
created

PseudoMatcher::hasFunction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 3
eloc 3
nc 3
nop 1
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
}