PHP code example of websix / br-validations
1. Go to this page and download the library: Download websix/br-validations library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
websix / br-validations example snippets
/**
* Somewere calling an autoloader and opening php
*/
use Websix\BrValidations\Validators\CnpjValidator,
Websix\BrValidations\Exceptions;
// ...
$validator = new CnpjValidator();
try {
$isValid = $validator->validate($aCnpj);
} catch (\InvalidArgumentException $e) {
// Has exceptions that can tell you what failed
switch (true) {
case $e instanceof Exceptions\InvalidCnpjException:
// Is really a invalid CNPJ
// ...
case $e instanceof Exceptions\EmptyArgumentException:
// If the CNPJ is an empty string or something like that
// ...
case $e instanceof Exceptions\InvalidLengthException:
// If the CNPJ do not have his correct length
// ...
case $e instanceof Exceptions\NotOnlyDigitsException:
// If the passed CNPJ has something other than digits in the string
// ...
case $e instanceof Exceptions\NotStringException:
// if the passed CNPJ not is a string
// ...
}
}
use
use Websix\BrValidations\Validations\CnpjValidator;
$validator = new CnpjValidator();