Completed
Pull Request — master (#61)
by Tomas
02:14
created

FileInputParam::getValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Tomaj\NetteApi\Params;
4
5
use Nette\Application\UI\Form;
6
use Nette\Forms\Controls\BaseControl;
7
8
class FileInputParam extends InputParam
9
{
10
    protected $type = self::TYPE_FILE;
11
12 3
    protected function addFormInput(Form $form, string $key): BaseControl
13
    {
14 3
        return $form->addUpload($key, $this->getParamLabel());
15
    }
16
17 9
    public function getValue()
18
    {
19 9
        if (isset($_FILES[$this->key])) {
20 6
            return $this->isMulti() ? $this->processMultiFileUploads($_FILES[$this->key]) : $_FILES[$this->key];
21
        }
22 3
        return $this->default;
23
    }
24
25 3
    private function processMultiFileUploads($files)
26
    {
27 3
        $result = [];
28 3
        foreach ($files as $key => $values) {
29 3
            foreach ($values as $index => $value) {
30 3
                $result[$index][$key] = $value;
31
            }
32
        }
33 3
        return $result;
34
    }
35
}
36