FileBag.php in Zircon Profile 8.0
File
vendor/symfony/http-foundation/FileBag.php
View source
<?php
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileBag extends ParameterBag {
private static $fileKeys = array(
'error',
'name',
'size',
'tmp_name',
'type',
);
public function __construct(array $parameters = array()) {
$this
->replace($parameters);
}
public function replace(array $files = array()) {
$this->parameters = array();
$this
->add($files);
}
public function set($key, $value) {
if (!is_array($value) && !$value instanceof UploadedFile) {
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
}
parent::set($key, $this
->convertFileInformation($value));
}
public function add(array $files = array()) {
foreach ($files as $key => $file) {
$this
->set($key, $file);
}
}
protected function convertFileInformation($file) {
if ($file instanceof UploadedFile) {
return $file;
}
$file = $this
->fixPhpFilesArray($file);
if (is_array($file)) {
$keys = array_keys($file);
sort($keys);
if ($keys == self::$fileKeys) {
if (UPLOAD_ERR_NO_FILE == $file['error']) {
$file = null;
}
else {
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
}
}
else {
$file = array_map(array(
$this,
'convertFileInformation',
), $file);
}
}
return $file;
}
protected function fixPhpFilesArray($data) {
if (!is_array($data)) {
return $data;
}
$keys = array_keys($data);
sort($keys);
if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
return $data;
}
$files = $data;
foreach (self::$fileKeys as $k) {
unset($files[$k]);
}
foreach ($data['name'] as $key => $name) {
$files[$key] = $this
->fixPhpFilesArray(array(
'error' => $data['error'][$key],
'name' => $name,
'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key],
));
}
return $files;
}
}
Classes
Name |
Description |
FileBag |
FileBag is a container for uploaded files. |