Completed
Pull Request — master (#239)
by Luc
05:18
created

LabelRelation::fromRelationalData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Label\ReadModels\Relations\Repository;
4
5
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\Doctrine\SchemaConfigurator;
6
use CultuurNet\UDB3\Label\ValueObjects\RelationType;
7
use ValueObjects\Identity\UUID;
8
use ValueObjects\String\String as StringLiteral;
9
10
class LabelRelation implements \JsonSerializable
11
{
12
    const UUID = 'uuid';
13
    const RELATION_TYPE = 'relationType';
14
    const RELATION_ID = 'relationId';
15
16
    /**
17
     * @var UUID
18
     */
19
    private $uuid;
20
21
    /**
22
     * @var RelationType
23
     */
24
    private $relationType;
25
26
    /**
27
     * @var StringLiteral
28
     */
29
    private $relationId;
30
31
    /**
32
     * Entity constructor.
33
     * @param UUID $uuid
34
     * @param RelationType $relationType
35
     * @param StringLiteral $relationId
36
     */
37
    public function __construct(
38
        UUID $uuid,
39
        RelationType $relationType,
40
        StringLiteral $relationId
41
    ) {
42
        $this->uuid = $uuid;
43
        $this->relationType = $relationType;
44
        $this->relationId = $relationId;
45
    }
46
47
    /**
48
     * @return UUID
49
     */
50
    public function getUuid()
51
    {
52
        return $this->uuid;
53
    }
54
55
    /**
56
     * @return RelationType
57
     */
58
    public function getRelationType()
59
    {
60
        return $this->relationType;
61
    }
62
63
    /**
64
     * @return StringLiteral
65
     */
66
    public function getRelationId()
67
    {
68
        return $this->relationId;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function jsonSerialize()
75
    {
76
        return [
77
            self::UUID => $this->uuid->toNative(),
78
            self::RELATION_TYPE => $this->relationType->toNative(),
79
            self::RELATION_ID => $this->relationId->toNative()
80
        ];
81
    }
82
83
    /**
84
     * @param array $relation
85
     * @return LabelRelation
86
     */
87
    public static function fromRelationalData(array $relation)
88
    {
89
        return new static(
90
            new UUID($relation[SchemaConfigurator::UUID_COLUMN]),
91
            RelationType::fromNative($relation[SchemaConfigurator::RELATION_TYPE_COLUMN]),
92
            new StringLiteral($relation[SchemaConfigurator::RELATION_ID_COLUMN])
93
        );
94
    }
95
}
96