Skip to content

Commit

Permalink
Do not hydrate files in Validator (#16017)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Oct 24, 2016
1 parent 2ac250b commit 232466f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 169 deletions.
80 changes: 9 additions & 71 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function __construct(Translator $translator, array $data, array $rules, a
$this->translator = $translator;
$this->customMessages = $messages;
$this->customAttributes = $customAttributes;
$this->data = $this->hydrateFiles($this->parseData($data));
$this->data = $this->parseData($data);

$this->setRules($rules);
}
Expand Down Expand Up @@ -233,38 +233,6 @@ public function parseData(array $data)
return $newData;
}

/**
* Hydrate the files array.
*
* @param array $data
* @param string $arrayKey
* @return array
*/
protected function hydrateFiles(array $data, $arrayKey = null)
{
if (is_null($arrayKey)) {
$this->files = [];
}

foreach ($data as $key => $value) {
$newKey = $arrayKey ? "$arrayKey.$key" : $key;

// If this value is an instance of the HttpFoundation File class we will
// remove it from the data array and add it to the files array, which
// we use to conveniently separate out these files from other data.
if ($value instanceof File) {
$this->files[$newKey] = $value;

unset($data[$key]);
} elseif (is_array($value) && ! empty($value) &&
empty($this->hydrateFiles($value, $newKey))) {
unset($data[$key]);
}
}

return $data;
}

/**
* Explode the rules into an array of rules.
*
Expand Down Expand Up @@ -341,9 +309,7 @@ public function sometimes($attribute, $rules, callable $callback)
*/
public function each($attribute, $rules)
{
$data = array_merge(
Arr::dot($this->initializeAttributeOnData($attribute)), $this->files
);
$data = Arr::dot($this->initializeAttributeOnData($attribute));

$pattern = str_replace('\*', '[^\.]+', preg_quote($attribute));

Expand Down Expand Up @@ -614,11 +580,7 @@ protected function attributesThatHaveMessages()
*/
protected function getValue($attribute)
{
if (! is_null($value = Arr::get($this->data, $attribute))) {
return $value;
} elseif (! is_null($value = Arr::get($this->files, $attribute))) {
return $value;
}
return Arr::get($this->data, $attribute);
}

/**
Expand Down Expand Up @@ -664,8 +626,7 @@ protected function passesOptionalCheck($attribute)
{
if ($this->hasRule($attribute, ['Sometimes'])) {
return array_key_exists($attribute, Arr::dot($this->data))
|| in_array($attribute, array_keys($this->data))
|| array_key_exists($attribute, $this->files);
|| in_array($attribute, array_keys($this->data));
}

return true;
Expand Down Expand Up @@ -837,7 +798,7 @@ protected function validateRequired($attribute, $value)
*/
protected function validatePresent($attribute, $value)
{
return Arr::has(array_merge($this->data, $this->files), $attribute);
return Arr::has($this->data, $attribute);
}

/**
Expand All @@ -849,7 +810,7 @@ protected function validatePresent($attribute, $value)
*/
protected function validateFilled($attribute, $value)
{
if (Arr::has(array_merge($this->data, $this->files), $attribute)) {
if (Arr::has($this->data, $attribute)) {
return $this->validateRequired($attribute, $value);
}

Expand Down Expand Up @@ -1025,7 +986,7 @@ protected function getPresentCount($attributes)
$count = 0;

foreach ($attributes as $key) {
if (Arr::get($this->data, $key) || Arr::get($this->files, $key)) {
if (Arr::get($this->data, $key)) {
$count++;
}
}
Expand Down Expand Up @@ -2187,7 +2148,7 @@ protected function getAttributeType($attribute)
return 'numeric';
} elseif ($this->hasRule($attribute, ['Array'])) {
return 'array';
} elseif (array_key_exists($attribute, $this->files)) {
} elseif ($this->getValue($attribute) instanceof UploadedFile) {
return 'file';
}

Expand Down Expand Up @@ -2657,7 +2618,7 @@ protected function replaceAfter($message, $attribute, $rule, $parameters)
*/
public function attributes()
{
return array_merge($this->data, $this->files);
return $this->data;
}

/**
Expand Down Expand Up @@ -3093,29 +3054,6 @@ public function setValueNames(array $values)
return $this;
}

/**
* Get the files under validation.
*
* @return array
*/
public function getFiles()
{
return $this->files;
}

/**
* Set the files under validation.
*
* @param array $files
* @return $this
*/
public function setFiles(array $files)
{
$this->files = $files;

return $this;
}

/**
* Get the Presence Verifier implementation.
*
Expand Down
Loading

0 comments on commit 232466f

Please sign in to comment.