MyHomeCollection/app/Parser/Safti.php

72 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Parser;
use App\ParsedHome;
use App\Parser;
use NumberFormatter;
use Symfony\Component\DomCrawler\Crawler;
class Safti extends Parser
{
/**
* @inheritDoc
*/
public function parse(): ParsedHome
{
$request = $this->client->get($this->url);
$body = $request->getBody()->getContents();
$crawler = new Crawler($body);
$parsed_home = new ParsedHome();
$property_single = $crawler->filter('[data-testid="real-estate"]');
$number_formatter = new NumberFormatter('en', NumberFormatter::DECIMAL);
$currency_formatter = new NumberFormatter('en', NumberFormatter::CURRENCY);
$currency = 'EUR';
$parsed_home->title = $property_single->filter('h1')->text();
2021-02-03 16:43:17 +01:00
$parsed_home->price = $currency_formatter
->parseCurrency($property_single->filter('.property__price')->text(), $currency);
$parsed_home->city = $property_single->children()->children('div')->eq(1)->filter('p.h4')->text();
$parsed_home->description = $crawler->filter('[data-testid="real-estate-annonce-single-description"]')->text();
$property__additionals = $crawler->filter('.property__additionals');
$energies = $property__additionals->filter('.energetic-indicator');
if ($energies->count() > 0) {
$parsed_home->energy = substr($energies->eq(0)->text(), 0, 1);
$parsed_home->ges = substr($energies->eq(1)->text(), 0, 1);
}
$crawler
->filter('.property__informations .mobile-extends > div .property__informations__element')
->each(static function (Crawler $property____information, $i) use (&$parsed_home, $number_formatter) {
$name = trim($property____information->filter('i')->text());
$value = trim($property____information->filter('b')->text());
switch ($name) {
case 'Pièces :':
$parsed_home->rooms = (int)$value;
break;
case 'Surface habitable :':
$parsed_home->surface = $number_formatter->parse($value);
break;
case 'Terrain :':
$parsed_home->garden_surface = $number_formatter->parse($value);
break;
default:
// break;
}
});
2021-02-03 16:43:17 +01:00
$parsed_home->pictures = $crawler
->filter('[data-testid="real-estate-mosaic-photo"]')
->filter('img')
->each(static function ($img) {
return $img->attr('src');
});
return $parsed_home;
}
}