Observe::hydrate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
/**
7
 * Base class for decorating hydrators with observable functionality.
8
 *
9
 * @author Stratadox
10
 */
11
abstract class Observe implements Hydrator
12
{
13
    /** @var Hydrator */
14
    private $hydrator;
15
    /** @var HydrationObserver */
16
    private $observer;
17
18
    final private function __construct(
19
        Hydrator $hydrator,
20
        HydrationObserver $observer
21
    ) {
22
        $this->hydrator = $hydrator;
23
        $this->observer = $observer;
24
    }
25
26
    /**
27
     * Attaches an observer to a hydrator.
28
     *
29
     * @param Hydrator          $hydrator The hydrator to observe.
30
     * @param HydrationObserver $observer The observer to attach.
31
     * @return Hydrator                   The decorated hydrator.
32
     */
33
    public static function hydrating(
34
        Hydrator $hydrator,
35
        HydrationObserver $observer
36
    ): Hydrator {
37
        return new static($hydrator, $observer);
38
    }
39
40
    /** @throws HydrationFailure */
41
    final protected function hydrate(object $target, array $input): void
42
    {
43
        $this->hydrator->writeTo($target, $input);
44
    }
45
46
    final protected function observe(object $target, array $input): void
47
    {
48
        $this->observer->hydrating($target, $input);
49
    }
50
}
51