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

PseudoMatcher   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 18
c 12
b 0
f 0
lcom 1
cbo 1
dl 0
loc 55
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A registerFunction() 0 3 1
A matches() 0 10 4
A hasFunction() 0 5 3
A getFuncArgs() 0 10 3
B getBracketType() 0 10 5
A getPseudo() 0 3 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
}