Passed
Pull Request — master (#14)
by Sergei
02:33
created

RequestModel::isOptionalField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\RequestModel;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
9
abstract class RequestModel implements RequestModelInterface
10
{
11
    private array $requestData = [];
12
13 20
    public function setRequestData(array $requestData): void
14
    {
15 20
        $this->requestData = $requestData;
16 20
    }
17
18 17
    public function getValue(string $field, $default = null)
19
    {
20 17
        if ($this->isOptionalField($field)) {
21 9
            $value = ArrayHelper::getValueByPath($this->requestData, $field);
22 9
            if (empty($value)) {
23 5
                return $default;
24
            }
25
        }
26
27 12
        return ArrayHelper::getValueByPath($this->requestData, $field, $default);
28
    }
29
30 1
    public function hasValue(string $field): bool
31
    {
32 1
        return $this->getValue($field) !== null;
33
    }
34
35 2
    public function getRequestData(): array
36
    {
37 2
        return $this->requestData;
38
    }
39
40 11
    public function getOptionalFields(): array
41
    {
42 11
        return [];
43
    }
44
45 17
    private function isOptionalField(string $field): bool
46
    {
47 17
        return in_array($field, $this->getOptionalFields());
48
    }
49
}
50