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
|
|
|
* @var AdUrlParser |
20
|
|
|
*/ |
21
|
|
|
protected $url; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $url |
25
|
|
|
* @return SearchResultUrlParser |
26
|
|
|
*/ |
27
|
10 |
|
protected function setUrlParser($url) |
28
|
|
|
{ |
29
|
10 |
|
$this->url = new AdUrlParser($url); |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the Ad's ID |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
10 |
|
public function getId() |
38
|
|
|
{ |
39
|
10 |
|
return $this->url->getId(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the title |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
10 |
|
public function getTitle() |
49
|
|
|
{ |
50
|
10 |
|
return $this->getFieldValue($this->node->filter('h2'), ''); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return the price |
55
|
|
|
* |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
10 |
|
public function getPrice() |
59
|
|
|
{ |
60
|
10 |
|
return $this->getFieldValue( |
61
|
10 |
|
$this->node->filter('*[itemprop=price]'), |
62
|
10 |
|
0, |
63
|
10 |
|
PrixSanitizer::class . '::clean' |
|
|
|
|
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the Ad's URL |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getUrl() |
73
|
|
|
{ |
74
|
10 |
|
return (string)Http::createFromString($this->url)->withScheme('https'); |
75
|
|
|
} |
76
|
10 |
|
|
77
|
|
|
/** |
78
|
|
|
* Return the data and time the ad was created |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getCreatedAt() |
83
|
|
|
{ |
84
|
10 |
|
return $this->node |
85
|
|
|
->filter('*[itemprop=availabilityStarts]') |
86
|
10 |
|
->first() |
87
|
10 |
|
->attr('content'); |
88
|
10 |
|
} |
89
|
10 |
|
|
90
|
|
|
/** |
91
|
|
|
* Return the thumb picture url |
92
|
|
|
* |
93
|
|
|
* @return null|string |
94
|
|
|
*/ |
95
|
|
|
public function getThumb() |
96
|
|
|
{ |
97
|
10 |
|
$image = $this->node |
98
|
|
|
->filter('.item_imagePic .lazyload[data-imgsrc]') |
99
|
10 |
|
->first(); |
100
|
10 |
|
|
101
|
10 |
|
if (0 === $image->count()) { |
102
|
|
|
return null; |
103
|
10 |
|
} |
104
|
|
|
|
105
|
|
|
$src = $image |
106
|
|
|
->attr('data-imgsrc'); |
107
|
|
|
|
108
|
10 |
|
return (string)Http::createFromString($src)->withScheme('https'); |
109
|
|
|
} |
110
|
10 |
|
|
111
|
|
|
/** |
112
|
|
|
* Return the number of picture of the ad |
113
|
|
|
* |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
|
|
public function getNbImage() |
117
|
|
|
{ |
118
|
10 |
|
$node = $this->node->filter('.item_imageNumber'); |
119
|
|
|
|
120
|
10 |
|
return $this->getFieldValue($node, 0); |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
/** |
124
|
10 |
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function getPlacement() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$node = $this->node->filter('*[itemprop=availableAtOrFrom]'); |
129
|
|
|
|
130
|
10 |
|
return $this->getFieldValue($node, '', function ($value) { |
131
|
|
|
return preg_replace('/\s+/', ' ', trim($value)); |
132
|
10 |
|
}); |
133
|
|
|
} |
134
|
|
|
|
135
|
10 |
|
/** |
136
|
10 |
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function getType() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$node = $this->node->filter('*[itemprop=category]'); |
141
|
|
|
|
142
|
10 |
|
return $this->getFieldValue($node, false, function ($value) { |
143
|
|
|
if ('pro' === preg_replace('/[\s()]+/', '', $value)) { |
144
|
10 |
|
return 'pro'; |
145
|
|
|
} |
146
|
10 |
|
|
147
|
10 |
|
return 'part'; |
148
|
8 |
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
10 |
|
/** |
152
|
10 |
|
* @return object |
153
|
|
|
*/ |
154
|
|
|
public function getAll() |
155
|
|
|
{ |
156
|
|
|
return (object)[ |
157
|
|
|
'id' => $this->getId(), |
158
|
10 |
|
'titre' => $this->getTitle(), |
159
|
|
|
'prix' => $this->getPrice(), |
160
|
|
|
'url' => $this->getUrl(), |
161
|
10 |
|
'created_at' => $this->getCreatedAt(), |
162
|
10 |
|
'images_thumbs' => $this->getThumb(), |
163
|
10 |
|
'nb_image' => $this->getNbImage(), |
164
|
10 |
|
'placement' => $this->getPlacement(), |
165
|
10 |
|
'type' => $this->getType(), |
166
|
10 |
|
]; |
167
|
10 |
|
} |
168
|
|
|
} |
169
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: