diff --git a/src/Validator/Domain.php b/src/Validator/Domain.php new file mode 100644 index 00000000..aea0a21c --- /dev/null +++ b/src/Validator/Domain.php @@ -0,0 +1,78 @@ +whitelist = $whitelist; + } + + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + return 'URL host must be one of: ' . \implode(', ', $this->whitelist); + } + + /** + * Is valid + * + * Validation will pass when $value starts with one of the given hosts + * + * @param mixed $value + * @return bool + */ + public function isValid($value): bool + { + $urlValidator = new URL(); + + if (!$urlValidator->isValid($value)) { + return false; + } + + if (\in_array(\parse_url($value, PHP_URL_HOST), $this->whitelist)) { + return true; + } + + return false; + } + + /** + * 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_STRING; + } +} diff --git a/src/Validator/IP.php b/src/Validator/IP.php new file mode 100644 index 00000000..f7a66266 --- /dev/null +++ b/src/Validator/IP.php @@ -0,0 +1,114 @@ +type = $type; + } + + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + return 'Value must be a valid IP address'; + } + + /** + * Is valid + * + * Validation will pass when $value is valid IP address. + * + * @param mixed $value + * @return bool + */ + public function isValid($value): bool + { + switch ($this->type) { + case self::ALL: + if (\filter_var($value, FILTER_VALIDATE_IP)) { + return true; + } + break; + + case self::V4: + if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + return true; + } + break; + + case self::V6: + if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + return true; + } + break; + + default: + return false; + break; + } + + return false; + } + + /** + * 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_STRING; + } +} diff --git a/src/Validator/URL.php b/src/Validator/URL.php new file mode 100644 index 00000000..1c120086 --- /dev/null +++ b/src/Validator/URL.php @@ -0,0 +1,86 @@ +allowedSchemes = $allowedSchemes; + } + + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + if (!empty($this->allowedSchemes)) { + return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')'; + } + + return 'Value must be a valid URL'; + } + + /** + * Is valid + * + * Validation will pass when $value is valid URL. + * + * @param mixed $value + * @return bool + */ + public function isValid($value): bool + { + if (\filter_var($value, FILTER_VALIDATE_URL) === false) { + return false; + } + + if (!empty($this->allowedSchemes) && !\in_array(\parse_url($value, PHP_URL_SCHEME), $this->allowedSchemes)) { + return false; + } + + return true; + } + + /** + * 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_STRING; + } +} diff --git a/tests/Validator/DomainTest.php b/tests/Validator/DomainTest.php new file mode 100644 index 00000000..69c6beee --- /dev/null +++ b/tests/Validator/DomainTest.php @@ -0,0 +1,56 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class DomainTest extends TestCase +{ + /** + * @var Domain + */ + protected $domain = null; + + public function setUp(): void + { + $this->domain = new Domain(); + } + + public function tearDown(): void + { + $this->domain = null; + } + + public function testIsValid() + { + // Assertions + $this->assertEquals(true, $this->domain->isValid('example.com')); + $this->assertEquals(true, $this->domain->isValid('subdomain.example.com')); + $this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com')); + $this->assertEquals(true, $this->domain->isValid('subdomain.example_app.com')); + $this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com')); + $this->assertEquals(true, $this->domain->isValid('subdomain_new.example.com')); + $this->assertEquals(true, $this->domain->isValid('localhost')); + $this->assertEquals(true, $this->domain->isValid('example.io')); + $this->assertEquals(true, $this->domain->isValid('example.org')); + $this->assertEquals(true, $this->domain->isValid('example.org')); + $this->assertEquals(false, $this->domain->isValid(false)); + $this->assertEquals(false, $this->domain->isValid('.')); + $this->assertEquals(false, $this->domain->isValid('..')); + $this->assertEquals(false, $this->domain->isValid('')); + $this->assertEquals(false, $this->domain->isValid(['string', 'string'])); + $this->assertEquals(false, $this->domain->isValid(1)); + $this->assertEquals(false, $this->domain->isValid(1.2)); + } +} diff --git a/tests/Validator/HostTest.php b/tests/Validator/HostTest.php new file mode 100644 index 00000000..f8f192d1 --- /dev/null +++ b/tests/Validator/HostTest.php @@ -0,0 +1,45 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class HostTest extends TestCase +{ + /** + * @var Host + */ + protected $host = null; + + public function setUp():void + { + $this->host = new Host(['example.io', 'subdomain.example.test', 'localhost']); + } + + public function tearDown():void + { + $this->host = null; + } + + public function testIsValid() + { + // Assertions + $this->assertEquals($this->host->isValid('https://example.io/link'), true); + $this->assertEquals($this->host->isValid('https://localhost'), true); + $this->assertEquals($this->host->isValid('localhost'), false); + $this->assertEquals($this->host->isValid('http://subdomain.example.test/path'), true); + $this->assertEquals($this->host->isValid('http://test.subdomain.example.test/path'), false); + $this->assertEquals($this->host->getType(), 'string'); + } +} diff --git a/tests/Validator/IPTest.php b/tests/Validator/IPTest.php new file mode 100644 index 00000000..1be8f78a --- /dev/null +++ b/tests/Validator/IPTest.php @@ -0,0 +1,81 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class IPTest extends TestCase +{ + public function tearDown():void + { + $this->validator = null; + } + + public function testIsValidIP() + { + $validator = new IP(); + + // Assertions + $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); + $this->assertEquals($validator->isValid('109.67.204.101'), true); + $this->assertEquals($validator->isValid(23.5), false); + $this->assertEquals($validator->isValid('23.5'), false); + $this->assertEquals($validator->isValid(null), false); + $this->assertEquals($validator->isValid(true), false); + $this->assertEquals($validator->isValid(false), false); + $this->assertEquals($validator->getType(), 'string'); + } + + public function testIsValidIPALL() + { + $validator = new IP(IP::ALL); + + // Assertions + $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); + $this->assertEquals($validator->isValid('109.67.204.101'), true); + $this->assertEquals($validator->isValid(23.5), false); + $this->assertEquals($validator->isValid('23.5'), false); + $this->assertEquals($validator->isValid(null), false); + $this->assertEquals($validator->isValid(true), false); + $this->assertEquals($validator->isValid(false), false); + } + + public function testIsValidIPV4() + { + $validator = new IP(IP::V4); + + // Assertions + $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false); + $this->assertEquals($validator->isValid('109.67.204.101'), true); + $this->assertEquals($validator->isValid(23.5), false); + $this->assertEquals($validator->isValid('23.5'), false); + $this->assertEquals($validator->isValid(null), false); + $this->assertEquals($validator->isValid(true), false); + $this->assertEquals($validator->isValid(false), false); + } + + public function testIsValidIPV6() + { + $validator = new IP(IP::V6); + + // Assertions + $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); + $this->assertEquals($validator->isValid('109.67.204.101'), false); + $this->assertEquals($validator->isValid(23.5), false); + $this->assertEquals($validator->isValid('23.5'), false); + $this->assertEquals($validator->isValid(null), false); + $this->assertEquals($validator->isValid(true), false); + $this->assertEquals($validator->isValid(false), false); + } +} diff --git a/tests/Validator/URLTest.php b/tests/Validator/URLTest.php new file mode 100644 index 00000000..30c17fba --- /dev/null +++ b/tests/Validator/URLTest.php @@ -0,0 +1,54 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class URLTest extends TestCase +{ + protected ?URL $url; + + public function setUp():void + { + $this->url = new URL(); + } + + public function tearDown():void + { + $this->url = null; + } + + public function testIsValid(): void + { + $this->assertEquals('Value must be a valid URL', $this->url->getDescription()); + $this->assertEquals(true, $this->url->isValid('http://example.com')); + $this->assertEquals(true, $this->url->isValid('https://example.com')); + $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol + $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol + $this->assertEquals(false, $this->url->isValid('http:/example.com')); + $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); + $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); + $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); + $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); + } + + public function testIsValidAllowedSchemes(): void + { + $this->url = new URL(['http', 'https']); + $this->assertEquals('Value must be a valid URL with following schemes (http, https)', $this->url->getDescription()); + $this->assertEquals(true, $this->url->isValid('http://example.com')); + $this->assertEquals(true, $this->url->isValid('https://example.com')); + $this->assertEquals(false, $this->url->isValid('gopher://www.example.com')); + } +}