Completed
Push — master ( 678b45...fc8189 )
by Tom
03:19
created

Rule::extractQuotedString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
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
/** Hooks into the template system, gets assigned as `ul li` or similar and `run()` is called with any elements that match */
9
class Rule implements \Transphporm\Hook {
10
	private $rules;
11
	private $valueParser;
12
	private $pseudoMatcher;
13
	private $properties = [];
14
15
	public function __construct(array $rules, PseudoMatcher $pseudoMatcher, \Transphporm\ValueParser $valueParser) {
16
		$this->rules = $rules;
17
		$this->valueParser = $valueParser;
18
		$this->pseudoMatcher = $pseudoMatcher;
19
	}
20
21
	public function run(\DomElement $element) {	
22
		//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
23
		if (!$this->pseudoMatcher->matches($element)) return;
24
25
		foreach ($this->rules as $name => $value) {
26
			$result = $this->callProperty($name, $element, $this->valueParser->parse(trim($value), $element));
27
			if ($result === false) break;
28
		}
29
	}
30
31
	public function getPseudoMatcher() {
32
		return $this->pseudoMatcher;
33
	}
34
35
	public function getRules() {
36
		return $this->rules;
37
	}
38
39
	public function registerProperty($name, \Transphporm\Property $property) {
40
		$this->properties[$name] = $property;
41
	}
42
43
	public function getProperties() {
44
		return $this->properties;
45
	}
46
47
	private function callProperty($name, $element, $value) {
48
		if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this);
49
		return false;
50
	}
51
52
}
53