Passed
Push — master ( bdc1f1...0b2259 )
by Jesse
03:03
created

Mapping::writeTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
use Stratadox\HydrationMapping\MapsProperties;
7
use Stratadox\HydrationMapping\UnmappableInput;
8
9
final class Mapping implements Hydrates
10
{
11
    private $hydrator;
12
    private $properties;
13
14
    public function __construct(
15
        Hydrates $hydrator,
16
        MapsProperties $properties
17
    ) {
18
        $this->hydrator = $hydrator;
19
        $this->properties = $properties;
20
    }
21
22
    public static function for(
23
        Hydrates $hydrator,
24
        MapsProperties $properties
25
    ): Hydrates {
26
        return new Mapping($hydrator, $properties);
27
    }
28
29
    /** @inheritdoc */
30
    public function writeTo(object $target, array $input): void
31
    {
32
        $data = [];
33
        try {
34
            foreach ($this->properties as $property) {
35
                $data[$property->name()] = $property->value($input, $target);
36
            }
37
        } catch (UnmappableInput $exception) {
38
            throw HydrationFailed::encountered($exception, $target);
39
        }
40
        $this->hydrator->writeTo($target, $data);
41
    }
42
}
43