1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lbc\Crawler; |
4
|
|
|
|
5
|
|
|
use Lbc\Filter\DefaultSanitizer; |
6
|
|
|
use Lbc\Filter\PriceSanitizer; |
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
|
|
|
* Return the title |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
10 |
|
public function getTitle() |
43
|
|
|
{ |
44
|
10 |
|
return DefaultSanitizer::clean($this->node->filter('h2')->text()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the price |
49
|
|
|
* |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
10 |
|
public function getPrice() |
53
|
|
|
{ |
54
|
10 |
|
if ($this->node->filter('*[itemprop=price]')->count()) { |
55
|
10 |
|
return PriceSanitizer::clean( |
56
|
10 |
|
$this->node->filter('*[itemprop=price]')->text() |
57
|
10 |
|
); |
58
|
|
|
} |
59
|
|
|
return 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the Ad's URL |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
10 |
|
public function getUrl() |
68
|
|
|
{ |
69
|
10 |
|
return (string)Http::createFromString($this->url)->withScheme('https'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the data and time the ad was created |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
10 |
|
public function getCreatedAt() |
78
|
|
|
{ |
79
|
10 |
|
$node = $this->node |
80
|
10 |
|
->filter('*[itemprop=availabilityStarts]') |
81
|
10 |
|
->first(); |
82
|
|
|
|
83
|
10 |
|
$date = $node->attr('content'); |
84
|
|
|
|
85
|
|
|
$time = $this->getFieldValue($node, 0, function ($value) { |
86
|
10 |
|
$value = trim($value); |
87
|
|
|
|
88
|
10 |
|
return substr($value, strpos($value, ',') + 2); |
89
|
10 |
|
}); |
90
|
|
|
|
91
|
10 |
|
return $date . ' ' . $time; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the thumb picture url |
96
|
|
|
* |
97
|
|
|
* @return null|string |
98
|
|
|
*/ |
99
|
10 |
|
public function getThumb() |
100
|
|
|
{ |
101
|
10 |
|
$image = $this->node |
102
|
10 |
|
->filter('.item_imagePic .lazyload[data-imgsrc]') |
103
|
10 |
|
->first(); |
104
|
|
|
|
105
|
10 |
|
if (0 === $image->count()) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$src = $image |
110
|
10 |
|
->attr('data-imgsrc'); |
111
|
|
|
|
112
|
10 |
|
return (string)Http::createFromString($src)->withScheme('https'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return the number of picture of the ad |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
10 |
|
public function getNbImage() |
121
|
|
|
{ |
122
|
10 |
|
$node = $this->node->filter('.item_imageNumber'); |
123
|
|
|
|
124
|
|
|
return $this->getFieldValue($node, 0, function ($value) { |
125
|
10 |
|
return (int)trim($value); |
126
|
10 |
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
10 |
View Code Duplication |
public function getPlacement() |
|
|
|
|
133
|
|
|
{ |
134
|
10 |
|
$node = $this->node->filter('*[itemprop=availableAtOrFrom]'); |
135
|
|
|
|
136
|
|
|
return $this->getFieldValue($node, '', function ($value) { |
137
|
10 |
|
return preg_replace('/\s+/', ' ', trim($value)); |
138
|
10 |
|
}); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
10 |
View Code Duplication |
public function getType() |
|
|
|
|
145
|
|
|
{ |
146
|
10 |
|
$node = $this->node->filter('*[itemprop=category]'); |
147
|
|
|
|
148
|
10 |
|
return $this->getFieldValue($node, false, function ($value) { |
149
|
10 |
|
if ('pro' === preg_replace('/[\s()]+/', '', $value)) { |
150
|
8 |
|
return 'pro'; |
151
|
|
|
} |
152
|
|
|
|
153
|
10 |
|
return 'part'; |
154
|
10 |
|
}); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return object |
159
|
|
|
*/ |
160
|
10 |
|
public function getAll() |
161
|
|
|
{ |
162
|
|
|
return (object)[ |
163
|
10 |
|
'id' => $this->getId(), |
164
|
10 |
|
'title' => $this->getTitle(), |
165
|
10 |
|
'price' => $this->getPrice(), |
166
|
10 |
|
'url' => $this->getUrl(), |
167
|
10 |
|
'created_at' => $this->getCreatedAt(), |
168
|
10 |
|
'thumb' => $this->getThumb(), |
169
|
10 |
|
'nb_image' => $this->getNbImage(), |
170
|
10 |
|
'placement' => $this->getPlacement(), |
171
|
10 |
|
'type' => $this->getType(), |
172
|
10 |
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return the field's value |
177
|
|
|
* |
178
|
|
|
* @param $node |
179
|
|
|
* @param $defaultValue |
180
|
|
|
* @param $callback |
181
|
|
|
* @param string $funcName |
182
|
|
|
* @param string $funcParam |
183
|
|
|
* |
184
|
|
|
* @return mixed |
185
|
|
|
*/ |
186
|
10 |
|
private function getFieldValue( |
187
|
|
|
Crawler $node, |
188
|
|
|
$defaultValue, |
189
|
|
|
$callback, |
190
|
|
|
$funcName = 'text', |
191
|
|
|
$funcParam = '' |
192
|
|
|
) { |
193
|
10 |
|
if ($node->count()) { |
194
|
10 |
|
return $callback($node->$funcName($funcParam)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $defaultValue; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
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: