RelationMapping   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inProperty() 0 5 1
A value() 0 6 2
A name() 0 3 1
A __construct() 0 4 1
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