-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from matmper/develop
Update Main: New rules (cep and not_html)
- Loading branch information
Showing
14 changed files
with
312 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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); | ||
} | ||
} |
This file contains 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
This file contains 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
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'; | ||
} | ||
} |
This file contains 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
This file contains 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
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'; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
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], | ||
]; | ||
} | ||
} |
This file contains 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
This file contains 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
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; | ||
} | ||
} |
Oops, something went wrong.