Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Symfony\Component\HttpFoundation;
13
 
14
use Symfony\Component\HttpFoundation\File\UploadedFile;
15
 
16
/**
17
 * FileBag is a container for uploaded files.
18
 *
19
 * @author Fabien Potencier <fabien@symfony.com>
20
 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
21
 */
22
class FileBag extends ParameterBag
23
{
24
    private const FILE_KEYS = ['error', 'name', 'size', 'tmp_name', 'type'];
25
 
26
    /**
27
     * @param array|UploadedFile[] $parameters An array of HTTP files
28
     */
29
    public function __construct(array $parameters = [])
30
    {
31
        $this->replace($parameters);
32
    }
33
 
34
    public function replace(array $files = [])
35
    {
36
        $this->parameters = [];
37
        $this->add($files);
38
    }
39
 
40
    public function set(string $key, mixed $value)
41
    {
42
        if (!\is_array($value) && !$value instanceof UploadedFile) {
43
            throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
44
        }
45
 
46
        parent::set($key, $this->convertFileInformation($value));
47
    }
48
 
49
    public function add(array $files = [])
50
    {
51
        foreach ($files as $key => $file) {
52
            $this->set($key, $file);
53
        }
54
    }
55
 
56
    /**
57
     * Converts uploaded files to UploadedFile instances.
58
     *
59
     * @return UploadedFile[]|UploadedFile|null
60
     */
61
    protected function convertFileInformation(array|UploadedFile $file): array|UploadedFile|null
62
    {
63
        if ($file instanceof UploadedFile) {
64
            return $file;
65
        }
66
 
67
        $file = $this->fixPhpFilesArray($file);
68
        $keys = array_keys($file);
69
        sort($keys);
70
 
71
        if (self::FILE_KEYS == $keys) {
72
            if (\UPLOAD_ERR_NO_FILE == $file['error']) {
73
                $file = null;
74
            } else {
75
                $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
76
            }
77
        } else {
78
            $file = array_map(function ($v) { return $v instanceof UploadedFile || \is_array($v) ? $this->convertFileInformation($v) : $v; }, $file);
79
            if (array_keys($keys) === $keys) {
80
                $file = array_filter($file);
81
            }
82
        }
83
 
84
        return $file;
85
    }
86
 
87
    /**
88
     * Fixes a malformed PHP $_FILES array.
89
     *
90
     * PHP has a bug that the format of the $_FILES array differs, depending on
91
     * whether the uploaded file fields had normal field names or array-like
92
     * field names ("normal" vs. "parent[child]").
93
     *
94
     * This method fixes the array to look like the "normal" $_FILES array.
95
     *
96
     * It's safe to pass an already converted array, in which case this method
97
     * just returns the original array unmodified.
98
     */
99
    protected function fixPhpFilesArray(array $data): array
100
    {
101
        // Remove extra key added by PHP 8.1.
102
        unset($data['full_path']);
103
        $keys = array_keys($data);
104
        sort($keys);
105
 
106
        if (self::FILE_KEYS != $keys || !isset($data['name']) || !\is_array($data['name'])) {
107
            return $data;
108
        }
109
 
110
        $files = $data;
111
        foreach (self::FILE_KEYS as $k) {
112
            unset($files[$k]);
113
        }
114
 
115
        foreach ($data['name'] as $key => $name) {
116
            $files[$key] = $this->fixPhpFilesArray([
117
                'error' => $data['error'][$key],
118
                'name' => $name,
119
                'type' => $data['type'][$key],
120
                'tmp_name' => $data['tmp_name'][$key],
121
                'size' => $data['size'][$key],
122
            ]);
123
        }
124
 
125
        return $files;
126
    }
127
}