rsporteman/validator

提供一组常用的验证器

dev-main 2022-04-27 21:36 UTC

This package is auto-updated.

Last update: 2024-09-28 03:07:27 UTC


README

License: MIT codecov

rsporteman/validator

简介

用于验证cpf和cnpj的代码,类似于Laminas验证器。

使用Composer安装

$  composer require rsporteman/validator

示例

<?php

use rsporteman\Validator\Cpf;
use rsporteman\Validator\Cnpj;

$post['cpf'] = '313.734.531/68'; // it is valid
$validatorCpf = new Cpf();
$isValidCpf = $validatorCpf->isValid($post['cpf']); //output: true


$post['cnpj'] = '1364679'; //it is invalid
$validatorCnpj = new Cnpj();
$isValidCnpj = $validatorCnpj->isValid($post['cnpj']); //output: false

//Get Error messages

//return array empty because cpf is valid
$errorCnpjMsg = $validatorCpf->getMessages(); //output: []

//return array with errors messages because cnpj is invalid
$errorCpfMsg = $validatorCnpj->getMessages();
var_dump($errorCpfMsg);// output: ['cnpjLength' => string 'The input contains an invalid amount of characters']

环境与测试

#up docker container
docker-compose up 

#enter on container
docker exec -it rsporteman-validator bash

#install dependencies
composer install

#run tests
vendor/bin/phpunit 

#generate coverage, files will be generate inside .phpunit.cache
composer test:coverage
composer test:coverage-html