RelationMapping::name()   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 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Relation;
4
5
use Stratadox\Deserializer\Deserializer;
6
use Stratadox\HydrationMapping\Mapping;
7
use Throwable;
8
9
final class RelationMapping implements Mapping
10
{
11
    /** @var string */
12
    private $name;
13
    /** @var Deserializer */
14
    private $deserialize;
15
16
    private function __construct(string $name, Deserializer $deserializer)
17
    {
18
        $this->name = $name;
19
        $this->deserialize = $deserializer;
20
    }
21
22
    /**
23
     * Creates a new has-one mapping.
24
     *
25
     * @param string       $name         The name of the property.
26
     * @param Deserializer $deserializer The deserializer for the related object.
27
     * @return Mapping                   The has-one mapping.
28
     */
29
    public static function inProperty(
30
        string $name,
31
        Deserializer $deserializer
32
    ): Mapping {
33
        return new self($name, $deserializer);
34
    }
35
36
    /** @inheritdoc */
37
    public function name(): string
38
    {
39
        return $this->name;
40
    }
41
42
    /** @inheritdoc */
43
    public function value(array $data, $owner = null)
44
    {
45
        try {
46
            return $this->deserialize->from($data);
47
        } catch (Throwable $exception) {
48
            throw RelationMappingFailure::encountered($this, $exception);
49
        }
50
    }
51
}
52