Completed
Push — master ( b6475d...698293 )
by Jesse
01:59
created

MappedHydrator::withInstantiator()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Stratadox\Hydrator;
6
7
use Closure;
8
use Stratadox\HydrationMapping\MapsProperties;
9
use Stratadox\Instantiator\CannotInstantiateThis;
10
use Stratadox\Instantiator\Instantiator;
11
use Stratadox\Instantiator\ProvidesInstances;
12
use Throwable;
13
14
/**
15
 * Hydrates an object from mapped array input.
16
 *
17
 * @package Stratadox\Hydrate
18
 * @author Stratadox
19
 */
20
final class MappedHydrator implements Hydrates
21
{
22
    private $make;
23
    private $properties;
24
    private $setter;
25
    private $observer;
26
27
    private function __construct(
28
        ProvidesInstances $instances,
29
        MapsProperties $mapped,
30
        ObservesHydration $observer,
31
        ?Closure $setter
32
    ) {
33
        $this->make = $instances;
34
        $this->properties = $mapped;
35
        $this->observer = $observer;
36
        $this->setter = $setter ?: function (string $attribute, $value) {
37
            $this->$attribute = $value;
38
        };
39
    }
40
41
    /**
42
     * Creates a new mapped hydrator for a class.
43
     *
44
     * @param string                 $class    The class to hydrate.
45
     * @param MapsProperties         $mapped   The mappings for the properties.
46
     * @param Closure|null           $setter   The closure that writes the values.
47
     * @param ObservesHydration|null $observer Object that gets updated with the
48
     *                                         hydrating instance.
49
     * @return self                            The mapped hydrator.
50
     * @throws CannotInstantiateThis           When the class is not instantiable.
51
     */
52
    public static function forThe(
53
        string $class,
54
        MapsProperties $mapped,
55
        Closure $setter = null,
56
        ObservesHydration $observer = null
57
    ): self {
58
        return new self(
59
            Instantiator::forThe($class),
60
            $mapped,
61
            $observer ?: BlindObserver::add(),
62
            $setter
63
        );
64
    }
65
66
    /**
67
     * Creates a new mapped hydrator with an instantiator.
68
     *
69
     * @param ProvidesInstances $instantiator The instance provider to use.
70
     * @param MapsProperties    $mapped       The mappings for the properties.
71
     * @param Closure|null           $setter   The closure that writes the values.
72
     * @param ObservesHydration|null $observer Object that gets updated with the
73
     *                                         hydrating instance.
74
     * @return MappedHydrator                 The mapped hydrator.
75
     */
76
    public static function withInstantiator(
77
        ProvidesInstances $instantiator,
78
        MapsProperties $mapped,
79
        Closure $setter = null,
80
        ObservesHydration $observer = null
81
    ): self {
82
        return new self(
83
            $instantiator,
84
            $mapped,
85
            $observer ?: BlindObserver::add(),
86
            $setter
87
        );
88
    }
89
90
    /** @inheritdoc */
91
    public function fromArray(array $data)
92
    {
93
        try {
94
            $object = $this->make->instance();
95
            $this->observer->hydrating($object);
96
            foreach ($this->properties as $property) {
97
                $this->setter->call($object,
98
                    $property->name(),
99
                    $property->value($data, $object)
100
                );
101
            }
102
            return $object;
103
        } catch (Throwable $exception) {
104
            throw HydrationFailed::encountered($exception, $this->make->class());
105
        }
106
    }
107
}
108