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

RequestModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequestData() 0 3 1
A getOptionalFields() 0 3 1
A isOptionalField() 0 3 1
A getValue() 0 10 3
A getRequestData() 0 3 1
A hasValue() 0 3 1
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