1 | <?php |
||
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 | $this->assert(is_numeric($criteria), "Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
||
27 | return $this->count == $criteria; |
||
28 | } |
||
29 | |||
30 | //TODO: Abstract assertions throughout |
||
31 | private function assert($condition, $error) { |
||
32 | if (!$condition) throw new \Exception($error); |
||
33 | } |
||
34 | |||
35 | private function odd($num) { |
||
38 | |||
39 | private function even($num) { |
||
42 | } |
||
43 |