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

FunctionSet::setElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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;
8
/* Handles data() and iteration() function calls from the stylesheet */
9
class FunctionSet {
10
	private $elementData;
11
	private $functions = [];
12
	private $element;
13
14
	public function __construct(Hook\ElementData $elementData) {
15
		$this->elementData = $elementData;
16
	}
17
18
	public function __call($name, $args) {
19
		try {
20
			if (!($this->functions[$name] instanceof TSSFunction\Data)) {
21
				$tokens = $args[0];
22
				$parser = new \Transphporm\Parser\Value($this);
23
				$args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element));
24
			}
25
			else if ($args[0] instanceof Parser\Tokens) {
26
				$args[0] = iterator_to_array($args[0]);
27
			}
28
			if (isset($this->functions[$name])) {
29
				return $this->functions[$name]->run($args[0], $this->element);
30
			}
31
		}
32
		catch (\Exception $e) {
33
			throw new RunException(Exception::TSS_FUNCTION, $name, $e);
34
		}
35
		return true;
36
	}
37
38
	public function addFunction($name, \Transphporm\TSSFunction $function) {
39
		$this->functions[$name] = $function;
40
	}
41
42
	public function hasFunction($name) {
43
		return isset($this->functions[$name]);
44
	}
45
46
	public function setElement(\DomElement $element) {
47
		$this->element = $element;
48
	}
49
50
}
51