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
|
|
|
/* Handles template() function calls from the stylesheet */ |
9
|
|
|
class TemplateFunction { |
10
|
|
|
private $xml; |
11
|
|
|
private $selector; |
12
|
|
|
private $tss; |
13
|
|
|
|
14
|
|
|
public function __construct($xml, $selector = null, $tss = null) { |
15
|
|
|
$this->xml = $xml; |
16
|
|
|
$this->selector = $selector; |
17
|
|
|
$this->tss = $tss; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function templateSubsection($doc) { |
21
|
|
|
$xpathStr = (new \Transphporm\Parser\CssToXpath($this->selector, new \Transphporm\Parser\Value($this)))->getXpath(); |
22
|
|
|
$xpath = new \DomXpath($doc); |
23
|
|
|
$nodes = $xpath->query($xpathStr); |
24
|
|
|
$result = []; |
25
|
|
|
foreach ($nodes as $node) { |
26
|
|
|
$result[] = $node; |
27
|
|
|
} |
28
|
|
|
return $result; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function getTss($val) { |
|
|
|
|
32
|
|
|
return isset($val[2]) ? $val[2] : null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function getClonedElement($node) { |
36
|
|
|
$clone = $node->cloneNode(true); |
37
|
|
|
if ($this->tss !== null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate'); |
38
|
|
|
return $clone; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getTemplateNodes($data) { |
42
|
|
|
$newTemplate = new \Transphporm\Builder($this->xml, $this->tss); |
43
|
|
|
|
44
|
|
|
$doc = $newTemplate->output($data, true)->body; |
45
|
|
|
if ($this->selector != '') return $this->templateSubsection($doc); |
46
|
|
|
|
47
|
|
|
$newNode = $doc->documentElement; |
48
|
|
|
$result = []; |
49
|
|
|
if ($newNode->tagName === 'template') { |
50
|
|
|
foreach ($newNode->childNodes as $node) { |
51
|
|
|
$result[] = $this->getClonedElement($node); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
//else $result[] = $newNode; |
|
|
|
|
55
|
|
|
return $result; |
56
|
|
|
} |
57
|
|
|
} |