Completed
Push — master ( d872cd...720a5f )
by Richard
02:14
created

Not::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
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\Pseudo;
8
class Not implements \Transphporm\Pseudo {
9
	private $cssToXpath;
10
    private $config;
11
12
	public function __construct(\Transphporm\Parser\CssToXpath $cssToXpath, \Transphporm\Config $config) {
13
		$this->cssToXpath = $cssToXpath;
14
        $this->config = $config;
15
	}
16
17
	public function match($name, $args, \DomElement $element) {
18
		if ($name !== 'not') return true;
19
20
		$xpath = new \DomXpath($element->ownerDocument);
21
		return $this->notElement($args, $xpath, $element);
22
	}
23
24
	private function notElement($css, $xpath, $element) {
25
26
		foreach ($css as $selector) {
27
			$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
28
			$xpathString = $this->cssToXpath->getXpath($tokenizer->getTokens());
29
            $pseudo = $this->cssToXpath->getPseudo($tokenizer->getTokens());
30
            $pseudoMatcher = $this->config->createPseudoMatcher($pseudo);
31
			//Find all nodes matched by the expressions in the brackets :not(EXPR)
32
			foreach ($xpath->query($xpathString) as $matchedElement) {
33
				//Check to see whether this node was matched by the not query
34
				if ($pseudoMatcher->matches($matchedElement) && $element->isSameNode($matchedElement)) return false;
35
			}
36
		}
37
		return true;
38
	}
39
}
40