You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

  1. <?php
  2. namespace App\Parser;
  3. use Absmoca\Leboncoin;
  4. use App\ParsedHome;
  5. use App\Parser;
  6. class LBC extends Parser
  7. {
  8. public function __construct($url)
  9. {
  10. parent::__construct($url);
  11. }
  12. /**
  13. * @return \App\ParsedHome
  14. */
  15. public function parse(): ParsedHome
  16. {
  17. $idAnnonce = (int) $this->getIdAnnonceFromUrl($this->url);
  18. $lbc = new Leboncoin();
  19. $annonce = $lbc->getAnnonce($idAnnonce);
  20. $parsedHome = new ParsedHome();
  21. $parsedHome->title = $annonce->getName();
  22. $parsedHome->price = $annonce->getPrice();
  23. $attributes = $annonce->getAttributes();
  24. $parsedHome->surface = $attributes[1]->value;
  25. $parsedHome->rooms = $attributes[2]->value;
  26. $parsedHome->energy = strtoupper($attributes[3]->value);
  27. $parsedHome->ges = strtoupper($attributes[4]->value);
  28. $parsedHome->description = $annonce->getDescription();
  29. $parsedHome->pictures = $annonce->getImages();
  30. $location = $annonce->getLocation();
  31. $parsedHome->city = $location['city'];
  32. $parsedHome->map = ['lat' => $location['lat'], 'lng' => $location['lng']];
  33. return $parsedHome;
  34. }
  35. /**
  36. * @param $url
  37. *
  38. * @return string
  39. */
  40. private function getIdAnnonceFromUrl($url)
  41. {
  42. // ex : https://www.leboncoin.fr/ventes_immobilieres/1813996383.htm/
  43. return collect(explode('.', collect(array_filter(explode('/', $url)))->last()))->first();
  44. }
  45. }