1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lbc; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Lbc\Crawler\AdCrawler; |
7
|
|
|
use Lbc\Crawler\SearchResultCrawler; |
8
|
|
|
use Lbc\Parser\AdUrlParser; |
9
|
|
|
use Lbc\Parser\SearchResultUrlParser; |
10
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class GetFrom |
14
|
|
|
* @package Lbc |
15
|
|
|
*/ |
16
|
|
|
class GetFrom |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* GetFrom constructor. |
25
|
|
|
* @param Client|null $client |
26
|
|
|
*/ |
27
|
12 |
|
public function __construct(Client $client = null) |
28
|
|
|
{ |
29
|
12 |
|
$this->client = $client ?: new Client(); |
30
|
12 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the http client |
34
|
|
|
* (useful to mock the response for unit testing) |
35
|
|
|
* |
36
|
|
|
* @return Client |
37
|
|
|
*/ |
38
|
2 |
|
public function getHttpClient() |
39
|
|
|
{ |
40
|
2 |
|
return $this->client; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* retrieve the search result data from the given url |
45
|
|
|
* |
46
|
|
|
* @param $url |
47
|
|
|
* @param bool $detailedAd |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
4 |
|
public function search($url, $detailedAd = false) |
52
|
|
|
{ |
53
|
4 |
|
$searchData = new SearchResultCrawler( |
54
|
4 |
|
new Crawler((string) $this->client->get($url)->getBody()), |
55
|
|
|
$url |
56
|
4 |
|
); |
57
|
|
|
|
58
|
4 |
|
$url = new SearchResultUrlParser($url, $searchData->getNbPages()); |
59
|
|
|
|
60
|
4 |
|
$ads = ($detailedAd) ? $searchData->getAds() : $searchData->getAdsId(); |
61
|
|
|
|
62
|
|
|
$sumarize = [ |
63
|
4 |
|
'total_ads' => $searchData->getNbAds(), |
64
|
4 |
|
'total_page' => $searchData->getNbPages(), |
65
|
4 |
|
'ads_per_page' => $searchData->getNbAdsPerPage(), |
66
|
4 |
|
'category' => $searchData->getUrlParser()->getCategory(), |
67
|
4 |
|
'location' => $searchData->getUrlParser()->getLocation(), |
|
|
|
|
68
|
4 |
|
'search_area' => $searchData->getUrlParser()->getSearchArea(), |
|
|
|
|
69
|
4 |
|
'sort_by' => $searchData->getUrlParser()->getSortType(), |
|
|
|
|
70
|
4 |
|
'type' => $searchData->getUrlParser()->getType(), |
|
|
|
|
71
|
4 |
|
'ads' => $ads, |
72
|
4 |
|
]; |
73
|
|
|
|
74
|
4 |
|
return array_merge($url->getNav(), $sumarize); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve the ad's data from an ad's ID and its category |
79
|
|
|
* |
80
|
|
|
* @param $id |
81
|
|
|
* @param $category |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
2 |
|
private function adById($id, $category) |
86
|
|
|
{ |
87
|
2 |
|
return $this->ad("https://www.leboncoin.fr/{$category}/{$id}.htm"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve the ad's data from the given url |
92
|
|
|
* |
93
|
|
|
* @param $url |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
4 |
|
private function adByUrl($url) |
97
|
|
|
{ |
98
|
4 |
|
$content = $this->client->get($url)->getBody()->getContents(); |
99
|
|
|
|
100
|
4 |
|
$adData = new AdCrawler(new Crawler(utf8_encode($content)), $url); |
101
|
|
|
|
102
|
4 |
|
return $adData->getAll(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Dynamique method to retrive the data by url OR id and category |
107
|
|
|
* |
108
|
|
|
* @return bool|mixed |
109
|
|
|
*/ |
110
|
6 |
|
public function ad() |
111
|
|
|
{ |
112
|
6 |
|
if (func_num_args() === 1) { |
113
|
4 |
|
return call_user_func_array([$this, 'adByUrl'], func_get_args()); |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
if (func_num_args() === 2) { |
117
|
2 |
|
return call_user_func_array([$this, 'adById'], func_get_args()); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
throw new \InvalidArgumentException('Bad number of argument'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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: