Skip to content

Commit fabd685

Browse files
committed
Added checks if array keys exist
1 parent df2c842 commit fabd685

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/Objects/ViaCepResponse.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public function __construct(int|string $cep)
2424
$response = ViaCep::fromCep($cep);
2525
$this->status = $response["statusText"];
2626
$this->cep = $cep;
27-
$this->complement = $response["complemento"];
28-
$this->ibge = $response["ibge"];
29-
$this->gia = $response["gia"];
30-
$this->siafi = $response["siafi"];
31-
$this->ddd = $response["ddd"];
32-
$this->street = $response["logradouro"];
33-
$this->area = $response["bairro"];
34-
$this->city = $response["localidade"];
35-
$this->state = $response["uf"];
27+
$this->complement = array_key_exists("complemento", $response) ? $response["complemento"] : null;
28+
$this->ibge = array_key_exists("ibge", $response) ? $response["ibge"] : null;
29+
$this->gia = array_key_exists("gia", $response) ? $response["gia"] : null;
30+
$this->siafi = array_key_exists("siafi", $response) ? $response["siafi"] : null;
31+
$this->ddd = array_key_exists("ddd", $response) ? $response["ddd"] : null;
32+
$this->street = array_key_exists("logradouro", $response) ? $response["logradouro"] : null;
33+
$this->area = array_key_exists("bairro", $response) ? $response["bairro"] : null;
34+
$this->city = array_key_exists("localidade", $response) ? $response["localidade"] : null;
35+
$this->state = array_key_exists("uf", $response) ? $response["uf"] : null;
3636
$this->country = "BR";
3737
}
3838

0 commit comments

Comments
 (0)