Completed
Push — master ( 49caad...2d4906 )
by Tom
9s
created

PropertyHook   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getArgs() 0 3 1
A registerProperty() 0 3 1
A callProperty() 0 5 3
B run() 0 11 5
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 $origBaseDir;
12
	private $newBaseDir;
13
	private $valueParser;
14
	private $pseudoMatcher;
15
	private $properties = [];
16
	private $functionSet;
17
18
	public function __construct(array $rules, &$origBaseDir, $newBaseDir, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) {
19
		$this->rules = $rules;
20
		$this->origBaseDir = $origBaseDir;
21
		$this->newBaseDir = $newBaseDir;
22
		$this->valueParser = $valueParser;
23
		$this->pseudoMatcher = $pseudoMatcher;
24
		$this->functionSet = $functionSet;
25
	}
26
27
	public function run(\DomElement $element) {
28
		$this->functionSet->setElement($element);
29
		if ($this->origBaseDir !== $this->newBaseDir) $this->origBaseDir = $this->newBaseDir;
30
		//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
31
		if (!$this->pseudoMatcher->matches($element)) return;
32
33
		foreach ($this->rules as $name => $value) {
34
			$result = $this->callProperty($name, $element, $this->getArgs($value, $element));
35
			if ($result === false) break;
36
		}
37
	}
38
39
	private function getArgs($value, $element) {
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
		return $this->valueParser->parse($value);
41
	}
42
43
	public function registerProperty($name, \Transphporm\Property $property) {
44
		$this->properties[$name] = $property;
45
	}
46
47
	private function callProperty($name, $element, $value) {
48
		if (empty($value[0])) $value[0] = [];
49
		if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties);
50
		return false;
51
	}
52
}
53