VarsAdapter::loadEntries()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
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
use kalanis\kw_input\Interfaces\IEntry;
8
9
10
class VarsAdapter extends AAdapter
11
{
12
    protected string $inputType = '';
13
14 3
    public function loadEntries(string $inputType): void
15
    {
16 3
        if (IEntry::SOURCE_POST == $inputType) {
17 1
            $this->vars = $this->loadVars($_POST);
18 2
        } elseif (IEntry::SOURCE_GET == $inputType) {
19 1
            $this->vars = $this->loadVars($_GET);
20
        } else {
21 1
            throw new FormsException(sprintf('Unknown input type - %s', $inputType));
22
        }
23 2
        $this->inputType = $inputType;
24 2
    }
25
26
    /**
27
     * @param array<string|int, string|int|float|bool|null>|null $array
28
     * @return array<string, string>
29
     */
30 3
    protected function loadVars(&$array): array
31
    {
32 3
        $result = [];
33 3
        if (is_array($array)) {
34 2
            foreach ($array as $postedKey => $posted) {
35 1
                $result[$this->removeNullBytes(strval($postedKey))] = $this->removeNullBytes(strval($posted));
36
            }
37
        }
38 3
        return $result;
39
    }
40
41 4
    public function getSource(): string
42
    {
43 4
        return $this->inputType;
44
    }
45
}
46