Skip to content

Commit 102c44b

Browse files
committed
Separate validators
1 parent 65d4fb8 commit 102c44b

File tree

5 files changed

+261
-158
lines changed

5 files changed

+261
-158
lines changed

src/Http/Validator/AllOf.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
/**
15+
* @param array<Validator> $validators
16+
*/
17+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
18+
{
19+
}
20+
21+
/**
22+
* Get Description
23+
*
24+
* Returns validator description
25+
*
26+
* @return string
27+
*/
28+
public function getDescription(): string
29+
{
30+
$description = '';
31+
foreach ($this->validators as $key => $rule) {
32+
$description .= ++$key . '. ' . $rule->getDescription() . " \n";
33+
}
34+
35+
return $description;
36+
}
37+
38+
/**
39+
* Is valid
40+
*
41+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
42+
*
43+
* @param mixed $value
44+
* @return bool
45+
*/
46+
public function isValid(mixed $value): bool
47+
{
48+
foreach ($this->validators as $rule) {
49+
$valid = $rule->isValid($value);
50+
51+
if(!$valid) {
52+
return false;
53+
}
54+
}
55+
56+
return true;
57+
}
58+
59+
/**
60+
* Get Type
61+
*
62+
* Returns validator type.
63+
*
64+
* @return string
65+
*/
66+
public function getType(): string
67+
{
68+
return $this->type;
69+
}
70+
71+
/**
72+
* Is array
73+
*
74+
* Function will return true if object is array.
75+
*
76+
* @return bool
77+
*/
78+
public function isArray(): bool
79+
{
80+
return true;
81+
}
82+
}

src/Http/Validator/AnyOf.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
/**
15+
* @param array<Validator> $validators
16+
*/
17+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
18+
{
19+
}
20+
21+
/**
22+
* Get Description
23+
*
24+
* Returns validator description
25+
*
26+
* @return string
27+
*/
28+
public function getDescription(): string
29+
{
30+
$description = '';
31+
foreach ($this->validators as $key => $rule) {
32+
$description .= ++$key . '. ' . $rule->getDescription() . " \n";
33+
}
34+
35+
return $description;
36+
}
37+
38+
/**
39+
* Is valid
40+
*
41+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
42+
*
43+
* @param mixed $value
44+
* @return bool
45+
*/
46+
public function isValid(mixed $value): bool
47+
{
48+
foreach ($this->validators as $rule) {
49+
$valid = $rule->isValid($value);
50+
51+
if($valid) {
52+
return true;
53+
}
54+
}
55+
56+
return false;
57+
}
58+
59+
/**
60+
* Get Type
61+
*
62+
* Returns validator type.
63+
*
64+
* @return string
65+
*/
66+
public function getType(): string
67+
{
68+
return $this->type;
69+
}
70+
71+
/**
72+
* Is array
73+
*
74+
* Function will return true if object is array.
75+
*
76+
* @return bool
77+
*/
78+
public function isArray(): bool
79+
{
80+
return true;
81+
}
82+
}

src/Http/Validator/Multiple.php

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

src/Http/Validator/NoneOf.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
/**
15+
* @param array<Validator> $validators
16+
*/
17+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
18+
{
19+
}
20+
21+
/**
22+
* Get Description
23+
*
24+
* Returns validator description
25+
*
26+
* @return string
27+
*/
28+
public function getDescription(): string
29+
{
30+
$description = '';
31+
foreach ($this->validators as $key => $rule) {
32+
$description .= ++$key . '. ' . $rule->getDescription() . " \n";
33+
}
34+
35+
return $description;
36+
}
37+
38+
/**
39+
* Is valid
40+
*
41+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
42+
*
43+
* @param mixed $value
44+
* @return bool
45+
*/
46+
public function isValid(mixed $value): bool
47+
{
48+
foreach ($this->validators as $rule) {
49+
$valid = $rule->isValid($value);
50+
51+
if($valid) {
52+
return false;
53+
}
54+
}
55+
56+
return true;
57+
}
58+
59+
/**
60+
* Get Type
61+
*
62+
* Returns validator type.
63+
*
64+
* @return string
65+
*/
66+
public function getType(): string
67+
{
68+
return $this->type;
69+
}
70+
71+
/**
72+
* Is array
73+
*
74+
* Function will return true if object is array.
75+
*
76+
* @return bool
77+
*/
78+
public function isArray(): bool
79+
{
80+
return true;
81+
}
82+
}

0 commit comments

Comments
 (0)