FilesAdapter::current()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Adapters;
4
5
6
use kalanis\kw_forms\Exceptions\FormsException;
7
8
9
class FilesAdapter extends AAdapter
10
{
11 7
    public function loadEntries(string $inputType): void
12
    {
13 7
        $this->vars = $this->loadVars($_FILES);
14 7
    }
15
16
    /**
17
     * @param array<string|int, array<string, string|int|array<string|int>>> $array
18
     * @return array<string, FileEntry>
19
     */
20 7
    protected function loadVars(&$array): array
21
    {
22 7
        $entry = new FileEntry();
23 7
        $result = [];
24 7
        foreach ($array as $postedKey => $posted) {
25 7
            if (is_array($posted['name']) && is_array($posted['tmp_name']) && is_array($posted['type']) && is_array($posted['error']) && is_array($posted['size'])) {
26 7
                foreach ($posted['name'] as $key => $value) {
27 7
                    $data = clone $entry;
28 7
                    $data->setData(
29 7
                        sprintf('%s[%s]', $this->removeNullBytes(strval($postedKey)), $this->removeNullBytes(strval($key))),
30 7
                        $this->removeNullBytes(strval($value)),
31 7
                        strval($posted['tmp_name'][$key]),
32 7
                        $this->removeNullBytes(strval($posted['type'][$key])),
33 7
                        intval($posted['error'][$key]),
34 7
                        intval($posted['size'][$key])
35
                    );
36 7
                    $result[$data->getKey()] = $data;
37
                }
38
            } else {
39 7
                $data = clone $entry;
40 7
                $data->setData(
41 7
                    $this->removeNullBytes(strval($postedKey)),
42 7
                    $this->removeNullBytes(strval($posted['name'])),
43 7
                    strval($posted['tmp_name']),
44 7
                    $this->removeNullBytes(strval($posted['type'])),
45 7
                    intval($posted['error']),
46 7
                    intval($posted['size'])
47
                );
48 7
                $result[$data->getKey()] = $data;
49
            }
50
        }
51 7
        return $result;
52
    }
53
54
    /**
55
     * @throws FormsException
56
     * @return mixed|null
57
     */
58
    #[\ReturnTypeWillChange]
59 5
    public function current()
60
    {
61 5
        if ($this->valid()) {
62 5
            return $this->offsetGet($this->key);
63
        }
64 1
        throw new FormsException(sprintf('Unknown offset %s', $this->key));
65
    }
66
67 1
    public function getSource(): string
68
    {
69 1
        return static::SOURCE_FILES;
70
    }
71
}
72