Passed
Pull Request — master (#303)
by Arman
02:44
created

File::getFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.8
13
 */
14
15
namespace Quantum\Http\Traits\Request;
16
17
use Quantum\Libraries\Storage\Exceptions\FileUploadException;
18
use Quantum\Libraries\Storage\UploadedFile;
19
use Quantum\App\Exceptions\BaseException;
20
use ReflectionException;
21
22
/**
23
 * Trait File
24
 * @package Quantum\Http\Request
25
 */
26
trait File
27
{
28
29
    /**
30
     * Files
31
     * @var array
32
     */
33
    private static $__files = [];
34
35
    /**
36
     * Checks to see if request contains file
37
     * @param string $key
38
     * @return bool
39
     */
40
    public static function hasFile(string $key): bool
41
    {
42
        if (!isset(self::$__files[$key])) {
43
            return false;
44
        }
45
46
        if (!is_array(self::$__files[$key]) && self::$__files[$key]->getErrorCode() != UPLOAD_ERR_OK) {
47
            return false;
48
        }
49
50
        if (is_array(self::$__files[$key])) {
51
            foreach (self::$__files[$key] as $file) {
52
                if ($file->getErrorCode() != UPLOAD_ERR_OK) {
53
                    return false;
54
                }
55
            }
56
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Gets the file or array of file objects
64
     * @param string $key
65
     * @return mixed
66
     * @throws BaseException
67
     */
68
    public static function getFile(string $key)
69
    {
70
        if (!self::hasFile($key)) {
71
            throw FileUploadException::fileNotFound($key);
72
        }
73
74
        return self::$__files[$key];
75
    }
76
77
    /**
78
     * Handle files
79
     * @param array $files
80
     * @return array|UploadedFile[]
81
     * @throws BaseException
82
     * @throws ReflectionException
83
     */
84
    public static function handleFiles(array $files): array
85
    {
86
        if (!count($files)) {
87
            return [];
88
        }
89
90
        $key = key($files);
91
92
        if (!$key) {
93
            return [];
94
        }
95
96
        if (!is_array($files[$key]['name'])) {
97
            return [$key => new UploadedFile($files[$key])];
98
        } else {
99
            $formatted = [];
100
101
            foreach ($files[$key]['name'] as $index => $name) {
102
                $formatted[$key][$index] = new UploadedFile([
103
                    'name' => $name,
104
                    'type' => $files[$key]['type'][$index],
105
                    'tmp_name' => $files[$key]['tmp_name'][$index],
106
                    'error' => $files[$key]['error'][$index],
107
                    'size' => $files[$key]['size'][$index],
108
                ]);
109
            }
110
111
            return $formatted;
112
        }
113
    }
114
}