1 | <?php |
||
15 | class GetFrom |
||
16 | { |
||
17 | /** |
||
18 | * @var Client |
||
19 | */ |
||
20 | protected $client; |
||
21 | |||
22 | /** |
||
23 | * GetFrom constructor. |
||
24 | * @param Client|null $client |
||
25 | */ |
||
26 | 12 | public function __construct(Client $client = null) |
|
30 | |||
31 | /** |
||
32 | * Return the http client |
||
33 | * (useful to mock the response for unit testing) |
||
34 | * |
||
35 | * @return Client |
||
36 | */ |
||
37 | 2 | public function getHttpClient() |
|
41 | |||
42 | /** |
||
43 | * retrieve the search result data from the given url |
||
44 | * |
||
45 | * @param $url |
||
46 | * @param bool $detailedAd |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | 4 | public function search($url, $detailedAd = false) |
|
51 | { |
||
52 | 4 | $searchData = new SearchResultCrawler( |
|
53 | 4 | new Crawler((string) $this->client->get($url)->getBody()), |
|
54 | 4 | $url |
|
55 | ); |
||
56 | |||
57 | 4 | $url = new SearchResultUrlParser($url, $searchData->getNbPages()); |
|
58 | |||
59 | 4 | $ads = ($detailedAd) ? $searchData->getAds() : $searchData->getAdsId(); |
|
60 | |||
61 | $sumarize = [ |
||
62 | 4 | 'total_ads' => $searchData->getNbAds(), |
|
63 | 4 | 'total_page' => $searchData->getNbPages(), |
|
64 | 4 | 'ads_per_page' => $searchData->getNbAdsPerPage(), |
|
65 | 4 | 'category' => $searchData->getUrlParser()->getCategory(), |
|
66 | 4 | 'location' => $searchData->getUrlParser()->getLocation(), |
|
67 | 4 | 'search_area' => $searchData->getUrlParser()->getSearchArea(), |
|
68 | 4 | 'sort_by' => $searchData->getUrlParser()->getSortType(), |
|
69 | 4 | 'type' => $searchData->getUrlParser()->getType(), |
|
70 | 4 | 'ads' => $ads, |
|
71 | ]; |
||
72 | |||
73 | 4 | return array_merge($url->getNav(), $sumarize); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Retrieve the ad's data from an ad's ID and its category |
||
78 | * |
||
79 | * @param $id |
||
80 | * @param $category |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 2 | private function adById($id, $category) |
|
88 | |||
89 | /** |
||
90 | * Retrieve the ad's data from the given url |
||
91 | * |
||
92 | * @param $url |
||
93 | * @return array |
||
94 | */ |
||
95 | 4 | private function adByUrl($url) |
|
103 | |||
104 | /** |
||
105 | * Dynamique method to retrive the data by url OR id and category |
||
106 | * |
||
107 | * @return bool|mixed |
||
108 | */ |
||
109 | 6 | public function ad() |
|
121 | } |
||
122 |