MyHomeCollection/app/Parser/LannionImmo.php

69 lines
2.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Parser;
use App\ParsedHome;
use App\Parser;
use Symfony\Component\DomCrawler\Crawler;
class LannionImmo extends Parser
{
/**
* @inheritDoc
*/
public function parse(): ParsedHome
{
$request = $this->client->get($this->url);
$body = $request->getBody()->getContents();
$crawler = new Crawler($body);
$parsedHome = new ParsedHome();
$parsedHome->title = $crawler->filter('h1')->text();
$parsedHome->price = (int)$crawler->filter('[itemprop=price]')->attr('content');
$parsedHome->description = $crawler->filter('[itemprop=description]')->text();
$crawler
->filter('.tech_detail tr')
->each(static function (Crawler $detail, $i) use (&$parsedHome) {
foreach (range(1, 2) as $loop) {
$l = sprintf('.l%u', $loop);
$r = sprintf('.r%u', $loop);
$detail_name = $detail->filter($l);
if ($detail_name instanceof Crawler && $detail_name->count() > 0) {
switch ($detail_name->text()) {
case 'Surface':
$parsedHome->surface = (int)$detail->filter($r)->text();
break;
case 'Ville':
$parsedHome->city = $detail->filter($r)->text();
break;
case 'Pièces':
$parsedHome->rooms = (int)$detail->filter($r)->text();
break;
case 'Superficie du terrain':
$parsedHome->garden_surface = (int)$detail->filter($r)->text();
break;
default:
break;
}
}
}
});
$parsedHome->energy = substr($crawler->filter('.dpe-letter-active')->first()->text(), 0, 1);
$parsedHome->ges = substr($crawler->filter('.dpe-letter-active')->eq(1)->text(), 0, 1);
$parsedHome->pictures = $crawler
->filter('.rsImg')
->each(static function (Crawler $node, $i) {
if (strtolower($node->nodeName()) === 'a') {
return $node->attr('href');
}
if ($node->children('img')) {
return $node->children('img')->attr('src');
}
return null;
});
return $parsedHome;
}
}