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
|
|
|
|