1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xparse\RecursivePagination; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Xparse\ElementFinder\ElementFinder; |
7
|
|
|
use Xparse\Parser\Parser; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @package Xparse\RecursivePagination |
12
|
|
|
*/ |
13
|
|
|
class RecursivePagination { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $queue = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Parser|null |
22
|
|
|
*/ |
23
|
|
|
protected $parser = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $elementSelector = []; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Parser $parser |
33
|
|
|
* @param string|array $expression |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Parser $parser, $expression) { |
36
|
|
|
$this->parser = $parser; |
37
|
|
|
|
38
|
|
|
$this->setExpression($expression); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array|string $links |
44
|
|
|
* @param bool $state |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function addToQueue($links, $state = false) { |
49
|
|
|
if (!is_string($links) and !is_array($links)) { |
50
|
|
|
throw new \InvalidArgumentException('Links should be an array or a string'); |
51
|
|
|
} |
52
|
|
|
$links = (array) $links; |
53
|
|
|
foreach ($links as $url) { |
54
|
|
|
if (!is_string($url)) { |
55
|
|
|
throw new \InvalidArgumentException('url should be a string'); |
56
|
|
|
} |
57
|
|
|
$this->queue[$url] = $state; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return ElementFinder|null |
66
|
|
|
*/ |
67
|
|
|
public function getNextPage() { |
68
|
|
|
|
69
|
|
|
if (func_num_args() > 0) { |
70
|
|
|
trigger_error('This method doesn\'t have arguments', E_USER_DEPRECATED); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$page = $this->parser->getLastPage(); |
74
|
|
|
if (!empty($page)) { |
75
|
|
|
foreach ($this->elementSelector as $expression => $state) { |
76
|
|
|
$queueLinks = $page->attribute($expression)->getItems(); |
77
|
|
|
if (!empty($queueLinks)) { |
78
|
|
|
$queueLinks = array_combine($queueLinks, array_fill(0, count($queueLinks), false)); |
79
|
|
|
$this->queue = array_merge($queueLinks, $this->queue); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$link = array_search(false, $this->queue, true); |
84
|
|
|
|
85
|
|
|
if (empty($link)) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->queue[$link] = true; |
90
|
|
|
return $this->parser->get($link); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string|array $expression |
96
|
|
|
* @throws \InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
private function setExpression($expression) { |
99
|
|
|
|
100
|
|
|
if (!is_string($expression) and !is_array($expression) or empty($expression)) { |
101
|
|
|
throw new \InvalidArgumentException('Invalid expression, should be not empty array or string'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$expression = (array) $expression; |
105
|
|
|
foreach ($expression as $path) { |
106
|
|
|
if (!is_string($path) or empty($path)) { |
107
|
|
|
throw new \InvalidArgumentException('Invalid expression, should be not empty string'); |
108
|
|
|
} |
109
|
|
|
$this->elementSelector[$path] = true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |