Completed
Push — master ( 9caa90...8321d8 )
by Tom
02:40
created

PropertyHook   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 44
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 9 4
A getPseudoMatcher() 0 3 1
A getRules() 0 3 1
A registerProperty() 0 3 1
A getProperties() 0 3 1
A callProperty() 0 4 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 PropertyHook 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\Parser\Value $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