Completed
Push — master ( 71ae65...4e11a6 )
by Tom
02:20
created

FunctionSet::getArgs0()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 3
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;
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 (isset($this->functions[$name])) {
21
				return $this->functions[$name]->run($this->getArgs0($name, $args), $this->element);
22
			}
23
		}
24
		catch (\Exception $e) {
25
			throw new RunException(Exception::TSS_FUNCTION, $name, $e);
26
		}
27
		return true;
28
	}
29
30
	private function getArgs0($name, $args) {
31
		if (isset($this->functions[$name]) && !($this->functions[$name] instanceof TSSFunction\Data)) {
32
			$tokens = $args[0];
33
			$parser = new \Transphporm\Parser\Value($this);
34
			return $parser->parseTokens($tokens, $this->elementData->getData($this->element));
35
		}
36
		else if ($args[0] instanceof Parser\Tokens) {
37
			return iterator_to_array($args[0]);
38
		}
39
40
		return $args[0];
41
	}
42
43
	public function addFunction($name, \Transphporm\TSSFunction $function) {
44
		$this->functions[$name] = $function;
45
	}
46
47
	public function hasFunction($name) {
48
		return isset($this->functions[$name]);
49
	}
50
51
	public function setElement(\DomElement $element) {
52
		$this->element = $element;
53
	}
54
55
}
56