Compare commits

...
2 Commits
Author SHA1 Message Date
KKlochko 2b37a4c2e7 Refactor ValidatorByNameFactories of Company and Item models.
continuous-integration/drone/push Build is passing
2023-08-31 13:05:49 +03:00
KKlochko c415a77272 Add InformativeValidatorByNameFactory. 2023-08-31 13:02:00 +03:00
3 changed files with 73 additions and 50 deletions
@@ -2,45 +2,28 @@
namespace App\Models\Validation\Messages\Factories; namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\InformativeValidatorFactory; use App\Models\Validation\Messages\Factories\InformativeValidatorByNameFactory;
use App\Models\Validation\Messages\InformativeValidator; use App\Models\Validation\Messages\InformativeValidator;
use App\Models\Validation\Messages\OkInformativeValidator; use App\Models\Validation\Validators\Validator;
use App\Models\Validation\Messages\NextInformativeValidator;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\CompanyValidatorByName; use App\Models\Validation\Validators\CompanyValidatorByName;
class CompanyInformativeValidatorByNameFactory extends InformativeValidatorFactory class CompanyInformativeValidatorByNameFactory extends InformativeValidatorByNameFactory
{ {
protected array $messages;
protected string $name;
public function __construct(string $name) public function __construct(string $name)
{ {
$this->messages = [ $this->setMessages([
'found' => 'A company with the name is valid.', 'found' => 'A company with the name is valid.',
'not_found' => 'A company with the name does not exist!!!', 'not_found' => 'A company with the name does not exist!!!',
'invalid_name' => 'The company name is empty, please, write the name!!!', 'invalid_name' => 'The company name is empty, please, write the name!!!',
]; ]);
$this->name = $name; $this->setName($name);
} }
function create(): InformativeValidator public function getValidatorByName(InformativeValidator $okValidator): Validator
{ {
$okValidator = new OkInformativeValidator($this->messages['found']); return new CompanyValidatorByName($this->name, $okValidator);
$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,57 @@
<?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\Validator;
abstract class InformativeValidatorByNameFactory extends InformativeValidatorFactory
{
protected array $messages;
protected string $name;
public function setMessages(array $messages): void
{
$this->messages = $messages;
}
public function getMessages(): array
{
return $this->messages;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
abstract public function getValidatorByName(InformativeValidator $okValidator): Validator;
public function create(): InformativeValidator
{
$okValidator = new OkInformativeValidator($this->messages['found']);
$nameValidator = new NextInformativeValidator(
$this->messages['not_found'],
$this->getValidatorByName($okValidator),
$okValidator
);
return new NextInformativeValidator(
$this->messages['invalid_name'],
new EmptyNameValidator($this->name, $nameValidator),
$nameValidator
);
}
}
@@ -2,45 +2,28 @@
namespace App\Models\Validation\Messages\Factories; namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\InformativeValidatorFactory; use App\Models\Validation\Messages\Factories\InformativeValidatorByNameFactory;
use App\Models\Validation\Messages\InformativeValidator; use App\Models\Validation\Messages\InformativeValidator;
use App\Models\Validation\Messages\OkInformativeValidator; use App\Models\Validation\Validators\Validator;
use App\Models\Validation\Messages\NextInformativeValidator;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\ItemValidatorByName; use App\Models\Validation\Validators\ItemValidatorByName;
class ItemInformativeValidatorByNameFactory extends InformativeValidatorFactory class ItemInformativeValidatorByNameFactory extends InformativeValidatorByNameFactory
{ {
protected array $messages;
protected string $name;
public function __construct(string $name) public function __construct(string $name)
{ {
$this->messages = [ $this->setMessages([
'found' => 'A item with the name is valid.', 'found' => 'A item with the name is valid.',
'not_found' => 'A item with the name does not exist!!!', 'not_found' => 'A item with the name does not exist!!!',
'invalid_name' => 'The item name is empty, please, write the name!!!', 'invalid_name' => 'The item name is empty, please, write the name!!!',
]; ]);
$this->name = $name; $this->setName($name);
} }
function create(): InformativeValidator public function getValidatorByName(InformativeValidator $okValidator): Validator
{ {
$okValidator = new OkInformativeValidator($this->messages['found']); return new ItemValidatorByName($this->name, $okValidator);
$nameValidator = new NextInformativeValidator(
$this->messages['not_found'],
new ItemValidatorByName($this->name, $okValidator),
$okValidator
);
return new NextInformativeValidator(
$this->messages['invalid_name'],
new EmptyNameValidator($this->name, $nameValidator),
$nameValidator
);
} }
} }