From e9cd0963594d6fa5ea5c052094ac3c4239afcdd6 Mon Sep 17 00:00:00 2001 From: chapeupreto Date: Sat, 12 Jan 2019 16:24:47 -0200 Subject: [PATCH] valida placa para aceitar padrao mercosul e outras melhorias --- README.md | 1 - src/Sinesp.php | 18 +++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c575384..86e5983 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,6 @@ try { $veiculo->buscar('GWW-6471'); if ($veiculo->existe()) { print_r($veiculo->dados()); - echo 'O ano do veiculo eh ' , $veiculo->anoModelo, PHP_EOL; } } catch (\Exception $e) { echo $e->getMessage(); diff --git a/src/Sinesp.php b/src/Sinesp.php index b7b9f13..4dd745c 100644 --- a/src/Sinesp.php +++ b/src/Sinesp.php @@ -6,7 +6,7 @@ class Sinesp { private $secret = '#8.1.0#g8LzUadkEHs7mbRqbX5l'; private $url = 'https://cidadao.sinesp.gov.br/sinesp-cidadao/mobile/consultar-placa/v4'; - private $proxy = null; + private $proxy = ''; private $placa = ''; private $response = ''; @@ -16,7 +16,7 @@ class Sinesp * Time (in seconds) to wait for a response * @var int */ - private $timeout = null; + private $timeout = 0; public function buscar($placa, array $proxy = []) { @@ -89,10 +89,8 @@ private function obterResposta() curl_setopt($ch, CURLOPT_PROXY, $this->proxy); } - if ($this->timeout) { - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); - curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); - } + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); @@ -129,13 +127,11 @@ private function verificarRequisitos() private function setUp($placa) { - $placa = $this->ajustar($placa); - if (!$this->validar($placa)) { throw new \Exception('Placa do veiculo nao especificada ou em formato invalido!'); } - $this->placa = $placa; + $this->placa = $this->ajustar($placa); } private function token() @@ -174,12 +170,12 @@ private function xml() private function validar($placa) { - return preg_match('/^[a-zA-Z]{3}-?\d{4}$/i', $placa); + return preg_match('/^[a-z]{3}-?\d[a-z0-9]{2}\d$/i', trim($placa)); } private function ajustar($placa) { - return str_replace(['-', ' '], '', $placa); + return str_replace('-', '', trim($placa)); } private function latitude()