ElementData::bind()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 3
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\Hook;
8
/* Maps which data is applied to which element */
9
class ElementData {
10
	private $data;
11
	private $elementMap;
12
13
	public function __construct(\SplObjectStorage $elementMap, $data) {
14
		$this->elementMap = $elementMap;
15
		$this->data = $data;
16
	}
17
18
	/** Binds data to an element */
19
	public function bind(\DomNode $element, $data, $type = 'data') {
20
		$content = isset($this->elementMap[$element]) ? $this->elementMap[$element] : [];
21
		$content[$type] = $data;
22
		$this->elementMap[$element] = $content;
23
	}
24
25
	/** Returns the data that has been bound to $element, or, if no data is bound to $element climb the DOM tree to find the data bound to a parent node*/
26
	public function getData(\DomElement $element = null, $type = 'data') {
27
		while ($element) {
28
			if (isset($this->elementMap[$element]) && array_key_exists($type, $this->elementMap[$element])) return $this->elementMap[$element][$type];
29
			$element = $element->parentNode;
30
		}
31
		return $this->data;
32
	}
33
}
34