Source of file ValidatorChain.php
Size: 6,398 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Validator/ValidatorChain.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Validator; use Bluz\Validator\Exception\ValidatorException; use Bluz\Validator\Rule\RequiredRule; use Bluz\Validator\Rule\RuleInterface; /** * Chain of Validators * * Chain can consists one or more validation rules * * @package Bluz\Validator * @author Anton Shevchuk * * @method ValidatorChain alpha($additionalCharacters = '') * @method ValidatorChain alphaNumeric($additionalCharacters = '') * @method ValidatorChain array($callback) * @method ValidatorChain between($min, $max) * @method ValidatorChain betweenInclusive($min, $max) * @method ValidatorChain condition($condition) * @method ValidatorChain contains($containsValue) * @method ValidatorChain containsStrict($containsValue) * @method ValidatorChain countryCode() * @method ValidatorChain creditCard() * @method ValidatorChain date($format) * @method ValidatorChain domain($checkDns = false) * @method ValidatorChain email($checkDns = false) * @method ValidatorChain equals($compareTo) * @method ValidatorChain equalsStrict($compareTo) * @method ValidatorChain float() * @method ValidatorChain in($haystack) * @method ValidatorChain inStrict($haystack) * @method ValidatorChain integer() * @method ValidatorChain ip($options = null) * @method ValidatorChain json() * @method ValidatorChain latin($additionalCharacters = '') * @method ValidatorChain latinNumeric($additionalCharacters = '') * @method ValidatorChain length($min = null, $max = null) * @method ValidatorChain less($maxValue) * @method ValidatorChain lessOrEqual($maxValue) * @method ValidatorChain more($minValue) * @method ValidatorChain moreOrEqual($minValue) * @method ValidatorChain notEmpty() * @method ValidatorChain noWhitespace() * @method ValidatorChain numeric() * @method ValidatorChain required() * @method ValidatorChain slug() * @method ValidatorChain string() */ class ValidatorChain implements ValidatorInterface { /** * @var string description of rules chain */ protected $description; /** * @var RuleInterface[] list of validation rules */ protected $rules = []; /** * @var string Error message */ protected $error; /** * Magic call for create new rule * * @param string $ruleName * @param array $arguments * * @return ValidatorChain * @throws Exception\ComponentException */ public function __call($ruleName, $arguments) : ValidatorChain { $this->addRule(Validator::rule($ruleName, $arguments)); return $this; } /** * Add Rule to ValidatorChain * * @param RuleInterface $rule * * @return ValidatorChain */ public function addRule($rule) : ValidatorChain { $this->rules[] = $rule; return $this; } /** * Get required flag * * @return bool */ public function isRequired() : bool { foreach ($this->rules as $rule) { if ($rule instanceof RequiredRule) { return true; } } return false; } /** * Add Callback Rule to ValidatorChain * * @param mixed $callable * @param string|null $description * * @return ValidatorChain * @throws \Bluz\Validator\Exception\ComponentException */ public function callback($callable, $description = null) : ValidatorChain { $rule = Validator::rule('callback', [$callable]); if (null !== $description) { $rule->setDescription($description); } $this->addRule($rule); return $this; } /** * Add Regexp Rule to ValidatorChain * * @param string $expression * @param string|null $description * * @return ValidatorChain * @throws \Bluz\Validator\Exception\ComponentException */ public function regexp($expression, $description = null) : ValidatorChain { $rule = Validator::rule('regexp', [$expression]); if (null !== $description) { $rule->setDescription($description); } $this->addRule($rule); return $this; } /** * Validate chain of rules * * @param mixed $input * * @return bool */ public function validate($input) : bool { $this->error = null; // clean foreach ($this->rules as $rule) { if (!$rule->validate($input)) { // apply custom description or use description from rule $this->setError($this->description ?? $rule->getDescription()); return false; } } return true; } /** * Assert * * @param mixed $input * * @throws ValidatorException */ public function assert($input) { if (!$this->validate($input)) { throw new ValidatorException($this->getError()); } } /** * @inheritdoc */ public function __invoke($input) : bool { return $this->validate($input); } /** * @inheritdoc */ public function __toString() : string { return implode("\n", $this->getDescription()); } /** * Get error message * * @return null|string */ public function getError() { return $this->error; } /** * Set error message for replace text from rule * * @param string $message * * @return ValidatorChain */ protected function setError($message) : ValidatorChain { $this->error = $message; return $this; } /** * Get validation description * * @return array */ public function getDescription() : array { // apply custom description if ($this->description) { return [$this->description]; } // eject description from rules return array_map('strval', $this->rules); } /** * Set validation description * * @param string $message * * @return ValidatorChain */ public function setDescription($message) : ValidatorChain { $this->description = $message; return $this; } } |