Revert "package update 재도전"

This reverts commit 595ab6d7e5.
This commit is contained in:
2020-01-28 00:31:53 +09:00
parent 595ab6d7e5
commit b726c33f3d
419 changed files with 16269 additions and 14006 deletions
+148 -361
View File
@@ -1,5 +1,4 @@
<?php
namespace Valitron;
/**
@@ -86,10 +85,10 @@ class Validator
/**
* Setup validation
*
* @param array $data
* @param array $fields
* @param string $lang
* @param string $langDir
* @param array $data
* @param array $fields
* @param string $lang
* @param string $langDir
* @throws \InvalidArgumentException
*/
public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null)
@@ -106,7 +105,7 @@ class Validator
// Load language file in directory
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
if (stream_resolve_include_path($langFile)) {
if (stream_resolve_include_path($langFile) ) {
$langMessages = include $langFile;
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
} else {
@@ -148,13 +147,13 @@ class Validator
* Required field validator
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateRequired($field, $value, $params = array())
protected function validateRequired($field, $value, $params= array())
{
if (isset($params[0]) && (bool)$params[0]) {
if (isset($params[0]) && (bool) $params[0]){
$find = $this->getPart($this->_fields, explode('.', $field), true);
return $find[1];
}
@@ -174,6 +173,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateEquals($field, $value, array $params)
@@ -189,6 +189,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateDifferent($field, $value, array $params)
@@ -204,7 +205,7 @@ class Validator
* This validation rule implies the field is "required"
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateAccepted($field, $value)
@@ -218,7 +219,7 @@ class Validator
* Validate that a field is an array
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateArray($field, $value)
@@ -230,7 +231,7 @@ class Validator
* Validate that a field is numeric
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateNumeric($field, $value)
@@ -242,13 +243,13 @@ class Validator
* Validate that a field is an integer
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateInteger($field, $value, $params)
{
if (isset($params[0]) && (bool)$params[0]) {
if (isset($params[0]) && (bool) $params[0]){
//strict mode
return preg_match('/^([0-9]|-[1-9]|-?[1-9][0-9]*)$/i', $value);
}
@@ -262,6 +263,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateLength($field, $value, $params)
@@ -281,7 +283,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @return bool
* @return boolean
*/
protected function validateLengthBetween($field, $value, $params)
{
@@ -294,10 +296,10 @@ class Validator
* Validate the length of a string (min)
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
*
* @return bool
* @return boolean
*/
protected function validateLengthMin($field, $value, $params)
{
@@ -310,10 +312,10 @@ class Validator
* Validate the length of a string (max)
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
*
* @return bool
* @return boolean
*/
protected function validateLengthMax($field, $value, $params)
{
@@ -345,6 +347,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateMin($field, $value, $params)
@@ -364,6 +367,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateMax($field, $value, $params)
@@ -381,8 +385,9 @@ class Validator
* Validate the size of a field is between min and max values
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateBetween($field, $value, $params)
@@ -405,6 +410,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateIn($field, $value, $params)
@@ -422,35 +428,13 @@ class Validator
return in_array($value, $params[0], $strict);
}
/**
* Validate a field is contained within a list of values
*
* @param string $field
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateListContains($field, $value, $params)
{
$isAssoc = array_values($value) !== $value;
if ($isAssoc) {
$value = array_keys($value);
}
$strict = false;
if (isset($params[1])) {
$strict = $params[1];
}
return in_array($params[0], $value, $strict);
}
/**
* Validate a field is not contained within a list of values
*
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateNotIn($field, $value, $params)
@@ -462,7 +446,7 @@ class Validator
* Validate a field contains a given string
*
* @param string $field
* @param string $value
* @param mixed $value
* @param array $params
* @return bool
*/
@@ -477,9 +461,10 @@ class Validator
$strict = true;
if (isset($params[1])) {
$strict = (bool)$params[1];
$strict = (bool) $params[1];
}
$isContains = false;
if ($strict) {
if (function_exists('mb_strpos')) {
$isContains = mb_strpos($value, $params[0]) !== false;
@@ -496,51 +481,11 @@ class Validator
return $isContains;
}
/**
* Validate that all field values contains a given array
*
* @param string $field
* @param array $value
* @param array $params
* @return bool
*/
protected function validateSubset($field, $value, $params)
{
if (!isset($params[0])) {
return false;
}
if (!is_array($params[0])) {
$params[0] = array($params[0]);
}
if (is_scalar($value) || is_null($value)) {
return $this->validateIn($field, $value, $params);
}
$intersect = array_intersect($value, $params[0]);
return array_diff($value, $intersect) === array_diff($intersect, $value);
}
/**
* Validate that field array has only unique values
*
* @param string $field
* @param array $value
* @return bool
*/
protected function validateContainsUnique($field, $value)
{
if (!is_array($value)) {
return false;
}
return $value === array_unique($value, SORT_REGULAR);
}
/**
* Validate that a field is a valid IP address
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateIp($field, $value)
@@ -548,35 +493,11 @@ class Validator
return filter_var($value, \FILTER_VALIDATE_IP) !== false;
}
/**
* Validate that a field is a valid IP v4 address
*
* @param string $field
* @param mixed $value
* @return bool
*/
protected function validateIpv4($field, $value)
{
return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false;
}
/**
* Validate that a field is a valid IP v6 address
*
* @param string $field
* @param mixed $value
* @return bool
*/
protected function validateIpv6($field, $value)
{
return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false;
}
/**
* Validate that a field is a valid e-mail address
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateEmail($field, $value)
@@ -584,29 +505,11 @@ class Validator
return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false;
}
/**
* Validate that a field contains only ASCII characters
*
* @param $field
* @param $value
* @return bool|false|string
*/
protected function validateAscii($field, $value)
{
// multibyte extension needed
if (function_exists('mb_detect_encoding')) {
return mb_detect_encoding($value, 'ASCII', true);
}
// fallback with regex
return 0 === preg_match('/[^\x00-\x7F]/', $value);
}
/**
* Validate that a field is a valid e-mail address and the domain name is active
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateEmailDNS($field, $value)
@@ -626,7 +529,7 @@ class Validator
* Validate that a field is a valid URL by syntax
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateUrl($field, $value)
@@ -644,7 +547,7 @@ class Validator
* Validate that a field is an active URL by verifying DNS record
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateUrlActive($field, $value)
@@ -664,7 +567,7 @@ class Validator
* Validate that a field contains only alphabetic characters
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateAlpha($field, $value)
@@ -676,7 +579,7 @@ class Validator
* Validate that a field contains only alpha-numeric characters
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateAlphaNum($field, $value)
@@ -688,12 +591,12 @@ class Validator
* Validate that a field contains only alpha-numeric characters, dashes, and underscores
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateSlug($field, $value)
{
if (is_array($value)) {
if(is_array($value)) {
return false;
}
return preg_match('/^([-a-z0-9_-])+$/i', $value);
@@ -703,8 +606,8 @@ class Validator
* Validate that a field passes a regular expression check
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateRegex($field, $value, $params)
@@ -716,7 +619,7 @@ class Validator
* Validate that a field is a valid date
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateDate($field, $value)
@@ -737,6 +640,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateDateFormat($field, $value, $params)
@@ -752,6 +656,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateDateBefore($field, $value, $params)
@@ -768,6 +673,7 @@ class Validator
* @param string $field
* @param mixed $value
* @param array $params
* @internal param array $fields
* @return bool
*/
protected function validateDateAfter($field, $value, $params)
@@ -782,7 +688,7 @@ class Validator
* Validate that a field contains a boolean.
*
* @param string $field
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function validateBoolean($field, $value)
@@ -795,8 +701,8 @@ class Validator
* optionally filtered by an array
*
* @param string $field
* @param mixed $value
* @param array $params
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateCreditCard($field, $value, $params)
@@ -812,7 +718,7 @@ class Validator
if (is_array($params[0])) {
$cards = $params[0];
} elseif (is_string($params[0])) {
$cardType = $params[0];
$cardType = $params[0];
if (isset($params[1]) && is_array($params[1])) {
$cards = $params[1];
if (!in_array($cardType, $cards)) {
@@ -835,7 +741,7 @@ class Validator
return false;
}
for ($i = 0; $i < $strlen; $i++) {
$digit = (int)substr($number, $strlen - $i - 1, 1);
$digit = (int) substr($number, $strlen - $i - 1, 1);
if ($i % 2 == 1) {
$sub_total = $digit * 2;
if ($sub_total > 9) {
@@ -847,7 +753,7 @@ class Validator
$sum += $sub_total;
}
if ($sum > 0 && $sum % 10 == 0) {
return true;
return true;
}
return false;
@@ -858,11 +764,11 @@ class Validator
return true;
} else {
$cardRegex = array(
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
'amex' => '#^3[47][0-9]{13}$#',
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
'amex' => '#^3[47][0-9]{13}$#',
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
);
if (isset($cardType)) {
@@ -920,127 +826,14 @@ class Validator
return $isInstanceOf;
}
/**
* Validates whether or not a field is required based on whether or not other fields are present.
*
* @param string $field name of the field in the data array
* @param mixed $value value of this field
* @param array $params parameters for this rule
* @param array $fields full list of data to be validated
* @return bool
*/
protected function validateRequiredWith($field, $value, $params, $fields)
{
$conditionallyReq = false;
// if we actually have conditionally required with fields to check against
if (isset($params[0])) {
// convert single value to array if it isn't already
$reqParams = is_array($params[0]) ? $params[0] : array($params[0]);
// check for the flag indicating if all fields are required
$allRequired = isset($params[1]) && (bool)$params[1];
$emptyFields = 0;
foreach ($reqParams as $requiredField) {
// check the field is set, not null, and not the empty string
if (isset($fields[$requiredField]) && !is_null($fields[$requiredField])
&& (is_string($fields[$requiredField]) ? trim($fields[$requiredField]) !== '' : true)) {
if (!$allRequired) {
$conditionallyReq = true;
break;
} else {
$emptyFields++;
}
}
}
// if all required fields are present in strict mode, we're requiring it
if ($allRequired && $emptyFields === count($reqParams)) {
$conditionallyReq = true;
}
}
// if we have conditionally required fields
if ($conditionallyReq && (is_null($value) ||
is_string($value) && trim($value) === '')) {
return false;
}
return true;
}
/**
* Validates whether or not a field is required based on whether or not other fields are present.
*
* @param string $field name of the field in the data array
* @param mixed $value value of this field
* @param array $params parameters for this rule
* @param array $fields full list of data to be validated
* @return bool
*/
protected function validateRequiredWithout($field, $value, $params, $fields)
{
$conditionallyReq = false;
// if we actually have conditionally required with fields to check against
if (isset($params[0])) {
// convert single value to array if it isn't already
$reqParams = is_array($params[0]) ? $params[0] : array($params[0]);
// check for the flag indicating if all fields are required
$allEmpty = isset($params[1]) && (bool)$params[1];
$filledFields = 0;
foreach ($reqParams as $requiredField) {
// check the field is NOT set, null, or the empty string, in which case we are requiring this value be present
if (!isset($fields[$requiredField]) || (is_null($fields[$requiredField])
|| (is_string($fields[$requiredField]) && trim($fields[$requiredField]) === ''))) {
if (!$allEmpty) {
$conditionallyReq = true;
break;
} else {
$filledFields++;
}
}
}
// if all fields were empty, then we're requiring this in strict mode
if ($allEmpty && $filledFields === count($reqParams)) {
$conditionallyReq = true;
}
}
// if we have conditionally required fields
if ($conditionallyReq && (is_null($value) ||
is_string($value) && trim($value) === '')) {
return false;
}
return true;
}
/**
* Validate optional field
*
* @param $field
* @param $value
* @param $params
* @return bool
*/
protected function validateOptional($field, $value, $params)
{
//Validate optional field
protected function validateOptional($field, $value, $params) {
//Always return true
return true;
}
protected function validateArrayHasKeys($field, $value, $params)
{
if (!is_array($value) || !isset($params[0])) {
return false;
}
$requiredFields = $params[0];
if (count($requiredFields) === 0) {
return false;
}
foreach ($requiredFields as $fieldName) {
if (!array_key_exists($fieldName, $value)) {
return false;
}
}
return true;
}
/**
* Get array of fields and data
* Get array of fields and data
*
* @return array
*/
@@ -1068,12 +861,12 @@ class Validator
* Add an error to error messages array
*
* @param string $field
* @param string $message
* @param string $msg
* @param array $params
*/
public function error($field, $message, array $params = array())
public function error($field, $msg, array $params = array())
{
$message = $this->checkAndSetLabel($field, $message, $params);
$msg = $this->checkAndSetLabel($field, $msg, $params);
$values = array();
// Printed values need to be in string format
@@ -1097,18 +890,18 @@ class Validator
$values[] = $param;
}
$this->_errors[$field][] = vsprintf($message, $values);
$this->_errors[$field][] = vsprintf($msg, $values);
}
/**
* Specify validation message to use for error for the last validation rule
*
* @param string $message
* @return Validator
* @param string $msg
* @return $this
*/
public function message($message)
public function message($msg)
{
$this->_validations[count($this->_validations) - 1]['message'] = $message;
$this->_validations[count($this->_validations) - 1]['message'] = $msg;
return $this;
}
@@ -1132,7 +925,7 @@ class Validator
}
// Catches the case where the data isn't an array or object
if (is_scalar($data)) {
return array(null, false);
return array(NULL, false);
}
$identifier = array_shift($identifiers);
// Glob match
@@ -1147,21 +940,24 @@ class Validator
}
}
return array($values, true);
} // Dead end, abort
elseif ($identifier === null || ! isset($data[$identifier])) {
}
// Dead end, abort
elseif ($identifier === NULL || ! isset($data[$identifier])) {
if ($allow_empty){
//when empty values are allowed, we only care if the key exists
return array(null, array_key_exists($identifier, $data));
}
return array(null, false);
} // Match array element
}
// Match array element
elseif (count($identifiers) === 0) {
if ($allow_empty) {
if ($allow_empty){
//when empty values are allowed, we only care if the key exists
return array(null, array_key_exists($identifier, $data));
}
return array($data[$identifier], $allow_empty);
} // We need to go deeper
return array($data[$identifier], false);
}
// We need to go deeper
else {
return $this->getPart($data[$identifier], $identifiers, $allow_empty);
}
@@ -1170,23 +966,22 @@ class Validator
/**
* Run validations and return boolean result
*
* @return bool
* @return boolean
*/
public function validate()
{
$set_to_break = false;
$set_to_break = false;
foreach ($this->_validations as $v) {
foreach ($v['fields'] as $field) {
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field), false);
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field));
// Don't validate if the field is not required and the value is empty and we don't have a conditionally required rule present on the field
if (($this->hasRule('optional', $field) && isset($values))
|| ($this->hasRule('requiredWith', $field) || $this->hasRule('requiredWithout', $field))) {
// Don't validate if the field is not required and the value is empty
if ($this->hasRule('optional', $field) && isset($values)) {
//Continue with execution below if statement
} elseif (
$v['rule'] !== 'required' && !$this->hasRule('required', $field) &&
$v['rule'] !== 'accepted' &&
(!isset($values) || $values === '' || ($multiple && count($values) == 0))
(! isset($values) || $values === '' || ($multiple && count($values) == 0))
) {
continue;
}
@@ -1201,8 +996,6 @@ class Validator
if (!$multiple) {
$values = array($values);
} else if (! $this->hasRule('required', $field)){
$values = array_filter($values);
}
$result = true;
@@ -1212,15 +1005,13 @@ class Validator
if (!$result) {
$this->error($field, $v['message'], $v['params']);
if ($this->stop_on_first_fail) {
$set_to_break = true;
break;
if($this->stop_on_first_fail) {
$set_to_break = true;
break;
}
}
}
if ($set_to_break) {
break;
}
if($set_to_break) break;
}
return count($this->errors()) === 0;
@@ -1230,9 +1021,8 @@ class Validator
* Should the validation stop a rule is failed
* @param bool $stop
*/
public function stopOnFirstFail($stop = true)
{
$this->stop_on_first_fail = (bool)$stop;
public function stopOnFirstFail($stop = true) {
$this->stop_on_first_fail = (bool) $stop;
}
/**
@@ -1260,8 +1050,9 @@ class Validator
*
* @param string $name The name of the rule
* @param string $field The name of the field
* @return bool
* @return boolean
*/
protected function hasRule($name, $field)
{
foreach ($this->_validations as $validation) {
@@ -1278,19 +1069,18 @@ class Validator
protected static function assertRuleCallback($callback)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException(
'Second argument must be a valid callback. Given argument was not callable.'
);
throw new \InvalidArgumentException('Second argument must be a valid callback. Given argument was not callable.');
}
}
/**
* Adds a new validation rule callback that is tied to the current
* instance only.
*
* @param string $name
* @param callable $callback
* @param string $message
* @param string $name
* @param mixed $callback
* @param string $message
* @throws \InvalidArgumentException
*/
public function addInstanceRule($name, $callback, $message = null)
@@ -1304,14 +1094,15 @@ class Validator
/**
* Register new validation rule callback
*
* @param string $name
* @param callable $callback
* @param string $message
* @param string $name
* @param mixed $callback
* @param string $message
* @throws \InvalidArgumentException
*/
public static function addRule($name, $callback, $message = null)
{
if ($message === null) {
if ($message === null)
{
$message = static::ERROR_DEFAULT;
}
@@ -1321,20 +1112,18 @@ class Validator
static::$_ruleMessages[$name] = $message;
}
/**
* @param mixed $fields
* @return string
*/
public function getUniqueRuleName($fields)
{
if (is_array($fields)) {
if (is_array($fields))
{
$fields = implode("_", $fields);
}
$orgName = "{$fields}_rule";
$name = $orgName;
$rules = $this->getRules();
while (isset($rules[$name])) {
while (isset($rules[$name]))
{
$name = $orgName . "_" . rand(0, 10000);
}
@@ -1345,7 +1134,7 @@ class Validator
* Returns true if either a validator with the given name has been
* registered or there is a default validator by that name.
*
* @param string $name
* @param string $name
* @return bool
*/
public function hasValidator($name)
@@ -1358,9 +1147,9 @@ class Validator
/**
* Convenience method to add a single validation rule
*
* @param string|callable $rule
* @param array|string $fields
* @return Validator
* @param string|callback $rule
* @param array|string $fields
* @return $this
* @throws \InvalidArgumentException
*/
public function rule($rule, $fields)
@@ -1369,10 +1158,11 @@ class Validator
$params = array_slice(func_get_args(), 2);
if (is_callable($rule)
&& !(is_string($rule) && $this->hasValidator($rule))) {
&& !(is_string($rule) && $this->hasValidator($rule)))
{
$name = $this->getUniqueRuleName($fields);
$message = isset($params[0]) ? $params[0] : null;
$this->addInstanceRule($name, $rule, $message);
$msg = isset($params[0]) ? $params[0] : null;
$this->addInstanceRule($name, $rule, $msg);
$rule = $name;
}
@@ -1380,15 +1170,13 @@ class Validator
if (!isset($errors[$rule])) {
$ruleMethod = 'validate' . ucfirst($rule);
if (!method_exists($this, $ruleMethod)) {
throw new \InvalidArgumentException(
"Rule '" . $rule . "' has not been registered with " . get_called_class() . "::addRule()."
);
throw new \InvalidArgumentException("Rule '" . $rule . "' has not been registered with " . __CLASS__ . "::addRule().");
}
}
// Ensure rule has an accompanying message
$messages = $this->getRuleMessages();
$message = isset($messages[$rule]) ? $messages[$rule] : self::ERROR_DEFAULT;
$msgs = $this->getRuleMessages();
$message = isset($msgs[$rule]) ? $msgs[$rule] : self::ERROR_DEFAULT;
// Ensure message contains field label
if (function_exists('mb_strpos')) {
@@ -1402,8 +1190,8 @@ class Validator
$this->_validations[] = array(
'rule' => $rule,
'fields' => (array)$fields,
'params' => (array)$params,
'fields' => (array) $fields,
'params' => (array) $params,
'message' => $message
);
@@ -1414,7 +1202,8 @@ class Validator
* Add label to rule
*
* @param string $value
* @return Validator
* @internal param array $labels
* @return $this
*/
public function label($value)
{
@@ -1428,7 +1217,7 @@ class Validator
* Add labels to rules
*
* @param array $labels
* @return Validator
* @return $this
*/
public function labels($labels = array())
{
@@ -1439,29 +1228,29 @@ class Validator
/**
* @param string $field
* @param string $message
* @param string $msg
* @param array $params
* @return array
*/
protected function checkAndSetLabel($field, $message, $params)
protected function checkAndSetLabel($field, $msg, $params)
{
if (isset($this->_labels[$field])) {
$message = str_replace('{field}', $this->_labels[$field], $message);
$msg = str_replace('{field}', $this->_labels[$field], $msg);
if (is_array($params)) {
$i = 1;
foreach ($params as $k => $v) {
$tag = '{field' . $i . '}';
$tag = '{field'. $i .'}';
$label = isset($params[$k]) && (is_numeric($params[$k]) || is_string($params[$k])) && isset($this->_labels[$params[$k]]) ? $this->_labels[$params[$k]] : $tag;
$message = str_replace($tag, $label, $message);
$msg = str_replace($tag, $label, $msg);
$i++;
}
}
} else {
$message = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message);
$msg = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $msg);
}
return $message;
return $msg;
}
/**
@@ -1474,8 +1263,8 @@ class Validator
foreach ($rules as $ruleType => $params) {
if (is_array($params)) {
foreach ($params as $innerParams) {
if (!is_array($innerParams)) {
$innerParams = (array)$innerParams;
if (! is_array($innerParams)){
$innerParams = (array) $innerParams;
}
array_unshift($innerParams, $ruleType);
call_user_func_array(array($this, 'rule'), $innerParams);
@@ -1491,7 +1280,7 @@ class Validator
*
* @param array $data
* @param array $fields
* @return Validator
* @return \Valitron\Validator
*/
public function withData($data, $fields = array())
{
@@ -1504,33 +1293,32 @@ class Validator
/**
* Convenience method to add validation rule(s) by field
*
* @param string $field
* @param array $rules
* @param string field_name
* @param array $rules
*/
public function mapFieldRules($field, $rules)
{
public function mapFieldRules($field_name, $rules){
$me = $this;
array_map(function ($rule) use ($field, $me) {
array_map(function($rule) use($field_name, $me){
//rule must be an array
$rule = (array)$rule;
//First element is the name of the rule
$ruleName = array_shift($rule);
$rule_name = array_shift($rule);
//find a custom message, if any
$message = null;
if (isset($rule['message'])) {
if (isset($rule['message'])){
$message = $rule['message'];
unset($rule['message']);
}
//Add the field and additional parameters to the rule
$added = call_user_func_array(array($me, 'rule'), array_merge(array($ruleName, $field), $rule));
if (!empty($message)) {
$added = call_user_func_array(array($me, 'rule'), array_merge(array($rule_name, $field_name), $rule));
if (! empty($message)){
$added->message($message);
}
}, (array)$rules);
}, (array) $rules);
}
/**
@@ -1538,11 +1326,10 @@ class Validator
*
* @param array $rules
*/
public function mapFieldsRules($rules)
{
public function mapFieldsRules($rules){
$me = $this;
array_map(function ($field) use ($rules, $me) {
$me->mapFieldRules($field, $rules[$field]);
array_map(function($field_name) use($rules, $me){
$me->mapFieldRules($field_name, $rules[$field_name]);
}, array_keys($rules));
}
}