Skip to content

Commit e3bbca0

Browse files
authored
Merge pull request #136 from utopia-php/feat-multiple-validator-adapter
Feat: Rules for Multiple validator
2 parents ae3bdee + 726540d commit e3bbca0

File tree

7 files changed

+469
-295
lines changed

7 files changed

+469
-295
lines changed

composer.lock

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

src/Http/Validator/AllOf.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Utopia\Http\Validator;
4+
5+
use Utopia\Http\Validator;
6+
7+
/**
8+
* Ensure all of the validators from a list passed the check
9+
*
10+
* @package Utopia\Validator
11+
*/
12+
class AllOf extends Validator
13+
{
14+
protected ?Validator $failedRule = null;
15+
16+
/**
17+
* @param array<Validator> $validators
18+
*/
19+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
20+
{
21+
}
22+
23+
/**
24+
* Get Description
25+
*
26+
* Returns validator description
27+
*
28+
* @return string
29+
*/
30+
public function getDescription(): string
31+
{
32+
if(!(\is_null($this->failedRule))) {
33+
$description = $this->failedRule->getDescription();
34+
} else {
35+
$description = $this->validators[0]->getDescription();
36+
}
37+
38+
return $description;
39+
}
40+
41+
/**
42+
* Is valid
43+
*
44+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
45+
*
46+
* @param mixed $value
47+
* @return bool
48+
*/
49+
public function isValid(mixed $value): bool
50+
{
51+
foreach ($this->validators as $rule) {
52+
$valid = $rule->isValid($value);
53+
54+
if(!$valid) {
55+
$this->failedRule = $rule;
56+
return false;
57+
}
58+
}
59+
60+
return true;
61+
}
62+
63+
/**
64+
* Get Type
65+
*
66+
* Returns validator type.
67+
*
68+
* @return string
69+
*/
70+
public function getType(): string
71+
{
72+
return $this->type;
73+
}
74+
75+
/**
76+
* Is array
77+
*
78+
* Function will return true if object is array.
79+
*
80+
* @return bool
81+
*/
82+
public function isArray(): bool
83+
{
84+
return true;
85+
}
86+
}

src/Http/Validator/AnyOf.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Utopia\Http\Validator;
4+
5+
use Utopia\Http\Validator;
6+
7+
/**
8+
* Ensure at least one validator from a list passed the check
9+
*
10+
* @package Utopia\Validator
11+
*/
12+
class AnyOf extends Validator
13+
{
14+
protected ?Validator $failedRule = null;
15+
16+
/**
17+
* @param array<Validator> $validators
18+
*/
19+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
20+
{
21+
}
22+
23+
/**
24+
* Get Description
25+
*
26+
* Returns validator description
27+
*
28+
* @return string
29+
*/
30+
public function getDescription(): string
31+
{
32+
if(!(\is_null($this->failedRule))) {
33+
$description = $this->failedRule->getDescription();
34+
} else {
35+
$description = $this->validators[0]->getDescription();
36+
}
37+
38+
return $description;
39+
}
40+
41+
/**
42+
* Is valid
43+
*
44+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
45+
*
46+
* @param mixed $value
47+
* @return bool
48+
*/
49+
public function isValid(mixed $value): bool
50+
{
51+
foreach ($this->validators as $rule) {
52+
$valid = $rule->isValid($value);
53+
54+
$this->failedRule = $rule;
55+
56+
if($valid) {
57+
return true;
58+
}
59+
}
60+
61+
return false;
62+
}
63+
64+
/**
65+
* Get Type
66+
*
67+
* Returns validator type.
68+
*
69+
* @return string
70+
*/
71+
public function getType(): string
72+
{
73+
return $this->type;
74+
}
75+
76+
/**
77+
* Is array
78+
*
79+
* Function will return true if object is array.
80+
*
81+
* @return bool
82+
*/
83+
public function isArray(): bool
84+
{
85+
return true;
86+
}
87+
}

src/Http/Validator/Multiple.php

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/Http/Validator/NoneOf.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Utopia\Http\Validator;
4+
5+
use Utopia\Http\Validator;
6+
7+
/**
8+
* Ensure no validators from a list passed the check
9+
*
10+
* @package Utopia\Validator
11+
*/
12+
class NoneOf extends Validator
13+
{
14+
protected ?Validator $failedRule = null;
15+
16+
/**
17+
* @param array<Validator> $validators
18+
*/
19+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
20+
{
21+
}
22+
23+
/**
24+
* Get Description
25+
*
26+
* Returns validator description
27+
*
28+
* @return string
29+
*/
30+
public function getDescription(): string
31+
{
32+
$description = '';
33+
34+
if(!(\is_null($this->failedRule))) {
35+
$description = $this->failedRule->getDescription();
36+
} else {
37+
$description = $this->validators[0]->getDescription();
38+
}
39+
40+
return $description;
41+
}
42+
43+
/**
44+
* Is valid
45+
*
46+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
47+
*
48+
* @param mixed $value
49+
* @return bool
50+
*/
51+
public function isValid(mixed $value): bool
52+
{
53+
foreach ($this->validators as $rule) {
54+
$valid = $rule->isValid($value);
55+
56+
if($valid) {
57+
$this->failedRule = $rule;
58+
return false;
59+
}
60+
}
61+
62+
return true;
63+
}
64+
65+
/**
66+
* Get Type
67+
*
68+
* Returns validator type.
69+
*
70+
* @return string
71+
*/
72+
public function getType(): string
73+
{
74+
return $this->type;
75+
}
76+
77+
/**
78+
* Is array
79+
*
80+
* Function will return true if object is array.
81+
*
82+
* @return bool
83+
*/
84+
public function isArray(): bool
85+
{
86+
return true;
87+
}
88+
}

0 commit comments

Comments
 (0)