Passed
Push — master ( c15889...08ecf6 )
by Sergei
03:03
created

FormHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

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