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

FormHydrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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