Open Journal Systems  3.3.0
vendor/symfony/http-foundation/FileBag.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
15 
22 class FileBag extends ParameterBag
23 {
24  private static $fileKeys = ['error', 'name', 'size', 'tmp_name', 'type'];
25 
29  public function __construct(array $parameters = [])
30  {
31  $this->replace($parameters);
32  }
33 
37  public function replace(array $files = [])
38  {
39  $this->parameters = [];
40  $this->add($files);
41  }
42 
46  public function set($key, $value)
47  {
48  if (!\is_array($value) && !$value instanceof UploadedFile) {
49  throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
50  }
51 
52  parent::set($key, $this->convertFileInformation($value));
53  }
54 
58  public function add(array $files = [])
59  {
60  foreach ($files as $key => $file) {
61  $this->set($key, $file);
62  }
63  }
64 
72  protected function convertFileInformation($file)
73  {
74  if ($file instanceof UploadedFile) {
75  return $file;
76  }
77 
78  if (\is_array($file)) {
79  $file = $this->fixPhpFilesArray($file);
80  $keys = array_keys($file);
81  sort($keys);
82 
83  if ($keys == self::$fileKeys) {
84  if (UPLOAD_ERR_NO_FILE == $file['error']) {
85  $file = null;
86  } else {
87  $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
88  }
89  } else {
90  $file = array_map([$this, 'convertFileInformation'], $file);
91  if (array_keys($keys) === $keys) {
92  $file = array_filter($file);
93  }
94  }
95  }
96 
97  return $file;
98  }
99 
116  protected function fixPhpFilesArray($data)
117  {
118  $keys = array_keys($data);
119  sort($keys);
120 
121  if (self::$fileKeys != $keys || !isset($data['name']) || !\is_array($data['name'])) {
122  return $data;
123  }
124 
125  $files = $data;
126  foreach (self::$fileKeys as $k) {
127  unset($files[$k]);
128  }
129 
130  foreach ($data['name'] as $key => $name) {
131  $files[$key] = $this->fixPhpFilesArray([
132  'error' => $data['error'][$key],
133  'name' => $name,
134  'type' => $data['type'][$key],
135  'tmp_name' => $data['tmp_name'][$key],
136  'size' => $data['size'][$key],
137  ]);
138  }
139 
140  return $files;
141  }
142 }
Symfony\Component\HttpFoundation\FileBag\__construct
__construct(array $parameters=[])
Definition: vendor/symfony/http-foundation/FileBag.php:29
Symfony\Component\HttpFoundation\FileBag\replace
replace(array $files=[])
Definition: vendor/symfony/http-foundation/FileBag.php:37
Symfony\Component\HttpFoundation\ParameterBag\$parameters
$parameters
Definition: lib/vendor/symfony/http-foundation/ParameterBag.php:29
Symfony\Component\HttpFoundation\FileBag\replace
replace(array $files=array())
Definition: lib/vendor/symfony/http-foundation/FileBag.php:39
Symfony\Component\HttpFoundation\FileBag\add
add(array $files=[])
Definition: vendor/symfony/http-foundation/FileBag.php:58
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\FileBag\fixPhpFilesArray
fixPhpFilesArray($data)
Definition: lib/vendor/symfony/http-foundation/FileBag.php:115
Symfony\Component\HttpFoundation\FileBag\add
add(array $files=array())
Definition: lib/vendor/symfony/http-foundation/FileBag.php:60
Symfony\Component\HttpFoundation\FileBag\convertFileInformation
convertFileInformation($file)
Definition: lib/vendor/symfony/http-foundation/FileBag.php:74
Symfony\Component\HttpFoundation\File\UploadedFile
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:25