Add ValidationByName and CompanyValidationByName.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
135f4c0c93
commit
f437d777a5
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
use App\Models\Validation\ModelValidationByName;
|
||||
use App\Models\Validation\Messages\CompanyMessagesFactory;
|
||||
|
||||
class CompanyValidationByName extends ModelValidationByName
|
||||
{
|
||||
public function __construct(string $name)
|
||||
{
|
||||
parent::__construct(
|
||||
$name,
|
||||
'App\Models\Company',
|
||||
(new CompanyMessagesFactory())->create(),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\ValidationStatus;
|
||||
|
||||
class BaseMessages
|
||||
{
|
||||
protected array $messages;
|
||||
|
||||
public function __construct($messages)
|
||||
{
|
||||
$this->messages = $messages;
|
||||
}
|
||||
|
||||
public function getMessage($status): string
|
||||
{
|
||||
return match($status)
|
||||
{
|
||||
ValidationStatus::FOUND => $this->messages['found'],
|
||||
ValidationStatus::NOT_FOUND => $this->messages['not_found'],
|
||||
ValidationStatus::INVALID_NAME => $this->messages['invalid_name'],
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\Messages\BaseMessages;
|
||||
|
||||
class CompanyMessagesFactory
|
||||
{
|
||||
protected array $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!!!',
|
||||
];
|
||||
|
||||
public function create()
|
||||
{
|
||||
return new BaseMessages($this->messages);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
use App\Models\Validation\ValidationByNameInterface;
|
||||
use App\Models\Validation\ValidationStatus;
|
||||
use App\Models\Validation\Messages\BaseMessages;
|
||||
|
||||
class ModelValidationByName
|
||||
{
|
||||
protected static BaseMessages $messages;
|
||||
protected static string $className;
|
||||
protected string $name;
|
||||
|
||||
public function __construct(string $name, string $className, BaseMessages $messages)
|
||||
{
|
||||
if (!in_array('App\Models\Validation\ValidationByNameInterface', class_implements($className)))
|
||||
throw new \RuntimeException("$className does not implement the ValidationByNameInterface interface.");
|
||||
|
||||
$this->name = $name;
|
||||
static::$className = $className;
|
||||
static::$messages = $messages;
|
||||
}
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
if(!call_user_func([static::$className, 'isNameValid'], $this->name))
|
||||
return ValidationStatus::INVALID_NAME;
|
||||
|
||||
if(!call_user_func([static::$className, 'isExistByName'], $this->name))
|
||||
return ValidationStatus::NOT_FOUND;
|
||||
|
||||
return ValidationStatus::FOUND;
|
||||
}
|
||||
|
||||
public function getMessageMap()
|
||||
{
|
||||
$status = $this->getStatus();
|
||||
|
||||
return [
|
||||
$status->status() => static::$messages->getMessage($status)
|
||||
];
|
||||
}
|
||||
|
||||
public function isValid()
|
||||
{
|
||||
return $this->getStatus()->isOk();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
interface ValidationByNameInterface {
|
||||
public static function isNameValid(string $name);
|
||||
|
||||
public static function isExistByName(string $name);
|
||||
}
|
||||
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
enum ValidationStatus
|
||||
{
|
||||
case FOUND;
|
||||
case NOT_FOUND;
|
||||
case INVALID_NAME;
|
||||
|
||||
public function value(): string
|
||||
{
|
||||
return match($this)
|
||||
{
|
||||
ValidationStatus::FOUND => 'found',
|
||||
ValidationStatus::NOT_FOUND => 'not_found',
|
||||
ValidationStatus::INVALID_NAME => 'invalid_name',
|
||||
};
|
||||
}
|
||||
|
||||
public function status(): string
|
||||
{
|
||||
return match($this)
|
||||
{
|
||||
ValidationStatus::FOUND => 'ok',
|
||||
ValidationStatus::NOT_FOUND => 'error',
|
||||
ValidationStatus::INVALID_NAME => 'error',
|
||||
};
|
||||
}
|
||||
|
||||
public function isError(): bool
|
||||
{
|
||||
return $this->status() == 'error';
|
||||
}
|
||||
|
||||
public function isOk(): bool
|
||||
{
|
||||
return $this->status() == 'ok';
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Validation\CompanyValidationByName;
|
||||
|
||||
class CompanyValidationByNameTest extends TestCase
|
||||
{
|
||||
public function testCompanyWithEmptyName(): void
|
||||
{
|
||||
$name = '';
|
||||
|
||||
$validator = new CompanyValidationByName($name);
|
||||
$json = $validator->getMessageMap();
|
||||
$isValid = $validator->isValid();
|
||||
|
||||
$this->assertEquals($json['error'], 'The company name is empty, please, write the name!!!');
|
||||
$this->assertFalse($isValid);
|
||||
}
|
||||
|
||||
public function testNotExistingCompanyWithName(): void
|
||||
{
|
||||
$name = '404 Company';
|
||||
|
||||
$validator = new CompanyValidationByName($name);
|
||||
$json = $validator->getMessageMap();
|
||||
$isValid = $validator->isValid();
|
||||
|
||||
$this->assertEquals($json['error'], 'A company with the name does not exist!!!');
|
||||
$this->assertFalse($isValid);
|
||||
}
|
||||
|
||||
public function testValidCompanyWithName(): void
|
||||
{
|
||||
$name = 'testCompany';
|
||||
|
||||
$validator = new CompanyValidationByName($name);
|
||||
$json = $validator->getMessageMap();
|
||||
$isValid = $validator->isValid();
|
||||
|
||||
$this->assertEquals($json['ok'], 'A company with the name is valid.');
|
||||
$this->assertTrue($isValid);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Company;
|
||||
|
||||
class CompanyValidationTest extends TestCase
|
||||
{
|
||||
public function testCompanyWithEmptyName(): void
|
||||
{
|
||||
$json = Company::validateWithName('');
|
||||
|
||||
$this->assertEquals($json['error'], 'The company name is empty, please, write the name!!!');
|
||||
}
|
||||
|
||||
public function testNotExistingCompanyWithName(): void
|
||||
{
|
||||
$name = '404 Company';
|
||||
|
||||
$json = Company::validateWithName($name);
|
||||
|
||||
$this->assertEquals($json['error'], 'A company with the name does not exist!!!');
|
||||
}
|
||||
|
||||
public function testValidCompanyWithName(): void
|
||||
{
|
||||
$name = 'testCompany';
|
||||
|
||||
$json = Company::validateWithName($name);
|
||||
|
||||
$this->assertEquals($json['ok'], 'A company with the name is valid.');
|
||||
}
|
||||
}
|
Loading…
Reference in new issue