Created
December 5, 2016 21:47
-
-
Save omarkdev/6370cb01a96ea89fd5adfa4c06375272 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Inquiries; | |
use PHPHtmlParser\Dom; | |
class Consult{ | |
protected $defaultUrl = 'https://consultaca.com/'; | |
protected $ca; | |
protected $url; | |
protected $dom; | |
protected $info = array(); | |
public function consult($ca){ | |
$this->ca = $ca; | |
$this->url = $this->defaultUrl.$ca; | |
$this->dom = $this->generateDom(); | |
$boxResult = $this->dom->find("div#box_result"); | |
$boxResultP = $boxResult->find('p'); | |
$groupResultCa = $boxResult->find('.grupo_result_ca'); | |
$this->info['status'] = $this->infoStatus($boxResultP); | |
$this->info['validation'] = $this->infoValidation($boxResultP); | |
$this->info['number_process'] = $this->infoNumberProcess($boxResultP); | |
$this->info['description'] = $this->infoDescription($groupResultCa); | |
$this->info['photos'] = $this->infoPhotos($groupResultCa); | |
$this->info['equipment'] = $this->infoEquipment($groupResultCa); | |
$this->info['standards'] = $this->infoStandards($groupResultCa); | |
$this->info['report'] = $this->infoReport($groupResultCa); | |
$this->info['cnpj-manufacturer'] = $this->infoCnpjManufacturer($groupResultCa); | |
return $this->info; | |
} | |
protected function generateDom(){ | |
$dom = new Dom; | |
$dom->loadFromUrl($this->url); | |
return $dom; | |
} | |
protected function partialPos($elements, $text){ | |
foreach($elements as $el){ | |
if(strpos($el->innerHtml, $text) === false) | |
continue; | |
return $el; | |
} | |
return false; | |
} | |
private function infoStatus($elements){ | |
$el = $this->partialPos($elements, "Situação"); | |
return ($el !== false) ? $el->find('span')->text : "Error"; | |
} | |
private function infoValidation($elements){ | |
$el = $this->partialPos($elements, "Validade"); | |
return ($el !== false) ? $el->find('span')->text : "Error"; | |
} | |
private function infoNumberProcess($elements){ | |
$el = $this->partialPos($elements, "N° Processo"); | |
return ($el !== false) ? $el->text : "Error"; | |
} | |
private function infoDescription($elements){ | |
$el = $this->partialPos($elements, "Descrição"); | |
return ($el !== false) ? $el->find('p')->text : "Error"; | |
} | |
private function infoPhotos($elements){ | |
$el = $this->partialPos($elements, "Fotos do Equipamento"); | |
$images = array(); | |
foreach($el->find('img') as $img){ | |
array_push($images, $img->getAttribute('src')); | |
} | |
return $images; | |
} | |
private function infoEquipment($elements){ | |
$el = $this->partialPos($elements, "Equipamento"); | |
return array( | |
'name' => $el->find('span.destaque')->text, | |
'group' => $el->find('span.grupo-epi-box')->text | |
); | |
} | |
private function infoStandards($elements){ | |
$el = $this->partialPos($elements, "Normas"); | |
$standards = []; | |
foreach($el->find('p.info') as $standard){ | |
array_push($standards, $standard->text); | |
} | |
return $standards; | |
} | |
private function infoReport($elements){ | |
$el = $this->partialPos($elements, "Laudo"); | |
$reports = []; | |
foreach($el->find('p.info') as $report){ | |
if(strpos($report->innerHtml, "N° do Laudo") !== false){ | |
$reports[] = ['title' => "N° do Laudo", 'value' => $report->find('strong.destaque')->text]; | |
continue; | |
} | |
$reports[] = ['title' => $report->find('strong')->text, 'value' => $report->text]; | |
} | |
return $reports; | |
} | |
private function infoCnpjManufacturer($elements){ | |
$el = $this->partialPos($elements, "Fabricante"); | |
foreach($el->find('p.info') as $manufacturer){ | |
$textStrong = $manufacturer->find('strong'); | |
if(strpos($textStrong, "CNPJ") === false) | |
continue; | |
return $manufacturer->text; | |
} | |
return "Error"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment