Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Do not hydrate files on Validation #16105

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 9 additions & 78 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ class Validator implements ValidatorContract
*/
protected $data;

/**
* The files under validation.
*
* @var array
*/
protected $files = [];

/**
* The initial rules provided.
*
Expand Down Expand Up @@ -204,7 +197,7 @@ public function __construct(TranslatorInterface $translator, array $data, array
$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 @@ -234,38 +227,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 @@ -342,9 +303,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 @@ -615,11 +574,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 @@ -665,8 +620,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 @@ -838,7 +792,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 @@ -850,7 +804,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 @@ -1026,7 +980,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 @@ -2192,7 +2146,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 @@ -2662,7 +2616,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 @@ -3098,29 +3052,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