1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lbc\Crawler; |
4
|
|
|
|
5
|
|
|
use Lbc\Filter\DefaultSanitizer; |
6
|
|
|
use Lbc\Filter\PrixSanitizer; |
7
|
|
|
use Lbc\Parser\AdUrlParser; |
8
|
|
|
use Lbc\Parser\SearchResultUrlParser; |
9
|
|
|
use League\Uri\Schemes\Http; |
10
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SearchResultAdCrawler |
14
|
|
|
* @package Lbc\Crawler |
15
|
|
|
*/ |
16
|
|
|
class SearchResultAdCrawler extends CrawlerAbstract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param $url |
20
|
|
|
* @return SearchResultUrlParser |
21
|
|
|
*/ |
22
|
10 |
|
protected function setUrlParser($url) |
23
|
|
|
{ |
24
|
10 |
|
$this->url = new AdUrlParser($url); |
25
|
10 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return the Ad's ID |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
10 |
|
public function getId() |
33
|
|
|
{ |
34
|
10 |
|
return $this->url->getId(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return the title |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
10 |
|
public function getTitle() |
44
|
|
|
{ |
45
|
10 |
|
return DefaultSanitizer::clean($this->node->filter('h2')->text()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return the price |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
10 |
|
public function getPrice() |
54
|
|
|
{ |
55
|
10 |
|
if ($this->node->filter('*[itemprop=price]')->count()) { |
56
|
10 |
|
return PrixSanitizer::clean( |
57
|
10 |
|
$this->node->filter('*[itemprop=price]')->text() |
58
|
5 |
|
); |
59
|
|
|
} |
60
|
|
|
return 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the Ad's URL |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
10 |
|
public function getUrl() |
69
|
|
|
{ |
70
|
10 |
|
return (string)Http::createFromString($this->url)->withScheme('https'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return the data and time the ad was created |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
10 |
|
public function getCreatedAt() |
79
|
|
|
{ |
80
|
10 |
|
$node = $this->node |
81
|
10 |
|
->filter('*[itemprop=availabilityStarts]') |
82
|
10 |
|
->first(); |
83
|
|
|
|
84
|
10 |
|
$date = $node->attr('content'); |
85
|
|
|
|
86
|
|
|
$time = $this->getFieldValue($node, 0, function ($value) { |
87
|
10 |
|
$value = trim($value); |
88
|
|
|
|
89
|
10 |
|
return substr($value, strpos($value, ',') + 2); |
90
|
10 |
|
}); |
91
|
|
|
|
92
|
10 |
|
return $date . ' ' . $time; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the thumb picture url |
97
|
|
|
* |
98
|
|
|
* @return null|string |
99
|
|
|
*/ |
100
|
10 |
|
public function getThumb() |
101
|
|
|
{ |
102
|
10 |
|
$image = $this->node |
103
|
10 |
|
->filter('.item_imagePic .lazyload[data-imgsrc]') |
104
|
10 |
|
->first(); |
105
|
|
|
|
106
|
10 |
|
if (0 === $image->count()) { |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$src = $image |
111
|
10 |
|
->attr('data-imgsrc'); |
112
|
|
|
|
113
|
10 |
|
return (string)Http::createFromString($src)->withScheme('https'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the number of picture of the ad |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
10 |
|
public function getNbImage() |
122
|
|
|
{ |
123
|
10 |
|
$node = $this->node->filter('.item_imageNumber'); |
124
|
|
|
|
125
|
|
|
return $this->getFieldValue($node, 0, function ($value) { |
126
|
10 |
|
return (int)trim($value); |
127
|
10 |
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
10 |
View Code Duplication |
public function getPlacement() |
|
|
|
|
134
|
|
|
{ |
135
|
10 |
|
$node = $this->node->filter('*[itemprop=availableAtOrFrom]'); |
136
|
|
|
|
137
|
|
|
return $this->getFieldValue($node, '', function ($value) { |
138
|
10 |
|
return preg_replace('/\s+/', ' ', trim($value)); |
139
|
10 |
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
10 |
View Code Duplication |
public function getType() |
|
|
|
|
146
|
|
|
{ |
147
|
10 |
|
$node = $this->node->filter('*[itemprop=category]'); |
148
|
|
|
|
149
|
10 |
|
return $this->getFieldValue($node, false, function ($value) { |
150
|
10 |
|
if ('pro' === preg_replace('/[\s()]+/', '', $value)) { |
151
|
8 |
|
return 'pro'; |
152
|
|
|
} |
153
|
|
|
|
154
|
10 |
|
return 'part'; |
155
|
10 |
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return object |
160
|
|
|
*/ |
161
|
10 |
|
public function getAll() |
162
|
|
|
{ |
163
|
|
|
return (object)[ |
164
|
10 |
|
'id' => $this->getId(), |
165
|
10 |
|
'title' => $this->getTitle(), |
166
|
10 |
|
'price' => $this->getPrice(), |
167
|
10 |
|
'url' => $this->getUrl(), |
168
|
10 |
|
'created_at' => $this->getCreatedAt(), |
169
|
10 |
|
'thumb' => $this->getThumb(), |
170
|
10 |
|
'nb_image' => $this->getNbImage(), |
171
|
10 |
|
'placement' => $this->getPlacement(), |
172
|
10 |
|
'type' => $this->getType(), |
173
|
5 |
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return the field's value |
178
|
|
|
* |
179
|
|
|
* @param Crawler $node |
180
|
|
|
* @param mixed $defaultValue |
181
|
|
|
* @param \Closure $callback |
182
|
|
|
* @param string $funcName |
183
|
|
|
* @param string $funcParam |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
10 |
|
private function getFieldValue( |
188
|
|
|
Crawler $node, |
189
|
|
|
$defaultValue, |
190
|
|
|
$callback, |
191
|
|
|
$funcName = 'text', |
192
|
|
|
$funcParam = '' |
193
|
|
|
) { |
194
|
10 |
|
if ($node->count()) { |
195
|
10 |
|
return $callback($node->$funcName($funcParam)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $defaultValue; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: