Last active
January 23, 2019 12:54
-
-
Save adlerdias/8f494dee91837b50c6e0fc8290787d70 to your computer and use it in GitHub Desktop.
mascara para cpf/cnpj/telefone
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\Traits; | |
| trait Mask | |
| { | |
| /** | |
| * Clear the given string | |
| * | |
| * @param string $str | |
| * | |
| * @return string | |
| */ | |
| private function clear(?string $str) : ?string | |
| { | |
| return str_replace([ | |
| '.', | |
| '/', | |
| '\\', | |
| '-', | |
| '(', | |
| ')', | |
| ' ' | |
| ], "", $str); | |
| } | |
| /** | |
| * Apply the correct mask to the given string | |
| * | |
| * @param string $cpfCnpj | |
| * | |
| * @return string | |
| */ | |
| public function maskCpfCnpj(?string $cpfCnpj) : ?string | |
| { | |
| $cpfCnpjCleared = $this->clear($cpfCnpj); | |
| if (!in_array(strlen($cpfCnpjCleared), [11, 14])) { | |
| return $cpfCnpjCleared; | |
| } | |
| return (strlen($cpfCnpjCleared) == 14) ? | |
| $this->maskCnpj($cpfCnpjCleared) : | |
| $this->maskCpf($cpfCnpjCleared); | |
| } | |
| /** | |
| * Apply the cpf mask to the given string | |
| * | |
| * @param string $cpf | |
| * | |
| * @return string | |
| */ | |
| private function maskCpf(?string $cpf) : ?string | |
| { | |
| return $this->format('%s%s%s.%s%s%s.%s%s%s-%s%s', $cpf); | |
| } | |
| /** | |
| * Apply the cnpj mask to the given string | |
| * | |
| * @param string $cnpj | |
| * | |
| * @return string | |
| */ | |
| private function maskCnpj(?string $cnpj) : ?string | |
| { | |
| return $this->format('%s%s.%s%s%s.%s%s%s/%s%s%s%s-%s%s', $cnpj); | |
| } | |
| /** | |
| * Apply the fone mask according to the given string | |
| * | |
| * @param string $fone | |
| * | |
| * @return string | |
| */ | |
| public function maskFoneCelular(?string $fone) : ?string | |
| { | |
| $foneCleared = $this->clear(ltrim($fone, '0')); | |
| if (!in_array(strlen($foneCleared), [11, 10])) { | |
| return $fone; | |
| } | |
| return (strlen($foneCleared)==11) ? | |
| $this->maskCelular($foneCleared) : | |
| $this->maskTelefone($foneCleared); | |
| } | |
| public function maskPhone(?string $fone) : ?string | |
| { | |
| $foneCleared = $this->clear(ltrim($fone, '0')); | |
| if (!in_array(strlen($foneCleared), [11, 10])) { | |
| return $fone; | |
| } | |
| return (strlen($foneCleared) == 11) ? | |
| $this->maskCelular($foneCleared) : | |
| $this->maskTelefone($foneCleared); | |
| } | |
| /** | |
| * Apply the telefone mask to the given string | |
| * | |
| * @param string $telefone | |
| * | |
| * @return string | |
| */ | |
| private function maskTelefone(?string $telefone) : ?string | |
| { | |
| return $this->format('(%s%s) %s%s%s%s-%s%s%s%s', $telefone); | |
| } | |
| /** | |
| * Apply the celular mask to the given string | |
| * | |
| * @param string $celular | |
| * | |
| * @return string | |
| */ | |
| private function maskCelular(?string $celular) : ?string | |
| { | |
| return $this->format('(%s%s) %s%s%s%s%s-%s%s%s%s', $celular); | |
| } | |
| /** | |
| * Apply the cep mask to the given string | |
| * | |
| * @param string $cep | |
| * | |
| * @return string | |
| */ | |
| private function maskCep(?string $cep) : ?string | |
| { | |
| $cepCleared = $this->clear(trim($cep)); | |
| if (!in_array(strlen($cepCleared), [8])) { | |
| return $cep; | |
| } | |
| return $this->format('%s%s%s%s%s-%s%s%s', $cepCleared); | |
| } | |
| /** | |
| * Apply the given mask to the given string | |
| * | |
| * @param string $mask | |
| * @param string $string | |
| * | |
| * @return string | |
| */ | |
| private function format(?string $mask, string $string) : ?string | |
| { | |
| return vsprintf($mask, str_split($string)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment