Skip to content

Commit

Permalink
feat(serviceProvider): add the service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fnsc committed Feb 9, 2024
1 parent 5c9c6dc commit fcff675
Show file tree
Hide file tree
Showing 10 changed files with 616 additions and 488 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
},
"autoload": {
"psr-4": {
"Fnsc\\": "src/"
"CpfCnpjValidationRules\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Fnsc\\": "tests/Unit/src/"
"CpfCnpjValidationRules\\": "tests/Unit/src/"
}
},
"config": {
Expand Down
1,022 changes: 550 additions & 472 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/AbstractValidator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Fnsc;
namespace CpfCnpjValidationRules;

abstract class AbstractValidator
{
Expand Down
9 changes: 7 additions & 2 deletions src/CNPJ/Validator.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Fnsc\CNPJ;
namespace CpfCnpjValidationRules\CNPJ;

use Fnsc\AbstractValidator;
use CpfCnpjValidationRules\AbstractValidator;
use Illuminate\Contracts\Validation\Rule;

class Validator extends AbstractValidator implements Rule
Expand Down Expand Up @@ -52,6 +52,11 @@ public function passes($attribute, $value): bool
return $this->isValid($firstDigit, $secondDigit);
}

public function message(): string
{
return 'The :attribute is invalid.';
}

private function calculateDigit(int $aux, int $loop): int
{
$result = 0;
Expand Down
9 changes: 7 additions & 2 deletions src/CPF/Validator.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Fnsc\CPF;
namespace CpfCnpjValidationRules\CPF;

use Fnsc\AbstractValidator;
use CpfCnpjValidationRules\AbstractValidator;
use Illuminate\Contracts\Validation\Rule;

class Validator extends AbstractValidator implements Rule
Expand Down Expand Up @@ -52,6 +52,11 @@ public function passes($attribute, $value): bool
return $this->isValid($firstDigit, $secondDigit);
}

public function message(): string
{
return 'The :attribute is invalid.';
}

private function calculateDigit(int $aux, int $loop): int
{
$result = 0;
Expand Down
13 changes: 9 additions & 4 deletions src/RegistrationNumber/Validator.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Fnsc\RegistrationNumber;
namespace CpfCnpjValidationRules\RegistrationNumber;

use Fnsc\AbstractValidator;
use Fnsc\CNPJ\Validator as CNPJ;
use Fnsc\CPF\Validator as CPF;
use CpfCnpjValidationRules\AbstractValidator;
use CpfCnpjValidationRules\CNPJ\Validator as CNPJ;
use CpfCnpjValidationRules\CPF\Validator as CPF;
use Illuminate\Contracts\Validation\Rule;

class Validator extends AbstractValidator implements Rule
Expand All @@ -27,4 +27,9 @@ public function passes($attribute, $value): bool

return $validator->passes($attribute, $value);
}

public function message(): string
{
return 'The :attribute is invalid.';
}
}
35 changes: 35 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace CpfCnpjValidationRules;

use CpfCnpjValidationRules\CNPJ\Validator as CNPJ;
use CpfCnpjValidationRules\CPF\Validator as CPF;
use CpfCnpjValidationRules\RegistrationNumber\Validator as RegistrationNumber;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
/**
* @var array|string[]
*/
private array $rules = [
CPF::class,
CNPJ::class,
RegistrationNumber::class,
];

public function boot(): void
{
$this->registerRules();
}

private function registerRules(): void
{
foreach ($this->rules as $rule) {
$rule = $this->app->make($rule);
$alias = $rule->getAlias();
Validator::extend($alias, $rule . '@passes', $rule->message());
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/src/CNPJ/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Fnsc\CNPJ;
namespace CpfCnpjValidationRules\CNPJ;

use PHPUnit\Framework\TestCase;

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/src/CPF/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Fnsc\CPF;
namespace CpfCnpjValidationRules\CPF;

use PHPUnit\Framework\TestCase;

Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/src/RegistrationNumber/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Fnsc\RegistrationNumber;
namespace CpfCnpjValidationRules\RegistrationNumber;

use Fnsc\CNPJ\Validator as CNPJ;
use Fnsc\CPF\Validator as CPF;
use CpfCnpjValidationRules\CNPJ\Validator as CNPJ;
use CpfCnpjValidationRules\CPF\Validator as CPF;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit fcff675

Please sign in to comment.