|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SP\Spiderling\Query; |
|
4
|
|
|
|
|
5
|
|
|
use SP\Spiderling\CrawlerInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Filters |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
private $filters; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Matches :text(value), where value is a double or single quoted string |
|
16
|
|
|
* Supports single or double quotes escaping inside |
|
17
|
|
|
* |
|
18
|
|
|
* http://regexr.com/3bqg1 |
|
19
|
|
|
* http://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes |
|
20
|
|
|
*/ |
|
21
|
|
|
const TEXT = '/:text\((["\'])((?:[^\1\\\\]|\\\\.)*?)\1\)/'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Matches :value(value), where value is a double or single quoted string |
|
25
|
|
|
* Supports single or double quotes escaping inside |
|
26
|
|
|
* |
|
27
|
|
|
* http://regexr.com/3bqg1 |
|
28
|
|
|
* http://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes |
|
29
|
|
|
*/ |
|
30
|
|
|
const VALUE = '/:value\((["\'])((?:[^\1\\\\]|\\\\.)*?)\1\)/'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Matches :visible(value), where value is a positive or negative string |
|
34
|
|
|
* Supports true/false, on/off, yes/no, 1/0 |
|
35
|
|
|
* |
|
36
|
|
|
* http://stackoverflow.com/questions/7336861/how-to-convert-string-to-boolean-php |
|
37
|
|
|
*/ |
|
38
|
|
|
const VISIBLE = '/:visible\((([\w]+))\)/'; |
|
39
|
|
|
|
|
40
|
|
|
private static $patterns = [ |
|
41
|
|
|
'text' => Filters::TEXT, |
|
42
|
|
|
'value' => Filters::VALUE, |
|
43
|
|
|
'visible' => Filters::VISIBLE, |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public static function getPatterns() |
|
50
|
|
|
{ |
|
51
|
1 |
|
return self::$patterns; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $filters |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function __construct(array $filters = []) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->filters = $filters; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function all() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->filters; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* @param string $pattern |
|
73
|
|
|
* @param string $selector |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function extractPattern($name, $pattern, $selector) |
|
77
|
|
|
{ |
|
78
|
|
|
if (preg_match($pattern, $selector, $matches)) { |
|
79
|
1 |
|
$this->filters[$name] = $matches[2]; |
|
80
|
|
|
return preg_replace($pattern, '', $selector); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $selector; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $selector |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function extractAllPatterns($selector) |
|
91
|
|
|
{ |
|
92
|
1 |
|
foreach (self::$patterns as $name => $pattern) { |
|
93
|
|
|
$selector = $this->extractPattern($name, $pattern, $selector); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return $selector; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param CrawlerInterface $crawler |
|
101
|
|
|
* @param string $id |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function match(CrawlerInterface $crawler, $id) |
|
105
|
|
|
{ |
|
106
|
2 |
|
foreach ($this->filters as $name => $value) { |
|
107
|
|
|
if (false === $this->$name($crawler, $id, $value)) { |
|
108
|
1 |
|
return false; |
|
109
|
|
|
} |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return true; |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isEmpty() |
|
119
|
|
|
{ |
|
120
|
|
|
return empty($this->filters); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param CrawlerInterface $crawler |
|
125
|
|
|
* @param array $ids |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function matchAll(CrawlerInterface $crawler, array $ids) |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->isEmpty()) { |
|
131
|
1 |
|
return $ids; |
|
132
|
|
|
} else { |
|
133
|
|
|
return array_values( |
|
134
|
|
|
array_filter( |
|
135
|
|
|
$ids, |
|
136
|
1 |
|
function ($id) use ($crawler) { |
|
137
|
|
|
return $this->match($crawler, $id); |
|
138
|
1 |
|
} |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
2 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param CrawlerInterface $crawler |
|
146
|
|
|
* @param string $id |
|
147
|
|
|
* @param string $value |
|
148
|
|
|
* @return boolean |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function value(CrawlerInterface $crawler, $id, $value) |
|
151
|
|
|
{ |
|
152
|
|
|
return $crawler->getValue($id) === (string) $value; |
|
153
|
2 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param CrawlerInterface $crawler |
|
157
|
|
|
* @param string $id |
|
158
|
|
|
* @param boolean $isVisible |
|
159
|
|
|
* @return boolean |
|
160
|
|
|
*/ |
|
161
|
10 |
|
public function visible(CrawlerInterface $crawler, $id, $isVisible = true) |
|
162
|
|
|
{ |
|
163
|
|
|
return $crawler->isVisible($id) === filter_var($isVisible, FILTER_VALIDATE_BOOLEAN); |
|
164
|
10 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param CrawlerInterface $crawler |
|
168
|
|
|
* @param string $id |
|
169
|
|
|
* @param string $text |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
4 |
|
public function text(CrawlerInterface $crawler, $id, $text) |
|
173
|
|
|
{ |
|
174
|
|
|
return false !== mb_stripos($crawler->getText($id), (string) $text); |
|
175
|
4 |
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|