From 67276c6444a70b61030ab3ee52b84dc5014af0e8 Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Mon, 27 Jul 2026 20:31:30 -0300 Subject: Carry the municipality of a not-found CEP on the exception The API now sends an optional faixa block on the 404 of GET /{cep}, naming the municipality the CEP would have belonged to. That failing CEP is rarely random: it is usually someone who knows the city and guessed the number, most often the -000 that 957 municipalities never received. Knowing the lookup failed "in Areal/RJ" is the difference between a message a user can act on and a bare "CEP not found". It stays an error. Faixa hangs off CepNotFoundException, never off Address, and parsing is deliberately forgiving: a missing, empty or malformed block yields null rather than turning a clean not-found into a parse failure. Its cep_ini/cep_fim are the CEPs that exist, not the bounds the Correios publish, and the interval between them is sparse -- hence ceps, and hence the docblocks insisting the pair is not a list of valid CEPs. --- src/Faixa.php | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/Faixa.php (limited to 'src/Faixa.php') diff --git a/src/Faixa.php b/src/Faixa.php new file mode 100644 index 0000000..3f4be84 --- /dev/null +++ b/src/Faixa.php @@ -0,0 +1,137 @@ +uf === '' ? $this->localidade : $this->localidade . '/' . $this->uf; + } + + /** + * @return array{ + * localidade: string, uf: string, estado: string, regiao: string, + * ibge: string, cep_ini: string, cep_fim: string, ceps: int + * } + */ + public function toArray(): array + { + return [ + 'localidade' => $this->localidade, + 'uf' => $this->uf, + 'estado' => $this->estado, + 'regiao' => $this->regiao, + 'ibge' => $this->ibge, + 'cep_ini' => $this->cep_ini, + 'cep_fim' => $this->cep_fim, + 'ceps' => $this->ceps, + ]; + } + + private static function asString(mixed $value): string + { + return is_scalar($value) ? (string) $value : ''; + } + + private static function asInt(mixed $value): int + { + return is_numeric($value) ? (int) (float) $value : 0; + } +} -- cgit v1.3