-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from utopia-php/feat-multiple-validator-adapter
Feat: Rules for Multiple validator
- Loading branch information
Showing
7 changed files
with
469 additions
and
295 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Utopia\Http\Validator; | ||
|
||
use Utopia\Http\Validator; | ||
|
||
/** | ||
* Ensure all of the validators from a list passed the check | ||
* | ||
* @package Utopia\Validator | ||
*/ | ||
class AllOf extends Validator | ||
{ | ||
protected ?Validator $failedRule = null; | ||
|
||
/** | ||
* @param array<Validator> $validators | ||
*/ | ||
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED) | ||
{ | ||
} | ||
|
||
/** | ||
* Get Description | ||
* | ||
* Returns validator description | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
if(!(\is_null($this->failedRule))) { | ||
$description = $this->failedRule->getDescription(); | ||
} else { | ||
$description = $this->validators[0]->getDescription(); | ||
} | ||
|
||
return $description; | ||
} | ||
|
||
/** | ||
* Is valid | ||
* | ||
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid(mixed $value): bool | ||
{ | ||
foreach ($this->validators as $rule) { | ||
$valid = $rule->isValid($value); | ||
|
||
if(!$valid) { | ||
$this->failedRule = $rule; | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get Type | ||
* | ||
* Returns validator type. | ||
* | ||
* @return string | ||
*/ | ||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
/** | ||
* Is array | ||
* | ||
* Function will return true if object is array. | ||
* | ||
* @return bool | ||
*/ | ||
public function isArray(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Utopia\Http\Validator; | ||
|
||
use Utopia\Http\Validator; | ||
|
||
/** | ||
* Ensure at least one validator from a list passed the check | ||
* | ||
* @package Utopia\Validator | ||
*/ | ||
class AnyOf extends Validator | ||
{ | ||
protected ?Validator $failedRule = null; | ||
|
||
/** | ||
* @param array<Validator> $validators | ||
*/ | ||
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED) | ||
{ | ||
} | ||
|
||
/** | ||
* Get Description | ||
* | ||
* Returns validator description | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
if(!(\is_null($this->failedRule))) { | ||
$description = $this->failedRule->getDescription(); | ||
} else { | ||
$description = $this->validators[0]->getDescription(); | ||
} | ||
|
||
return $description; | ||
} | ||
|
||
/** | ||
* Is valid | ||
* | ||
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid(mixed $value): bool | ||
{ | ||
foreach ($this->validators as $rule) { | ||
$valid = $rule->isValid($value); | ||
|
||
$this->failedRule = $rule; | ||
|
||
if($valid) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Get Type | ||
* | ||
* Returns validator type. | ||
* | ||
* @return string | ||
*/ | ||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
/** | ||
* Is array | ||
* | ||
* Function will return true if object is array. | ||
* | ||
* @return bool | ||
*/ | ||
public function isArray(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Utopia\Http\Validator; | ||
|
||
use Utopia\Http\Validator; | ||
|
||
/** | ||
* Ensure no validators from a list passed the check | ||
* | ||
* @package Utopia\Validator | ||
*/ | ||
class NoneOf extends Validator | ||
{ | ||
protected ?Validator $failedRule = null; | ||
|
||
/** | ||
* @param array<Validator> $validators | ||
*/ | ||
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED) | ||
{ | ||
} | ||
|
||
/** | ||
* Get Description | ||
* | ||
* Returns validator description | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
$description = ''; | ||
|
||
if(!(\is_null($this->failedRule))) { | ||
$description = $this->failedRule->getDescription(); | ||
} else { | ||
$description = $this->validators[0]->getDescription(); | ||
} | ||
|
||
return $description; | ||
} | ||
|
||
/** | ||
* Is valid | ||
* | ||
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid(mixed $value): bool | ||
{ | ||
foreach ($this->validators as $rule) { | ||
$valid = $rule->isValid($value); | ||
|
||
if($valid) { | ||
$this->failedRule = $rule; | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get Type | ||
* | ||
* Returns validator type. | ||
* | ||
* @return string | ||
*/ | ||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
/** | ||
* Is array | ||
* | ||
* Function will return true if object is array. | ||
* | ||
* @return bool | ||
*/ | ||
public function isArray(): bool | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.