-
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 #2 from alleyinteractive/feature/supporting-valida…
…tors Add `Not` and `AnyValidator` validators
- Loading branch information
Showing
7 changed files
with
260 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?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 Countable; | ||
use Laminas\Validator\ValidatorInterface; | ||
use ReturnTypeWillChange; | ||
|
||
final class AnyValidator implements Countable, ValidatorInterface | ||
{ | ||
/** | ||
* Validator chain. | ||
* | ||
* @var ValidatorInterface[] | ||
*/ | ||
protected array $validators = []; | ||
|
||
/** | ||
* Array of validation failure messages. | ||
* | ||
* @var string[] | ||
*/ | ||
protected $messages = []; | ||
|
||
/** | ||
* @param ValidatorInterface[] $validators | ||
*/ | ||
public function __construct(array $validators) | ||
{ | ||
foreach ($validators as $validator) { | ||
$this->attach($validator); | ||
} | ||
} | ||
|
||
/** | ||
* Attach a validator to the end of the chain. | ||
* | ||
* @param ValidatorInterface $validator | ||
* @return self | ||
*/ | ||
public function attach(ValidatorInterface $validator) | ||
{ | ||
$this->validators[] = $validator; | ||
|
||
return $this; | ||
} | ||
|
||
public function isValid($value) | ||
{ | ||
$this->messages = []; | ||
|
||
if ($this->count() === 0) { | ||
// Consistent with `\Laminas\Validator\ValidatorChain()`. | ||
return true; | ||
} | ||
|
||
foreach ($this->validators as $validator) { | ||
if ($validator->isValid($value)) { | ||
return true; | ||
} | ||
} | ||
|
||
foreach ($this->validators as $validator) { | ||
$this->messages = array_replace($this->messages, $validator->getMessages()); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getMessages() | ||
{ | ||
return $this->messages; | ||
} | ||
|
||
#[ReturnTypeWillChange] | ||
public function count() | ||
{ | ||
return \count($this->validators); | ||
} | ||
} |
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,45 @@ | ||
<?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; | ||
|
||
final class Not implements ValidatorInterface | ||
{ | ||
private ValidatorInterface $origin; | ||
|
||
private string $message; | ||
|
||
public function __construct(ValidatorInterface $origin, string $message) | ||
{ | ||
$this->origin = $origin; | ||
$this->message = $message; | ||
} | ||
|
||
public function isValid($value) | ||
{ | ||
return !$this->origin->isValid($value); | ||
} | ||
|
||
public function getMessages() | ||
{ | ||
$messages = []; | ||
|
||
if (\count($this->origin->getMessages()) === 0) { | ||
$messages[] = $this->message; | ||
} | ||
|
||
return $messages; | ||
} | ||
} |
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,47 @@ | ||
<?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\GreaterThan; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AnyValidatorTest extends TestCase | ||
{ | ||
public function testNoValidators() | ||
{ | ||
$validator = new AnyValidator([]); | ||
$this->assertTrue($validator->isValid(42)); | ||
} | ||
|
||
public function testValidValidator() | ||
{ | ||
$validator = new AnyValidator([new AlwaysValid()]); | ||
$this->assertTrue($validator->isValid(42)); | ||
} | ||
|
||
public function testInvalidValidator() | ||
{ | ||
$validator = new AnyValidator([new GreaterThan(['min' => 43])]); | ||
$this->assertFalse($validator->isValid(42)); | ||
} | ||
|
||
public function testFirstValidValidator() | ||
{ | ||
$validator = new AnyValidator([ | ||
new AlwaysValid(), | ||
new GreaterThan(['min' => 43]), | ||
]); | ||
$this->assertTrue($validator->isValid(42)); | ||
} | ||
} |
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,28 @@ | ||
<?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 NotTest extends TestCase | ||
{ | ||
public function testNegation() | ||
{ | ||
$origin = new AlwaysValid(); | ||
$actual = new Not($origin, 'foo'); | ||
$value = 42; | ||
$this->assertNotSame($actual->isValid($value), $origin->isValid($value)); | ||
$this->assertCount(1, $actual->getMessages()); | ||
} | ||
} |