Completed
Push — master ( 52b075...cc4ebe )
by Tom
03:34
created

Attribute::getOperator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
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\Pseudo;
8
class Attribute implements \Transphporm\Pseudo {
9
	private $dataFunction;
10
11
	public function __construct(\Transphporm\Hook\DataFunction $dataFunction) {
12
		$this->dataFunction = $dataFunction;
13
	}
14
15
	public function match($pseudo, \DomElement $element) {
16
		$pos = strpos($pseudo, '[');
17
		if ($pos === false) return true;
18
		
19
		$name = substr($pseudo, 0, $pos);
20
		if (!is_callable([$this->dataFunction, $name])) return true;
21
22
		$bracketMatcher = new \Transphporm\Parser\BracketMatcher($pseudo);
23
		$criteria = $bracketMatcher->match('[', ']');
24
25
		if (strpos($pseudo, '=') === false) {
26
			$lookupValue = $this->dataFunction->$name([$criteria], $element);
27
			return $lookupValue !== null;
28
		}
29
		list ($field, $value) = explode('=', $criteria);
30
31
		$operator = $this->getOperator($field);
32
		$lookupValue = $this->dataFunction->$name([trim($field, $operator)], $element);
33
34
		return $this->processOperator($operator, $lookupValue, $this->parseValue(trim($value, '"')));
35
	}
36
37
38
	//Currently only not is supported, but this is separated out to support others in future
39
	private function processOperator($operator, $lookupValue, $value) {
40
		$matched = $lookupValue == $value;
41
		return $operator === '!' ? !$matched : $matched;
42
	}
43
44
	private function parseValue($value) {
45
		if ($value == 'true') return true;
46
		else if ($value == 'false') return false;
47
		else return $value;
48
	}
49
50
	private function getOperator($field) {
51
		if ($field[strlen($field)-1] == '!') {
52
			return '!';
53
		}
54
		else return '';
55
	}
56
57
}