Passed
Push — master ( 2a96e4...6b3c4f )
by Jesse
02:05
created

MutableCollectionHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A write() 0 4 2
A default() 0 3 1
A writeTo() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
use ReflectionException;
7
use ReflectionMethod as Reflected;
8
use Throwable;
9
10
/**
11
 * Hydrates a collection by calling its constructor with squashed array input.
12
 *
13
 * @package Stratadox\Hydrate
14
 * @author  Stratadox
15
 */
16
final class MutableCollectionHydrator implements Hydrates
17
{
18
    private function __construct()
19
    {
20
    }
21
22
    /**
23
     * Produces a collection hydrator.
24
     *
25
     * @return Hydrates A hydrator that calls the constructor through reflection.
26
     */
27
    public static function default(): Hydrates
28
    {
29
        return new MutableCollectionHydrator;
30
    }
31
32
    /** @inheritdoc */
33
    public function writeTo(object $collection, array $input): void
34
    {
35
        try {
36
            $this->write($collection, $input);
37
        } catch (Throwable $exception) {
38
            throw HydrationFailed::encountered($exception, $collection);
39
        }
40
    }
41
42
    private function write(object $collection, array $input): void
43
    {
44
        foreach ($input as $key => $value) {
45
            $collection[$key] = $value;
46
        }
47
    }
48
}
49