-
Notifications
You must be signed in to change notification settings - Fork 0
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 #6 from wmde/integer_value_validator
Create integer value validator
- Loading branch information
Showing
2 changed files
with
51 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,23 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace WMDE\FunValidators\Validators; | ||
|
||
use WMDE\FunValidators\ConstraintViolation; | ||
use WMDE\FunValidators\ValidationResult; | ||
|
||
/** | ||
* @license GNU GPL v2+ | ||
*/ | ||
class IntegerValueValidator { | ||
|
||
public function validate( string $input ): ValidationResult { | ||
if ( !ctype_digit( $input ) ) { | ||
return new ValidationResult( new ConstraintViolation( $input, 'field_numeric' ) ); | ||
} | ||
|
||
return new ValidationResult(); | ||
} | ||
|
||
} |
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 | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace WMDE\FunValidators\Tests\Unit\Validators; | ||
|
||
use WMDE\FunValidators\Validators\IntegerValueValidator; | ||
|
||
/** | ||
* @covers \WMDE\FunValidators\Validators\IntegerValueValidator | ||
* | ||
* @license GNU GPL v2+ | ||
*/ | ||
class IntegerValueValidatorTest extends \PHPUnit\Framework\TestCase { | ||
|
||
public function testGivenIntegerValues_validationSucceeds(): void { | ||
$validator = new IntegerValueValidator(); | ||
$this->assertTrue( $validator->validate( '1234567890' )->isSuccessful() ); | ||
$this->assertTrue( $validator->validate( '000123456789' )->isSuccessful() ); | ||
} | ||
|
||
public function testGivenInvalidValues_validationFails(): void { | ||
$validator = new IntegerValueValidator(); | ||
$this->assertFalse( $validator->validate( '-1234567890' )->isSuccessful() ); | ||
$this->assertFalse( $validator->validate( '21391e213123' )->isSuccessful() ); | ||
$this->assertFalse( $validator->validate( '21391e213123' )->isSuccessful() ); | ||
} | ||
} |