-
Notifications
You must be signed in to change notification settings - Fork 34
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
1 parent
8fe57da
commit 373be4c
Showing
2 changed files
with
142 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,104 @@ | ||
<?php | ||
|
||
namespace Utopia\Validator; | ||
|
||
use Utopia\Validator; | ||
|
||
/** | ||
* Numeric WhiteList | ||
* | ||
* Checks if a variable is inside predefined numerical white list. | ||
*/ | ||
class NumericWhiteList extends Validator | ||
{ | ||
/** | ||
* @var int[] | ||
*/ | ||
protected array $list; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected bool $strict; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* Sets a white list array. | ||
* | ||
* @param int[] $list | ||
* @param bool $strict disable type check | ||
*/ | ||
public function __construct(array $list, bool $strict = flase) | ||
{ | ||
$this->list = $list; | ||
$this->strict = $strict; | ||
} | ||
|
||
/** | ||
* Get List of All Allowed Values | ||
* | ||
* @return array | ||
*/ | ||
public function getList(): array | ||
{ | ||
return $this->list; | ||
} | ||
|
||
/** | ||
* Get Description | ||
* | ||
* Returns validator description | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
return 'Value must be one of ('.\implode(', ', $this->list).')'; | ||
} | ||
|
||
/** | ||
* Is array | ||
* | ||
* Function will return true if object is array. | ||
* | ||
* @return bool | ||
*/ | ||
public function isArray(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Get Type | ||
* | ||
* Returns validator type. | ||
* | ||
* @return string | ||
*/ | ||
public function getType(): string | ||
{ | ||
return self::TYPE_INTEGER; | ||
} | ||
|
||
/** | ||
* Is valid | ||
* | ||
* Validation will pass if $value is in the white list array. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid(mixed $value): bool | ||
{ | ||
if (\is_array($value)) { | ||
return false; | ||
} | ||
|
||
if (!\in_array($value, $this->list, $this->strict)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Utopia\Validator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class NumericWhiteListTest extends TestCase | ||
{ | ||
public function testCanValidateStrictly() | ||
{ | ||
$whiteList = new NumericWhiteList([1, 2, 3, 4], true); | ||
|
||
$this->assertTrue($whiteList->isValid(3)); | ||
$this->assertTrue($whiteList->isValid(4)); | ||
|
||
$this->assertFalse($whiteList->isValid('STRING1')); | ||
$this->assertFalse($whiteList->isValid('strIng1')); | ||
$this->assertFalse($whiteList->isValid('3')); | ||
$this->assertFalse($whiteList->isValid(5)); | ||
$this->assertFalse($whiteList->isArray()); | ||
$this->assertEquals($whiteList->getList(), [1 ,2, 3, 4]); | ||
$this->assertEquals(\Utopia\Validator::TYPE_INTEGER, $whiteList->getType()); | ||
} | ||
|
||
public function testCanValidateLoosely(): void | ||
{ | ||
$whiteList = new NumericWhiteList([1, 2, 3, 4]); | ||
|
||
$this->assertTrue($whiteList->isValid(3)); | ||
$this->assertTrue($whiteList->isValid(4)); | ||
$this->assertTrue($whiteList->isValid('3')); | ||
$this->assertTrue($whiteList->isValid('4')); | ||
$this->assertFalse($whiteList->isValid('STRING1')); | ||
$this->assertFalse($whiteList->isValid('strIng1')); | ||
$this->assertFalse($whiteList->isValid('5')); | ||
$this->assertEquals($whiteList->getList(), [1, 2, 3, 4]); | ||
} | ||
} |