for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Validate;
use GuzzleHttp\Client as Http;
use Exception;
class Cep implements \Validate\Contracts\Validate
{
public static function toDatabase($cep)
return preg_replace('/[^0-9]/', '', trim($cep));
}
public static function toUser($cep)
return $cep;
public static function validate($cep)
if (preg_match('/[0-9]{2,2}([.]?)[0-9]{3,3}([- ]?)[0-9]{3}$/', $cep) == 0 ) {
return false;
try {
$client = new Http();
$res = $client->request('GET', 'https://viacep.com.br/ws/'.self::toDatabase($cep).'/json/');
if ($res->getStatusCode() !== 200) {
$json = json_decode($res->getBody());
if (isset($json->error) && $json->error = true) {
} catch (Exception $e) {
return true;
public static function isSame(string $to, string $from)
return (self::toDatabase($to)===self::toDatabase($from));