-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
namespace Packaged\Validate\Validators; | ||
|
||
use Generator; | ||
use Packaged\Validate\AbstractSerializableValidator; | ||
use Packaged\Validate\SerializableValidator; | ||
|
||
class RequiredValidator extends AbstractSerializableValidator | ||
{ | ||
public static function deserialize($configuration): SerializableValidator | ||
{ | ||
return new static(); | ||
} | ||
|
||
public function serialize(): array | ||
{ | ||
return []; | ||
} | ||
|
||
protected function _doValidate($value): Generator | ||
{ | ||
if($value === null || $value === '') | ||
{ | ||
yield $this->_makeError('required'); | ||
} | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
namespace Packaged\Validate\Tests; | ||
|
||
use Packaged\Validate\Validators\RequiredValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RequiredValidatorTest extends TestCase | ||
{ | ||
public function testRequired() | ||
{ | ||
$validator = new RequiredValidator(); | ||
$this->assertFalse($validator->isValid(null)); | ||
$this->assertFalse($validator->isValid('')); | ||
$this->assertTrue($validator->isValid('test')); | ||
$this->assertTrue($validator->isValid(0)); | ||
$this->assertTrue($validator->isValid(false)); | ||
$this->assertTrue($validator->isValid(true)); | ||
} | ||
} |