1
|
|
|
<?php |
|
|
|
|
2
|
|
|
require_once("mod_selector/nodes.php"); |
3
|
|
|
require_once("mod_selector/parser.php"); |
4
|
|
|
|
5
|
|
|
class selector { |
6
|
|
|
protected $nodes; |
7
|
|
|
|
8
|
|
|
public function __construct($string) { |
9
|
|
|
$parser = new selectorParser($string); |
10
|
|
|
$this->nodes = $parser->parse(); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function run($count, $offset = 0, $definitions = Array()) { |
14
|
|
|
if (!$this->nodes) { |
15
|
|
|
return false; |
16
|
|
|
} |
17
|
|
|
if ($offset >= $count) { |
18
|
|
|
return false; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$result = $this->nodes->run($count, $offset); |
22
|
|
|
if (isset($definitions[$result])) { |
23
|
|
|
$result = $definitions[$result]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $result; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getIterator($count, $definitions = Array()) { |
30
|
|
|
return new selectorIterator($this, $count, $definitions); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
class selectorIterator implements Iterator { |
|
|
|
|
36
|
|
|
|
37
|
|
|
public function __construct($selector, $count, $definitions = Array()) { |
38
|
|
|
$this->selector = $selector; |
|
|
|
|
39
|
|
|
$this->count = $count; |
|
|
|
|
40
|
|
|
$this->offset = 0; |
|
|
|
|
41
|
|
|
$this->definitions = $definitions; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function current() { |
45
|
|
|
return $this->selector->run($this->count, $this->offset, $this->definitions); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function key() { |
49
|
|
|
return $this->offset; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function next() { |
53
|
|
|
$this->offset = $this->offset + 1; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function rewind() { |
57
|
|
|
$this->offset = 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function valid() { |
61
|
|
|
return ($this->offset >= 0); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
class pinp_selector extends selector { |
|
|
|
|
67
|
|
|
|
68
|
|
|
public function _create($string) { |
69
|
|
|
return new pinp_selector($string); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function _run($count, $offset = 0, $definitions = Array()) { |
73
|
|
|
return $this->run($count, $offset, $definitions); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function _getIterator($count, $definitions = Array()) { |
77
|
|
|
return new pinp_selectorIterator($count, $definitions); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
class pinp_selectorIterator extends selectorIterator { |
|
|
|
|
84
|
|
|
|
85
|
|
|
public function __construct($selector, $count, $definitions = Array()) { |
86
|
|
|
return parent::__construct($selector, $count, $definitions); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function _current() { |
90
|
|
|
return $this->current(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function _key() { |
94
|
|
|
return $this->key(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function _next() { |
98
|
|
|
return $this->next(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function _rewind() { |
102
|
|
|
return $this->rewind(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function _valid() { |
106
|
|
|
return $this->valid(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.