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

Attribute   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 6
c 7
b 2
f 0
lcom 1
cbo 2
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B match() 0 24 5
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
}