File::loadVars()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 32
ccs 27
cts 27
cp 1
crap 4
rs 9.52
1
<?php
2
3
namespace kalanis\kw_input\Loaders;
4
5
6
use kalanis\kw_input\Entries;
7
8
9
/**
10
 * Class File
11
 * @package kalanis\kw_input\Loaders
12
 * Load file input array into normalized entries
13
 * @link https://www.php.net/manual/en/reserved.variables.files.php
14
 */
15
class File extends ALoader
16
{
17 2
    public function loadVars(string $source, $array): array
18
    {
19 2
        $entries = new Entries\FileEntry();
20 2
        $result = [];
21 2
        foreach ($array as $postedKey => $posted) {
22 2
            if (is_array($posted['name'])) {
23 2
                foreach ($posted['name'] as $key => $value) {
24 2
                    $entry = clone $entries;
25 2
                    $entry->setEntry($source, sprintf('%s[%s]', $postedKey, $key));
26 2
                    $entry->setFile(
27 2
                        $value,
28 2
                        $posted['tmp_name'][$key],
29 2
                        $posted['type'][$key],
30 2
                        intval($posted['error'][$key]),
31 2
                        intval($posted['size'][$key])
32 2
                    );
33 2
                    $result[] = $entry;
34
                }
35
            } else {
36 2
                $entry = clone $entries;
37 2
                $entry->setEntry($source, $postedKey);
38 2
                $entry->setFile(
39 2
                    $posted['name'],
40 2
                    $posted['tmp_name'],
41 2
                    $posted['type'],
42 2
                    intval($posted['error']),
43 2
                    intval($posted['size'])
44 2
                );
45 2
                $result[] = $entry;
46
            }
47
        }
48 2
        return $result;
49
    }
50
}
51