Add CompanyInformativeValidatorByNameFactory.

This commit is contained in:
2023-08-29 17:00:28 +03:00
parent 36a803131e
commit a37992c2e9
3 changed files with 117 additions and 0 deletions
@@ -0,0 +1,46 @@
<?php
namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\InformativeValidatorFactory;
use App\Models\Validation\Messages\InformativeValidator;
use App\Models\Validation\Messages\OkInformativeValidator;
use App\Models\Validation\Messages\NextInformativeValidator;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\CompanyValidatorByName;
class CompanyInformativeValidatorByNameFactory extends InformativeValidatorFactory
{
protected array $messages;
protected string $name;
public function __construct(string $name)
{
$this->messages = [
'found' => 'A company with the name is valid.',
'not_found' => 'A company with the name does not exist!!!',
'invalid_name' => 'The company name is empty, please, write the name!!!',
];
$this->name = $name;
}
function create(): InformativeValidator
{
$okValidator = new OkInformativeValidator($this->messages['found']);
$nameValidator = new NextInformativeValidator(
$this->messages['not_found'],
new CompanyValidatorByName($this->name, $okValidator),
$okValidator
);
return new NextInformativeValidator(
$this->messages['invalid_name'],
new EmptyNameValidator($this->name, $nameValidator),
$nameValidator
);
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\Validator;
use App\Models\Company;
class CompanyValidatorByName extends Validator {
private string $name;
protected Validator $nextValidator;
public function __construct(string $name, Validator $nextValidator)
{
$this->name = $name;
$this->nextValidator = $nextValidator;
}
public function isCurrentValid(): bool
{
$count = Company::where('name', $this->name)->count();
return $count != 0;
}
}