Skip to content

Commit

Permalink
extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
SjorsO committed Dec 18, 2019
1 parent a273871 commit 30cc4bb
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Expand Up @@ -1396,13 +1396,7 @@ public function validateRequiredIf($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'required_if');

$other = Arr::get($this->data, $parameters[0]);

$values = array_slice($parameters, 1);

if (is_bool($other)) {
$values = $this->convertValuesToBoolean($values);
}
[$values, $other] = $this->prepareValuesAndOther($parameters);

if (in_array($other, $values)) {
return $this->validateRequired($attribute, $value);
Expand All @@ -1423,19 +1417,13 @@ public function validateExcludeIf($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'exclude_if');

$other = Arr::get($this->data, $parameters[0]);

$values = array_slice($parameters, 1);

if (is_bool($other)) {
$values = $this->convertValuesToBoolean($values);
}
[$values, $other] = $this->prepareValuesAndOther($parameters);

return ! in_array($other, $values);
}

/**
* Indicate that an attribute should be excluded when another attribute does not have given value.
* Indicate that an attribute should be excluded when another attribute does not have a given value.
*
* @param string $attribute
* @param mixed $value
Expand All @@ -1446,6 +1434,19 @@ public function validateExcludeUnless($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'exclude_unless');

[$values, $other] = $this->prepareValuesAndOther($parameters);

return in_array($other, $values);
}

/**
* Prepare the values and the other value for validation.
*
* @param array $parameters
* @return array
*/
protected function prepareValuesAndOther($parameters)
{
$other = Arr::get($this->data, $parameters[0]);

$values = array_slice($parameters, 1);
Expand All @@ -1454,7 +1455,7 @@ public function validateExcludeUnless($attribute, $value, $parameters)
$values = $this->convertValuesToBoolean($values);
}

return in_array($other, $values);
return [$values, $other];
}

/**
Expand Down

0 comments on commit 30cc4bb

Please sign in to comment.