FunctionSet::setElement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
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
		if (isset($this->functions[$name])) {
20
			return $this->functions[$name]->run($this->getArgs0($name, $args), $this->element);
21
		}
22
23
		return false;
24
	}
25
26
	private function getArgs0($name, $args) {
27
		if (isset($this->functions[$name]) && !($this->functions[$name] instanceof TSSFunction\Data)) {
28
			$tokens = $args[0];
29
			if ($tokens->count() == 0) return [];
30
			$parser = new \Transphporm\Parser\Value($this);
31
			return $parser->parseTokens($tokens, $this->elementData->getData($this->element));
32
		}
33
		else {
34
			return iterator_to_array($args[0]);
35
		}
36
37
	}
38
39
	public function addFunction($name, \Transphporm\TSSFunction $function) {
40
		$this->functions[$name] = $function;
41
	}
42
43
	public function hasFunction($name) {
44
		return isset($this->functions[$name]);
45
	}
46
47
	public function setElement(\DomElement $element) {
48
		$this->element = $element;
49
	}
50
51
}
52