1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksAwareInterface; |
7
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksContainer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Mapping Definition |
11
|
|
|
* |
12
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition |
13
|
|
|
*/ |
14
|
|
|
class Definition implements LinksAwareInterface |
15
|
|
|
{ |
16
|
|
|
use LinksContainer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class covered by definition |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $class; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Attributes |
27
|
|
|
* |
28
|
|
|
* @var Attribute[] |
29
|
|
|
*/ |
30
|
|
|
protected $attributes = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Relationships |
34
|
|
|
* |
35
|
|
|
* @var Relationship[] |
36
|
|
|
*/ |
37
|
|
|
protected $relationships = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Definition constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $class |
43
|
|
|
*/ |
44
|
14 |
|
public function __construct(string $class) |
45
|
|
|
{ |
46
|
14 |
|
$this->class = $class; |
47
|
14 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get class covered by definition |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
2 |
|
public function getClass(): string |
55
|
|
|
{ |
56
|
2 |
|
return $this->class; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add attribute |
61
|
|
|
* |
62
|
|
|
* @param Attribute $attribute |
63
|
|
|
* @throws \LogicException |
64
|
|
|
*/ |
65
|
|
|
public function addAttribute(Attribute $attribute) |
66
|
|
|
{ |
67
|
|
|
$name = $attribute->getName(); |
68
|
|
|
|
69
|
|
|
if (isset($this->attributes[$name])) { |
70
|
|
|
throw new \LogicException(sprintf('Attribute "%s" already defined.', $name)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->attributes[$name] = $attribute; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get attributes |
78
|
|
|
* |
79
|
|
|
* @return Attribute[] |
80
|
|
|
*/ |
81
|
1 |
|
public function getAttributes(): array |
82
|
|
|
{ |
83
|
1 |
|
return $this->attributes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add relationship |
88
|
|
|
* |
89
|
|
|
* @param Relationship $relationship |
90
|
|
|
* @throws \LogicException |
91
|
|
|
*/ |
92
|
7 |
|
public function addRelationship(Relationship $relationship) |
93
|
|
|
{ |
94
|
7 |
|
$name = $relationship->getName(); |
95
|
|
|
|
96
|
7 |
|
if (isset($this->relationships[$name])) { |
97
|
1 |
|
throw new \LogicException(sprintf('Relationship "%s" already defined.', $name)); |
98
|
|
|
} |
99
|
|
|
|
100
|
7 |
|
$this->relationships[$name] = $relationship; |
101
|
7 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get relationships |
105
|
|
|
* |
106
|
|
|
* @return Relationship[] |
107
|
|
|
*/ |
108
|
6 |
|
public function getRelationships(): array |
109
|
|
|
{ |
110
|
6 |
|
return $this->relationships; |
111
|
|
|
} |
112
|
|
|
} |