-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from alleyinteractive/feature/freeform-validator
Add `FreeformValidator` abstract class
- Loading branch information
Showing
10 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the laminas-validator-extensions package. | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Alley\Validator; | ||
|
||
use Laminas\Validator\ValidatorInterface; | ||
use Stringable; | ||
|
||
abstract class FreeformValidator implements ValidatorInterface | ||
{ | ||
private array $messages = []; | ||
|
||
/** | ||
* Apply validation logic and add any validation errors. | ||
* | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
abstract protected function testValue($value): void; | ||
|
||
final public function isValid($value): bool | ||
{ | ||
$this->messages = []; | ||
$this->testValue($value); | ||
return \count($this->getMessages()) === 0; | ||
} | ||
|
||
final public function getMessages() | ||
{ | ||
return $this->messages; | ||
} | ||
|
||
/** | ||
* Add validation error. | ||
* | ||
* @param string $key Error key. | ||
* @param string $message Message text. | ||
* @return void | ||
*/ | ||
final protected function error(string $key, $message): void | ||
{ | ||
$this->messages[$key] = (string) $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the laminas-validator-extensions package. | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Alley\Validator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class FreeformValidatorTest extends TestCase | ||
{ | ||
public function testSampleImplementation() | ||
{ | ||
$actual = new class () extends FreeformValidator { | ||
protected function testValue($value): void | ||
{ | ||
$fizz = new DivisibleBy(['divisor' => 3]); | ||
$buzz = new DivisibleBy(['divisor' => 5]); | ||
$fizzbuzz = new FastFailValidatorChain([$fizz, $buzz]); | ||
|
||
if ($fizz->isValid($value)) { | ||
$this->error('fizz', 'Fizz'); | ||
} | ||
|
||
if ($buzz->isValid($value)) { | ||
$this->error('buzz', 'Buzz'); | ||
} | ||
|
||
if ($fizzbuzz->isValid($value)) { | ||
$this->error('fizzbuzz', 'FizzBuzz'); | ||
} | ||
} | ||
}; | ||
|
||
$this->assertFalse($actual->isValid(9)); | ||
$this->assertSame(['fizz' => 'Fizz'], $actual->getMessages()); | ||
|
||
$this->assertTrue($actual->isValid(1)); | ||
$this->assertSame([], $actual->getMessages()); | ||
|
||
$this->assertFalse($actual->isValid(15)); | ||
$this->assertSame(['fizz' => 'Fizz', 'buzz' => 'Buzz', 'fizzbuzz' => 'FizzBuzz'], $actual->getMessages()); | ||
} | ||
} |