/** * Creates a new Before Date Validator. * * @param \DateTime $date The reference date/time. * @param string $format The format of the date to show in the error message. */ public function __construct(\DateTime $date, $format = DATE_RFC3339) { if (!is_string($format)) { throw new \InvalidArgumentException(Intl\GetText::_d('Flikore.Validator', 'The format argument must be a string.')); } $this->date = $date; $this->addKeyValue('date', $date->format($format)); }
/** * Creates a new Less Than Validator. * @param int $max The exclusive upper boundary. */ public function __construct($max) { if (!is_int($max)) { throw new \InvalidArgumentException(Intl\GetText::_d('Flikore.Validator', 'The limit must be a valid integer')); } $this->max = $max; $this->addKeyValue('max', $max); }
/** * Creates a new Validation Choice. You can pass as many validators as you want. * * @param array|Validator $... The validators to check (list as arguments or in an array). */ public function __construct() { $validators = func_get_args(); if (count($validators) == 1 and is_array($validators[0])) { $validators = $validators[0]; } $i = 1; foreach ($validators as $arg) { if (!$arg instanceof Validator) { throw new \InvalidArgumentException(Intl\GetText::_d('Flikore.Validator', 'The arguments must be intances of validators')); } $this->validators[] = $arg; $this->addKeyValue('v' . $i++, $arg->getErrorMessage()); } }
/** * Creates a new concrete validator rule based on the given array. * Such array is created automatically by ValidationSet. * * @param array $fields The key-value fileds * @return Validator The built validator. */ public function createRule($fields) { $params = $this->args; foreach ($params as $i => $arg) { if ($arg instanceof ValidationKey) { $key = $arg->getKey(); if (!isset($fields[$key])) { throw new \OutOfBoundsException(sprintf(Intl\GetText::_d('Flikore.Validator', 'The "%s" key is missing in the input'), $key)); } $params[$i] = $fields[$key]; } } $ref = new \ReflectionClass($this->validator); $rule = $ref->newInstanceArgs($params); if ($this->message) { $rule->setErrorMessage($this->message); } foreach ($this->values as $key => $value) { $rule->addKeyValue($key, $value); } return $rule; }
/** * Gets the fields from the input. As function to avoid repeat on validate and assert. * * @param string|array $fields The input fields. * @throws \InvalidArgumentException If the fields parameter is not string nor array. */ protected function getFields($fields) { if ($fields === null) { $fields = array_keys($this->validators); } elseif (is_string($fields) || is_int($fields)) { $fields = array($fields); } elseif (!is_array($fields)) { throw new \InvalidArgumentException(sprintf(Intl\GetText::_d('Flikore.Validator', 'The argument "%s" must be either %s, %s or %s.'), 'fields', Intl\GetText::_d('Flikore.Validator', 'a string'), Intl\GetText::_d('Flikore.Validator', 'an array'), Intl\GetText::_d('Flikore.Validator', 'an integer'))); } return $fields; }