Completed
Push — master ( d42201...aa2fa2 )
by Tom
03:09
created

Nth   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 1
cbo 0
dl 0
loc 29
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B match() 0 16 5
A odd() 0 3 1
A even() 0 3 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
use \Transphporm\Parser\Tokenizer;
9
class Nth implements \Transphporm\Pseudo {
10
	private $count = 0;
11
	private $lastParentNode;
12
13
	public function match($name, $args, \DomElement $element) {
14
		if ($element->parentNode !== $this->lastParentNode) $this->count = 0;
15
16
		$this->lastParentNode = $element->parentNode;
17
18
19
20
		if ($name !== 'nth-child') return true;
21
22
		$this->count++;
23
		$criteria = $args[0];
24
25
		if (is_callable([$this, $criteria])) return $this->$criteria($this->count);
26
		else if (!is_numeric($criteria)) throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int");
27
		else return $this->count == $criteria;
28
	}
29
30
	private function odd($num) {
31
		return $num % 2 === 1;
32
	}
33
34
	private function even($num) {
35
		return $num % 2 === 0;
36
	}
37
}
38