MyHomeCollection/app/Parser/Safti.php
2021-01-28 17:13:25 +01:00

68 lines
2.6 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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();
$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;
}
});
$parsed_home->pictures = $crawler->filter('[data-testid="real-estate-mosaic-photo"]')->filter('img')->each(static function($img) {
return $img->attr('src');
});
return $parsed_home;
}
}