Completed
Push — master ( 39abe4...b867be )
by Richard
02:31
created

PropertyHook::run()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 11
nc 12
nop 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
/** 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 $baseDir;
12
	private $configLine;
13
	private $file;
14
	private $line;
15
	private $valueParser;
16
	private $pseudoMatcher;
17
	private $properties = [];
18
	private $functionSet;
19
20 View Code Duplication
	public function __construct(array $rules, &$baseDir, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
		$this->rules = $rules;
22
		$this->baseDir = &$baseDir;
23
		$this->configLine = &$configLine;
24
		$this->file = $file;
25
		$this->line = $line;
26
		$this->valueParser = $valueParser;
27
		$this->pseudoMatcher = $pseudoMatcher;
28
		$this->functionSet = $functionSet;
29
	}
30
31
	public function run(\DomElement $element) {
32
		$this->functionSet->setElement($element);
33
		if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR;
34
		$this->configLine = $this->line;
35
		try {
36
			//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
37
			if (!$this->pseudoMatcher->matches($element)) return;
38
39
			// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens
40
			// problem with this is that anything in data changed by run properties is not shown
41
			// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check)
42
43
			foreach ($this->rules as $name => $value) {
44
				$result = $this->callProperty($name, $element, $this->getArgs($value));
45
				if ($result === false) break;
46
			}
47
		}
48
		catch (\Transphporm\RunException $e) {
49
			throw new \Transphporm\Exception($e, $this->file, $this->line);
50
		}
51
	}
52
53
	private function getArgs($value) {
54
		return $this->valueParser->parseTokens($value);
55
	}
56
57
	public function registerProperty($name, \Transphporm\Property $property) {
58
		$this->properties[$name] = $property;
59
	}
60
61
	private function callProperty($name, $element, $value) {
62
		if (isset($this->properties[$name])) {
63
			try {
64
				return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties);
65
			}
66
			catch (\Exception $e) {
67
				if ($e instanceof \Transphporm\RunException) throw $e;
68
				throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e);
69
			}
70
		}
71
	}
72
}
73