Passed
Pull Request — master (#254)
by Sergei
13:05 queued 10:16
created

FormHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 36
ccs 14
cts 15
cp 0.9333
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A populate() 0 29 5
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Vjik\InputValidation\ValidatingHydrator;
8
9
use function is_array;
10
11
final class FormHydrator
12
{
13 16
    public function __construct(
14
        private ValidatingHydrator $hydrator,
15
    ) {
16 16
    }
17
18 16
    public function populate(
19
        FormModel $model,
20
        mixed $data,
21
        array $map = [],
22
        bool $strict = false,
23
        ?string $scope = null
24
    ): bool {
25 16
        if (!is_array($data)) {
26 2
            return false;
27
        }
28
29 14
        $scope ??= $model->getFormName();
30 14
        if ($scope === '') {
31 3
            $hydrateData = $data;
32
        } else {
33 12
            if (!isset($data[$scope])) {
34 2
                return false;
35
            }
36
            /** @var mixed $hydrateData */
37 10
            $hydrateData = $data[$scope];
38
        }
39
40 13
        if (!is_array($hydrateData)) {
41
            return false;
42
        }
43
44 13
        $this->hydrator->populate($model, $hydrateData, $map, $strict);
45
46 13
        return true;
47
    }
48
}
49