ElementData   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 25
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bind() 0 5 2
A getData() 0 7 4
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