Skip to content

Commit a75add2

Browse files
committed
Add multiple validator
1 parent e50d2d1 commit a75add2

3 files changed

Lines changed: 186 additions & 40 deletions

File tree

composer.lock

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Validator/Multiple.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Utopia\Validator;
4+
5+
use Utopia\Validator;
6+
7+
/**
8+
* Multiple
9+
*
10+
* Multiple validator is a container of multiple validations each acting as a rule.
11+
*
12+
* @package Utopia\Validator
13+
*/
14+
class Multiple extends Validator
15+
{
16+
/**
17+
* @var Validator[]
18+
*/
19+
protected $rules = [];
20+
21+
/**
22+
* Constructor
23+
*
24+
* Multiple constructor can get any number of arguments containing Validator instances using PHP func_get_args function.
25+
*
26+
* Example:
27+
*
28+
* $multiple = new Multiple($validator1, $validator2, $validator3);
29+
*/
30+
public function __construct()
31+
{
32+
// array of all method arguments
33+
$rules = \func_get_args();
34+
35+
foreach ($rules as $rule) {
36+
$this->addRule($rule);
37+
}
38+
}
39+
40+
/**
41+
* Add rule
42+
*
43+
* Add a new rule to the end of the rules containing array
44+
*
45+
* @param Validator $rule
46+
* @return $this
47+
*/
48+
public function addRule(Validator $rule)
49+
{
50+
$this->rules[] = $rule;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* Get Description
57+
*
58+
* Returns validator description
59+
*
60+
* @return string
61+
*/
62+
public function getDescription(): string
63+
{
64+
$description = '';
65+
foreach ($this->rules as $key => $rule) {
66+
$description .= ++$key . '. ' . $rule->getDescription() . " \n";
67+
}
68+
69+
return $description;
70+
}
71+
72+
/**
73+
* Is valid
74+
*
75+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
76+
*
77+
* @param mixed $value
78+
* @return bool
79+
*/
80+
public function isValid(mixed $value): bool
81+
{
82+
foreach ($this->rules as $rule) { /* @var $rule Validator */
83+
if (false === $rule->isValid($value)) {
84+
return false;
85+
}
86+
}
87+
88+
return true;
89+
}
90+
91+
/**
92+
* Get Type
93+
*
94+
* Returns validator type.
95+
*
96+
* @return string
97+
*/
98+
public function getType(): string
99+
{
100+
return self::TYPE_MIXED;
101+
}
102+
103+
/**
104+
* Is array
105+
*
106+
* Function will return true if object is array.
107+
*
108+
* @return bool
109+
*/
110+
public function isArray(): bool
111+
{
112+
return true;
113+
}
114+
}

tests/Validator/MultipleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Utopia\Validator;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class MultipleTest extends TestCase
8+
{
9+
protected Multiple $validator;
10+
11+
public function setUp(): void
12+
{
13+
$this->validator = new Multiple(new Text(20), new URL());
14+
}
15+
16+
public function testIsValid()
17+
{
18+
// Valid URL but invalid text length
19+
$this->assertFalse($this->validator->isValid('http://example.com/very-long-url'));
20+
21+
// Valid text within length, but invalid URL
22+
$this->assertFalse($this->validator->isValid('hello world'));
23+
24+
// Both conditions satisfied
25+
$this->assertTrue($this->validator->isValid('http://example.com'));
26+
$this->assertTrue($this->validator->isValid('https://google.com'));
27+
28+
// Neither condition satisfied
29+
$this->assertFalse($this->validator->isValid('example.com/hello-world'));
30+
$this->assertFalse($this->validator->isValid(''));
31+
}
32+
}

0 commit comments

Comments
 (0)