Passed
Pull Request — master (#254)
by Sergei
02:48
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Yiisoft\Hydrator\HydratorInterface;
8
9
use function is_array;
10
11
/**
12
 * @psalm-import-type MapType from HydratorInterface
13
 */
14
final class FormHydrator
15
{
16 16
    public function __construct(
17
        private HydratorInterface $hydrator,
18
    ) {
19 16
    }
20
21
    /**
22
     * @psalm-param MapType $map
23
     */
24 16
    public function populate(
25
        FormModel $model,
26
        mixed $data,
27
        array $map = [],
28
        bool $strict = false,
29
        ?string $scope = null
30
    ): bool {
31 16
        if (!is_array($data)) {
32 2
            return false;
33
        }
34
35 14
        $scope ??= $model->getFormName();
36 14
        if ($scope === '') {
37 3
            $hydrateData = $data;
38
        } else {
39 12
            if (!isset($data[$scope])) {
40 2
                return false;
41
            }
42 10
            $hydrateData = $data[$scope];
43
        }
44
45 13
        if (!is_array($hydrateData)) {
46
            return false;
47
        }
48
49 13
        $this->hydrator->hydrate($model, $hydrateData, $map, $strict);
50
51 13
        return true;
52
    }
53
}
54