Completed
Pull Request — master (#61)
by Michal
02:15
created

FileInputParam::addFormInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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