Completed
Push — master ( e6458b...c9bd8d )
by Tom
03:29
created

Attribute::match()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 15
nc 6
nop 2
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 $functionSet;
10
11
	public function __construct(\Transphporm\FunctionSet $functionSet) {
12
		$this->functionSet = $functionSet;
13
	}
14
15
	public function match($pseudo, \DomElement $element) {
16
17
		$pos = strpos($pseudo, '[');
18
		if ($pos === false) return true;
19
20
		$name = substr($pseudo, 0, $pos);
21
		if (!is_callable([$this->functionSet, $name])) return true;
22
23
		$bracketMatcher = new \Transphporm\Parser\BracketMatcher($pseudo);
24
		$criteria = $bracketMatcher->match('[', ']');
25
26
		$valueParser = new \Transphporm\Parser\Value($this->functionSet);
27
28
		$criteria = $name . '(' . $criteria;
29
30
		$pos = strpos($pseudo, '!');
31
		if ($pos === false) $pos = strpos($pseudo, '=');
32
		if ($pos === false) {
33
			$criteria .= ')=true';
34
		}
35
		else $criteria = substr_replace($criteria, ')', $pos, 0);
36
37
		return $valueParser->parse($criteria, $element)[0];
38
	}
39
}