Passed
Push — master ( 01c6a8...5a4258 )
by Jesse
09:00
created

ObservedRelationMapping::hydrating()   A

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 declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Relation;
4
5
use Stratadox\HydrationMapping\Mapping;
6
use Stratadox\Hydrator\HydrationObserver;
7
8
final class ObservedRelationMapping implements Mapping, HydrationObserver
9
{
10
    /** @var string */
11
    private $name;
12
    /** @var null|object */
13
    private $referenceTo;
14
15
    private function __construct(string $name)
16
    {
17
        $this->name = $name;
18
    }
19
20
    public static function inProperty(string $name): self
21
    {
22
        return new self($name);
23
    }
24
25
    public function name(): string
26
    {
27
        return $this->name;
28
    }
29
30
    public function hydrating(object $theInstance, array $theData): void
31
    {
32
        $this->referenceTo = $theInstance;
33
    }
34
35
    public function value(array $data, $owner = null)
36
    {
37
        if (null === $this->referenceTo) {
38
            throw NoRelationObservationMade::forIn($this->name);
39
        }
40
        return $this->referenceTo;
41
    }
42
}