Skip to content

Commit

Permalink
Merge pull request #1 from matmper/develop
Browse files Browse the repository at this point in the history
Update Main: New rules (cep and not_html)
  • Loading branch information
matmper authored Sep 28, 2023
2 parents 6336568 + 0e562be commit e053f49
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 28 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ $ php artisan vendor:publish --provider="Matmper\Providers\ValidationProvider"

# Usage

| Name | Validation | Description |
|---|---|---|
| DOCUMENT| document | validate CPF or CNPJ value, ignore non numeric char |
| - | document:cpf | validate CPF value, ignore non numeric char |
| - | document:cpf,mask | validate CPF value and format (000.000.000-00) |
| Validation | Description |
|---|---|
| cep | validates CEP value, ignore non numeric char |
| cep:mask | validates CEP value and format (00000-000) |
| document | validates CPF or CNPJ value, ignore non numeric char |
| document:cpf | validates CPF value, ignore non numeric char |
| document:cpf,mask | validates CPF value and format (000.000.000-00) |
| not_html | validates that the string does not contain html |

**Example**
```php
Expand All @@ -45,7 +48,7 @@ $ php artisan vendor:publish --provider="Matmper\Providers\ValidationProvider"
// 00000000000000 -> true
// 00.000.000/0000-00 -> true
return ['document_number' => 'document'];
return ['document_number' => 'document:cpf|cnpj'];
return ['document_number' => 'document:cpf.cnpj'];

// 00000000000 -> true
// 000.000.000-00 -> true
Expand All @@ -58,6 +61,10 @@ return ['cpf' => 'document:cpf'];
// 00000000000000 -> false
// 00.000.000/0000-00 -> true
return ['cnpj' => 'document:cnpj,mask'];

// hello world -> true
// <p>hello world</p> -> false
return ['content' => 'string|not_html'];
```

| laravel-brazil-validation version | Laravel versions |
Expand Down
13 changes: 6 additions & 7 deletions src/Exceptions/InvalidDocumentTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

class InvalidDocumentTypeException extends Exception
{
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
string $message = null,
int $code = 0,
int $code = Response::HTTP_NOT_FOUND,
Throwable $previous = null,
) {
$message = "Invalid document type for validation: {$message}";
$code = $code ? $code : Response::HTTP_NOT_FOUND;

parent::__construct($message, $code, $previous);
}
Expand Down
13 changes: 6 additions & 7 deletions src/Exceptions/InvalidValidationParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

class InvalidValidationParameterException extends Exception
{
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
string $message = null,
int $code = 0,
int $code = Response::HTTP_NOT_ACCEPTABLE,
Throwable $previous = null,
) {
$message = "Validation parameter is invalid: {$message}";
$code = $code ? $code : Response::HTTP_NOT_ACCEPTABLE;

parent::__construct($message, $code, $previous);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Exceptions/ValueIsNotStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Matmper\Exceptions;

use Exception;
use Illuminate\Http\Response;
use Throwable;

class ValueIsNotStringException extends Exception
{
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
string $message = null,
int $code = Response::HTTP_BAD_REQUEST,
Throwable $previous = null,
) {
$message = "Parameter value is not a valid string: {$message}";

parent::__construct($message, $code, $previous);
}
}
2 changes: 2 additions & 0 deletions src/Providers/ValidationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class ValidationProvider extends ServiceProvider
public function boot(): void
{
$services = [
'cep' => \Matmper\Rules\CepRule::class,
'document' => \Matmper\Rules\DocumentNumberRule::class,
'not_html' => \Matmper\Rules\NotHtmlRule::class,
];

foreach ($services as $name => $service) {
Expand Down
68 changes: 68 additions & 0 deletions src/Rules/CepRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Matmper\Rules;

use Illuminate\Contracts\Validation\Rule;

class CepRule implements Rule
{
/**
* Set true if check mask and value
*
* @var bool
*/
private $checkMask;

/**
* Set rule params
*
* @return self
*/
public function params(array $params): self
{
$this->setMask($params[0] ?? 'value');

return $this;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
if ($this->checkMask && !preg_match('/\d{5}\-\d{3}/', $value)) {
return false;
}

return strlen(preg_replace("/[^\d]/", "", $value)) === 8;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return ':attribute não é um CEP válido';
}

/**
* Validate type and set $this->setMask
*
* @param string $type
* @return void
*/
private function setMask(string $type): void
{
if (!in_array($type, ['value', 'mask'])) {
throw new \Matmper\Exceptions\InvalidValidationParameterException($type);
}

$this->checkMask = $type === 'mask';
}
}
6 changes: 3 additions & 3 deletions src/Rules/DocumentNumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DocumentNumberRule implements Rule
*/
public function params(array $params): self
{
$this->setDocuments($params[0] ?? 'cpf|cnpj');
$this->setDocuments($params[0] ?? 'cpf.cnpj');
$this->setMask($params[1] ?? 'value');

return $this;
Expand Down Expand Up @@ -71,12 +71,12 @@ public function message()
/**
* Validate document list and set in $this->documents
*
* @param string $documents cpf|cnpj
* @param string $documents cpf.cnpj
* @return void
*/
private function setDocuments(string $documents): void
{
$this->documents = explode('|', $documents);
$this->documents = explode('.', $documents);

$validDocumentTypes = [DocumentType::CPF, DocumentType::CNPJ];

Expand Down
48 changes: 48 additions & 0 deletions src/Rules/NotHtmlRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Matmper\Rules;

use Illuminate\Contracts\Validation\Rule;

class NotHtmlRule implements Rule
{
/**
* Set rule params
*
* @return self
*/
public function params(array $params): self
{
return $this;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
if (!is_string($value)) {
throw new \Matmper\Exceptions\ValueIsNotStringException($attribute);
}

if (preg_match('/<\s?[^\>]*\/?\s?>/i', $value)) {
return false;
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return ':attribute não deve conter código HTML';
}
}
2 changes: 1 addition & 1 deletion src/Services/DocumentCnpjService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function passes(mixed $value, bool $checkMask): bool
*/
private function validate(string $cnpj): bool
{
if (strlen($cnpj) <> 14 || (int) $cnpj <= 0) {
if (strlen($cnpj) <> 14 || substr_count($cnpj, substr($cnpj, 0, 1)) === strlen($cnpj)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Services/DocumentCpfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function passes(mixed $value, bool $checkMask): bool
*/
private function validate(string $cpf): bool
{
if (strlen($cpf) <> 11 || (int) $cpf <= 0) {
if (strlen($cpf) <> 11 || substr_count($cpf, substr($cpf, 0, 1)) === strlen($cpf)) {
return false;
}

Expand Down
51 changes: 51 additions & 0 deletions tests/Unit/CepRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace Tests;

use Tests\Helpers\FakeCepHelper;

class CepRuleTest extends TestCase
{
/**
* @test
*/
public function test_invalid_validation_parameter_exception(): void
{
$this->expectException(\Matmper\Exceptions\InvalidValidationParameterException::class);

$rule = app(\Matmper\Rules\CepRule::class);
$rule->params(['wrong_value']);
}

/**
* @test
* @dataProvider testCepDataProvider
*/
public function test_document_number(string $type, string $cep, bool $assert): void
{
$rule = app(\Matmper\Rules\CepRule::class);

$rule->params([$type]);
$passes = $rule->passes(fake()->word(), $cep);

$this->assertEquals($passes, $assert);
}

/**
* Data Provider: test_document_number
*
* @return array
*/
public function testCepDataProvider(): array
{
return [
'cep_true' => ['value', FakeCepHelper::cep(), true],
'cep_true_mask' => ['value', FakeCepHelper::cep(true), true],
'cep_false' => ['value', FakeCepHelper::cep() . fake()->randomDigit(), false],

'cep_mask_true' => ['mask', FakeCepHelper::cep(true), true],
'cep_mask_false_value' => ['mask', FakeCepHelper::cep(), false],
'cep_mask_false' => ['mask', FakeCepHelper::cep(true) . fake()->randomDigit(), false],
];
}
}
6 changes: 3 additions & 3 deletions tests/Unit/DocumentNumberRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function test_document_number(string $type, string $mask, string $documen
public function testDocumentNumberDataProvider(): array
{
return [
'cpf_cnpj_true_cpf' => ['cpf|cnpj', 'value', FakeDocumentHelper::cpf(), true],
'cpf_cnpj_true_cnpj' => ['cpf|cnpj', 'value', FakeDocumentHelper::cnpj(), true],
'cpf_cnpj_false' => ['cpf|cnpj', 'value', fake()->word(), false],
'cpf_cnpj_true_cpf' => ['cpf.cnpj', 'value', FakeDocumentHelper::cpf(), true],
'cpf_cnpj_true_cnpj' => ['cpf.cnpj', 'value', FakeDocumentHelper::cnpj(), true],
'cpf_cnpj_false' => ['cpf.cnpj', 'value', fake()->word(), false],

'cpf_true' => ['cpf', 'value', FakeDocumentHelper::cpf(), true],
'cpf_true_mask' => ['cpf', 'mask', FakeDocumentHelper::cpf(true), true],
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Helpers/FakeCepHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Helpers;

class FakeCepHelper
{
/**
* Generate a fake CEP
*
* @param boolean $mask
* @return string
*/
public static function cep(bool $mask = false): string
{
$cep01 = random_int(10000, 99999);
$mask = $mask ? '-' : '';
$cep02 = random_int(100, 999);

return $cep01 . $mask . $cep02;
}
}
Loading

0 comments on commit e053f49

Please sign in to comment.