Open Journal Systems  3.3.0
lib/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 = array('error', 'name', 'size', 'tmp_name', 'type');
25 
31  public function __construct(array $parameters = array())
32  {
33  $this->replace($parameters);
34  }
35 
39  public function replace(array $files = array())
40  {
41  $this->parameters = array();
42  $this->add($files);
43  }
44 
48  public function set($key, $value)
49  {
50  if (!is_array($value) && !$value instanceof UploadedFile) {
51  throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
52  }
53 
54  parent::set($key, $this->convertFileInformation($value));
55  }
56 
60  public function add(array $files = array())
61  {
62  foreach ($files as $key => $file) {
63  $this->set($key, $file);
64  }
65  }
66 
74  protected function convertFileInformation($file)
75  {
76  if ($file instanceof UploadedFile) {
77  return $file;
78  }
79 
80  $file = $this->fixPhpFilesArray($file);
81  if (is_array($file)) {
82  $keys = array_keys($file);
83  sort($keys);
84 
85  if ($keys == self::$fileKeys) {
86  if (UPLOAD_ERR_NO_FILE == $file['error']) {
87  $file = null;
88  } else {
89  $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
90  }
91  } else {
92  $file = array_map(array($this, 'convertFileInformation'), $file);
93  }
94  }
95 
96  return $file;
97  }
98 
115  protected function fixPhpFilesArray($data)
116  {
117  if (!is_array($data)) {
118  return $data;
119  }
120 
121  $keys = array_keys($data);
122  sort($keys);
123 
124  if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
125  return $data;
126  }
127 
128  $files = $data;
129  foreach (self::$fileKeys as $k) {
130  unset($files[$k]);
131  }
132 
133  foreach ($data['name'] as $key => $name) {
134  $files[$key] = $this->fixPhpFilesArray(array(
135  'error' => $data['error'][$key],
136  'name' => $name,
137  'type' => $data['type'][$key],
138  'tmp_name' => $data['tmp_name'][$key],
139  'size' => $data['size'][$key],
140  ));
141  }
142 
143  return $files;
144  }
145 }
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\ParameterBag
Definition: lib/vendor/symfony/http-foundation/ParameterBag.php:19
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\FileBag
Definition: lib/vendor/symfony/http-foundation/FileBag.php:22
Symfony\Component\HttpFoundation\File\UploadedFile
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:25
Symfony\Component\HttpFoundation\FileBag\__construct
__construct(array $parameters=array())
Definition: lib/vendor/symfony/http-foundation/FileBag.php:31