diff --git a/composer.lock b/composer.lock index 538ea48c..01fc5fe1 100644 --- a/composer.lock +++ b/composer.lock @@ -2254,16 +2254,16 @@ }, { "name": "sebastian/global-state", - "version": "5.0.3", + "version": "5.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49" + "reference": "19c519631c5a511b7ed0ad64a6713fdb3fd25fe4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/23bd5951f7ff26f12d4e3242864df3e08dec4e49", - "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/19c519631c5a511b7ed0ad64a6713fdb3fd25fe4", + "reference": "19c519631c5a511b7ed0ad64a6713fdb3fd25fe4", "shasum": "" }, "require": { @@ -2306,7 +2306,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.3" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.4" }, "funding": [ { @@ -2314,7 +2314,7 @@ "type": "github" } ], - "time": "2021-06-11T13:31:12+00:00" + "time": "2022-02-10T07:01:19+00:00" }, { "name": "sebastian/lines-of-code", diff --git a/src/Validator/Text.php b/src/Validator/Text.php index b27c79d5..213609b3 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -23,21 +23,33 @@ */ class Text extends Validator { + const NUMBERS = [ '0','1','2','3','4','5','6','7','8','9' ]; + const ALPHABET_UPPER = [ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' ]; + const ALPHABET_LOWER = [ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' ]; + /** * @var int */ protected int $length = 0; + /** + * @var string[] + */ + protected array $allowList = []; + /** * Text constructor. * - * Validate text with maximum length $length. Use $length = 0 for unlimited length + * Validate text with maximum length $length. Use $length = 0 for unlimited length. + * Optionally, provide allowList characters array $allowList to only allow specific character. * * @param int $length + * @param string[] $allowList */ - public function __construct(int $length) + public function __construct(int $length, array $allowList = []) { $this->length = $length; + $this->allowList = $allowList; } /** @@ -54,6 +66,11 @@ public function getDescription(): string if ($this->length) { $message .= ' and no longer than ' . $this->length . ' chars'; } + + if ($this->allowList) { + $message .= ' and only consist of \'' . \join(', ', $this->allowList) . '\' chars'; + } + return $message; } @@ -99,6 +116,14 @@ public function isValid(mixed $value): bool return false; } + if(\count($this->allowList) > 0) { + foreach (\str_split($value) as $char) { + if(!\in_array($char, $this->allowList)) { + return false; + } + } + } + return true; } } diff --git a/tests/Validator/TextTest.php b/tests/Validator/TextTest.php index f99dbaa8..72c3106d 100755 --- a/tests/Validator/TextTest.php +++ b/tests/Validator/TextTest.php @@ -45,4 +45,64 @@ public function testIsValid() $this->assertEquals(false, $this->text->isArray()); $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->text->getType()); } + + public function testAllowList() + { + // Test lowercase alphabet + $this->validator = new Text(100, Text::ALPHABET_LOWER); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->validator->getType()); + $this->assertEquals(false, $this->validator->isArray()); + $this->assertEquals(true, $this->validator->isValid('qwertzuiopasdfghjklyxcvbnm')); + $this->assertEquals(true, $this->validator->isValid('hello')); + $this->assertEquals(true, $this->validator->isValid('world')); + $this->assertEquals(false, $this->validator->isValid('hello world')); + $this->assertEquals(false, $this->validator->isValid('Hello')); + $this->assertEquals(false, $this->validator->isValid('worlD')); + $this->assertEquals(false, $this->validator->isValid('hello123')); + + // Test uppercase alphabet + $this->validator = new Text(100, Text::ALPHABET_UPPER); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->validator->getType()); + $this->assertEquals(false, $this->validator->isArray()); + $this->assertEquals(true, $this->validator->isValid('QWERTZUIOPASDFGHJKLYXCVBNM')); + $this->assertEquals(true, $this->validator->isValid('HELLO')); + $this->assertEquals(true, $this->validator->isValid('WORLD')); + $this->assertEquals(false, $this->validator->isValid('HELLO WORLD')); + $this->assertEquals(false, $this->validator->isValid('hELLO')); + $this->assertEquals(false, $this->validator->isValid('WORLd')); + $this->assertEquals(false, $this->validator->isValid('HELLO123')); + + // Test numbers + $this->validator = new Text(100, Text::NUMBERS); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->validator->getType()); + $this->assertEquals(false, $this->validator->isArray()); + $this->assertEquals(true, $this->validator->isValid('1234567890')); + $this->assertEquals(true, $this->validator->isValid('123')); + $this->assertEquals(false, $this->validator->isValid('123 456')); + $this->assertEquals(false, $this->validator->isValid('hello123')); + + // Test combination of allowLists + $this->validator = new Text(100, [ + ...Text::ALPHABET_LOWER, + ...Text::ALPHABET_UPPER, + ...Text::NUMBERS + ]); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->validator->getType()); + $this->assertEquals(false, $this->validator->isArray()); + $this->assertEquals(true, $this->validator->isValid('1234567890')); + $this->assertEquals(true, $this->validator->isValid('qwertzuiopasdfghjklyxcvbnm')); + $this->assertEquals(true, $this->validator->isValid('QWERTZUIOPASDFGHJKLYXCVBNM')); + $this->assertEquals(true, $this->validator->isValid('QWERTZUIOPASDFGHJKLYXCVBNMqwertzuiopasdfghjklyxcvbnm1234567890')); + $this->assertEquals(false, $this->validator->isValid('hello-world')); + $this->assertEquals(false, $this->validator->isValid('hello_world')); + $this->assertEquals(false, $this->validator->isValid('hello/world')); + + // Test length validation + $this->validator = new Text(5, Text::ALPHABET_LOWER); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->validator->getType()); + $this->assertEquals(false, $this->validator->isArray()); + $this->assertEquals(true, $this->validator->isValid('hell')); + $this->assertEquals(true, $this->validator->isValid('hello')); + $this->assertEquals(false, $this->validator->isValid('hellow')); + } }