Compare commits
2
Commits
a4c0b3463b
...
7662d98bce
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7662d98bce | ||
|
|
da847273b8 |
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Validation\Messages;
|
||||||
|
|
||||||
|
use App\Models\Validation\Validators\Validator;
|
||||||
|
|
||||||
|
abstract class InformativeValidator extends Validator {
|
||||||
|
protected Validator $validator;
|
||||||
|
protected string $message;
|
||||||
|
|
||||||
|
public function isCurrentValid(): bool
|
||||||
|
{
|
||||||
|
return $this->validator->isCurrentValid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isValid(): bool
|
||||||
|
{
|
||||||
|
return $this->validator->isValid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setValidator($validator): void
|
||||||
|
{
|
||||||
|
$this->validator = $validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMessage($message): void
|
||||||
|
{
|
||||||
|
$this->message = $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMessage(): string
|
||||||
|
{
|
||||||
|
if(!$this->isCurrentValid())
|
||||||
|
return $this->message;
|
||||||
|
|
||||||
|
return $this->nextValidator->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function okStatus(): array
|
||||||
|
{
|
||||||
|
if(!$this->isCurrentValid())
|
||||||
|
return ['error' => $this->message];
|
||||||
|
|
||||||
|
return $this->nextValidator->okStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Validation\Messages;
|
||||||
|
|
||||||
|
use App\Models\Validation\Messages\InformativeValidator;
|
||||||
|
use App\Models\Validation\Validators\OkValidator;
|
||||||
|
|
||||||
|
class OkInformativeValidator extends InformativeValidator {
|
||||||
|
public function __construct($message)
|
||||||
|
{
|
||||||
|
$this->message = $message;
|
||||||
|
$this->validator = new OkValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMessage(): string
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function okStatus(): array
|
||||||
|
{
|
||||||
|
return ['ok' => $this->getMessage()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Validation\Messages;
|
||||||
|
|
||||||
|
use App\Models\Validation\Messages\InformativeValidator;
|
||||||
|
use App\Models\Validation\Validators\UpperRangeValidator;
|
||||||
|
|
||||||
|
class UpperRangeInformativeValidator extends InformativeValidator {
|
||||||
|
protected InformativeValidator $nextValidator;
|
||||||
|
|
||||||
|
public function __construct(string $message, UpperRangeValidator $validator, InformativeValidator $nextValidator)
|
||||||
|
{
|
||||||
|
$this->setMessage($message);
|
||||||
|
$this->setValidator($validator);
|
||||||
|
$this->nextValidator = $nextValidator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,9 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Models\Validation\Validators;
|
namespace App\Models\Validation\Validators;
|
||||||
|
|
||||||
use App\Models\Validation\Validators\ValidationInterface;
|
use App\Models\Validation\Validators\Validator;
|
||||||
|
|
||||||
|
class OkValidator extends Validator {
|
||||||
|
public function isCurrentValid(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
class OkValidator implements ValidationInterface {
|
|
||||||
public function isValid(): bool
|
public function isValid(): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Models\Validation\Validators;
|
namespace App\Models\Validation\Validators;
|
||||||
|
|
||||||
use App\Models\Validation\Validators\ValidationInterface;
|
use App\Models\Validation\Validators\Validator;
|
||||||
use App\Models\Validation\Validators\ValidationTrait;
|
|
||||||
|
|
||||||
class UpperRangeValidator implements ValidationInterface {
|
class UpperRangeValidator extends Validator {
|
||||||
private ValidationInterface $nextValidator;
|
|
||||||
private int $value;
|
private int $value;
|
||||||
private int $rangeLimit;
|
private int $rangeLimit;
|
||||||
|
protected Validator $nextValidator;
|
||||||
|
|
||||||
public function __construct(int $value, int $rangeLimit, ValidationInterface $nextValidator)
|
public function __construct(int $value, int $rangeLimit, Validator $nextValidator)
|
||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
$this->rangeLimit = $rangeLimit;
|
$this->rangeLimit = $rangeLimit;
|
||||||
@@ -24,10 +23,5 @@ class UpperRangeValidator implements ValidationInterface {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValid(): bool
|
|
||||||
{
|
|
||||||
return $this->isCurrentValid() && $this->nextValidator->isValid();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Validation\Validators;
|
||||||
|
|
||||||
|
abstract class Validator {
|
||||||
|
abstract public function isCurrentValid(): bool;
|
||||||
|
|
||||||
|
public function isValid(): bool
|
||||||
|
{
|
||||||
|
return $this->isCurrentValid() && $this->nextValidator->isValid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
use App\Models\Validation\Messages\OkInformativeValidator;
|
||||||
|
|
||||||
|
class OkInformativeValidatorTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testIsValid(): void
|
||||||
|
{
|
||||||
|
$message = 'All fine';
|
||||||
|
$validator = new OkInformativeValidator($message);
|
||||||
|
|
||||||
|
$this->assertTrue($validator->isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetMessage(): void
|
||||||
|
{
|
||||||
|
$message = 'All fine';
|
||||||
|
$validator = new OkInformativeValidator($message);
|
||||||
|
|
||||||
|
$this->assertEquals($validator->getMessage(), $message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
use App\Models\Validation\Messages\UpperRangeInformativeValidator;
|
||||||
|
use App\Models\Validation\Messages\OkInformativeValidator;
|
||||||
|
use App\Models\Validation\Validators\UpperRangeValidator;
|
||||||
|
use App\Models\Validation\Validators\OkValidator;
|
||||||
|
|
||||||
|
class UpperRangeInformativeValidatorTest extends TestCase
|
||||||
|
{
|
||||||
|
protected $messages = [
|
||||||
|
'ok' => 'valid',
|
||||||
|
'error' => 'invalid',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function dataProvider() {
|
||||||
|
return [
|
||||||
|
'Valid Case for 10' => [
|
||||||
|
'value' => 10,
|
||||||
|
'limit' => 10,
|
||||||
|
'isValid' => true,
|
||||||
|
'message' => $this->messages['ok'],
|
||||||
|
],
|
||||||
|
'Valid Case for 255' => [
|
||||||
|
'value' => 0,
|
||||||
|
'limit' => 255,
|
||||||
|
'isValid' => true,
|
||||||
|
'message' => $this->messages['ok'],
|
||||||
|
],
|
||||||
|
'Invalid Case for 10' => [
|
||||||
|
'value' => 11,
|
||||||
|
'limit' => 10,
|
||||||
|
'isValid' => false,
|
||||||
|
'message' => $this->messages['error'],
|
||||||
|
],
|
||||||
|
'Invalid Case for 255' => [
|
||||||
|
'value' => 256,
|
||||||
|
'limit' => 255,
|
||||||
|
'isValid' => false,
|
||||||
|
'message' => $this->messages['error'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUpValidator(int $value, int $rangeLimit): UpperRangeInformativeValidator
|
||||||
|
{
|
||||||
|
$upperRangeValidator = new UpperRangeValidator($value, $rangeLimit, new OkValidator());
|
||||||
|
|
||||||
|
return new UpperRangeInformativeValidator(
|
||||||
|
$this->messages['error'],
|
||||||
|
$upperRangeValidator,
|
||||||
|
new OkInformativeValidator($this->messages['ok']),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataProvider
|
||||||
|
*/
|
||||||
|
public function testGetMessage(int $value, int $limit, bool $isValid, string $message): void
|
||||||
|
{
|
||||||
|
$validator = $this->setUpValidator($value, $limit);
|
||||||
|
|
||||||
|
$this->assertEquals($validator->isValid(), $isValid);
|
||||||
|
$this->assertEquals($validator->getMessage(), $message);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user