Completed
Push — master ( 0b7883...3269d2 )
by Tom
02:16
created

Template::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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\TSSFunction;
8
/* Handles template() function calls from the stylesheet */
9
class Template implements \Transphporm\TSSFunction {
10
	private $elementData;
11
	private $baseDir;
0 ignored issues
show
Unused Code introduced by
The property $baseDir is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
	private $xPath;
13
14
	public function __construct(\Transphporm\Hook\ElementData $elementData, \Transphporm\Parser\CssToXpath $xPath, \Transphporm\FilePath $filePath) {
15
		$this->filePath = $filePath;
0 ignored issues
show
Bug introduced by
The property filePath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
		$this->elementData = $elementData;
17
		$this->xPath = $xPath;
18
	}
19
20
	private function readArray($array, $index) {
21
		return isset($array[$index]) ? $array[$index] : null;
22
	}
23
24
	public function run(array $args, \DomElement $element = null) {
25
		$selector = $this->readArray($args, 1);
26
		$tss = $this->readArray($args, 2);
27
28
		if (trim($args[0])[0] === '<') $xml = $args[0];
29
		else $xml = $this->filePath->getFilePath($args[0]);
30
31
		$newTemplate = new \Transphporm\Builder($xml, $tss ? $this->filePath->getFilePath($tss) : null);
32
33
		$doc = $newTemplate->output($this->elementData->getData($element), true)->body;
34
		if ($selector != '') return $this->templateSubsection($doc, $selector);
35
36
		return $this->getTemplateContent($doc->documentElement, $tss);
37
38
	}
39
40
	private function getTemplateContent($newNode, $tss) {
41
		$result = [];
42
		foreach ($newNode->childNodes as $node) {
43
            if (isset($node->tagName) && $node->tagName === 'template') $result[] = $this->getTemplateContent($node, $tss);
44
			else $result[] = $this->getClonedElement($node, $tss);
45
		}
46
		return $result;
47
	}
48
49
	private function templateSubsection($doc, $selector) {
50
		$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
51
		$xpathStr = $this->xPath->getXpath($tokenizer->getTokens());
52
		$xpath = new \DomXpath($doc);
53
		$nodes = $xpath->query($xpathStr);
54
		$result = [];
55
		foreach ($nodes as $node) {
56
			$result[] = $node;
57
		}
58
		return $result;
59
	}
60
61
	private function getClonedElement($node, $tss) {
62
		$clone = $node->cloneNode(true);
63
		if ($tss != null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate');
64
		return $clone;
65
	}
66
}
67