Completed
Pull Request — master (#61)
by Tomas
03:17
created

InputParam::isMulti()   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 0
crap 1
1
<?php
2
3
namespace Tomaj\NetteApi\Params;
4
5
use Exception;
6
7
class InputParam implements ParamInterface
8
{
9
    const TYPE_POST      = 'POST';
10
    const TYPE_GET       = 'GET';
11
    const TYPE_PUT       = 'PUT';
12
    const TYPE_FILE      = 'FILE';
13
    const TYPE_COOKIE    = 'COOKIE';
14
    const TYPE_POST_RAW  = 'POST_RAW';
15
    const TYPE_POST_JSON_KEY  = 'POST_JSON_KEY';
16
17
    const OPTIONAL = false;
18
    const REQUIRED = true;
19
20
    /**
21
     * @var string
22
     */
23
    private $type;
24
25
    /**
26
     * @var string
27
     */
28
    private $key;
29
30
    /**
31
     * @var bool
32
     */
33
    private $required;
34
35
    /**
36
     * @var array|null
37
     */
38
    private $availableValues;
39
40
    /**
41
     * @var bool
42
     */
43
    private $multi;
44
45 48
    public function __construct(string $type, string $key, bool $required = self::OPTIONAL, ?array $availableValues = null, bool $multi = false)
46
    {
47 48
        $this->type = $type;
48 48
        $this->key = $key;
49 48
        $this->required = $required;
50 48
        $this->availableValues = $availableValues;
51 48
        $this->multi = $multi;
52 48
    }
53
54 9
    public function getType(): string
55
    {
56 9
        return $this->type;
57
    }
58
59 21
    public function getKey(): string
60
    {
61 21
        return $this->key;
62
    }
63
64 6
    public function isRequired(): bool
65
    {
66 6
        return $this->required;
67
    }
68
69 6
    public function getAvailableValues(): ?array
70
    {
71 6
        return $this->availableValues;
72
    }
73
74 24
    public function isMulti(): bool
75
    {
76 24
        return $this->multi;
77
    }
78
79
    /**
80
     * Check if actual value from environment is valid
81
     *
82
     * @return bool
83
     *
84
     * @throws Exception if actual InputParam has unsupported type
85
     */
86 18
    public function isValid(): bool
87
    {
88 18
        if ($this->type === self::TYPE_POST_JSON_KEY) {
89
            $input = file_get_contents("php://input");
90
            $params = json_decode($input, true);
91
            if ($input && $params === null) {
92
                return false;
93
            }
94
        }
95
96 18
        if ($this->required === self::OPTIONAL) {
97 6
            return true;
98
        }
99
100 18
        $value = $this->getValue();
101 18
        if ($this->availableValues !== null) {
102 9
            if (is_array($this->availableValues)) {
103 9
                return empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues));
104
            }
105
        }
106
107 12
        if ($this->required) {
108 12
            if ($value === null || $value == '') {
109 6
                return false;
110
            }
111
        }
112 6
        return true;
113
    }
114
115
    /**
116
     * Process environment variables like POST|GET|etc.. and return actual value
117
     *
118
     * @return mixed
119
     *
120
     * @throws Exception if actual InputParam has unsupported type
121
     */
122 39
    public function getValue()
123
    {
124 39
        if ($this->type == self::TYPE_GET) {
125 15
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
126 6
                return $_GET[$this->key];
127
            }
128 9
            if ($this->isMulti()) {
129
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
130
            }
131 9
            return filter_input(INPUT_GET, $this->key);
132
        }
133 33
        if ($this->type == self::TYPE_POST) {
134 12
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
135 6
                return $_POST[$this->key];
136
            }
137 9
            if ($this->isMulti()) {
138
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
139
            }
140 9
            return filter_input(INPUT_POST, $this->key);
141
        }
142 24
        if ($this->type == self::TYPE_FILE) {
143 9
            if (isset($_FILES[$this->key])) {
144 6
                if ($this->isMulti()) {
145 3
                    return $this->processMultiFileUploads($_FILES[$this->key]);
146
                } else {
147 3
                    return $_FILES[$this->key];
148
                }
149
            }
150 3
            return null;
151
        }
152 15
        if ($this->type == self::TYPE_COOKIE) {
153 3
            if (isset($_COOKIE[$this->key])) {
154 3
                return $_COOKIE[$this->key];
155
            }
156
        }
157 12
        if ($this->type == self::TYPE_POST_RAW) {
158 3
            return file_get_contents("php://input");
159
        }
160 9
        if ($this->type == self::TYPE_PUT) {
161 6
            parse_str(file_get_contents("php://input"), $params);
162 6
            if (isset($params[$this->key])) {
163
                return $params[$this->key];
164
            }
165 6
            return '';
166
        }
167 3
        if ($this->type == self::TYPE_POST_JSON_KEY) {
168
            $params = file_get_contents("php://input");
169
            $params = json_decode($params, true);
170
            if (isset($params[$this->key])) {
171
                return $params[$this->key];
172
            }
173
            return '';
174
        }
175 3
        throw new Exception('Invalid type');
176
    }
177
178 3
    private function processMultiFileUploads($files)
179
    {
180 3
        $result = [];
181 3
        foreach ($files as $key => $values) {
182 3
            foreach ($values as $index => $value) {
183 3
                $result[$index][$key] = $value;
184
            }
185
        }
186 3
        return $result;
187
    }
188
}
189