|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Definition of relationship |
|
8
|
|
|
* |
|
9
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition |
|
10
|
|
|
*/ |
|
11
|
|
|
class Relationship |
|
12
|
|
|
{ |
|
13
|
|
|
const TYPE_X_TO_ONE = 1; |
|
14
|
|
|
const TYPE_X_TO_MANY = 2; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Unique name |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $name; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Type |
|
25
|
|
|
* |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $relationType; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Collection of links |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $links = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Extra metadata |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $metadata = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Getter-method to access related data |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $getter; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Getter-method to access an identifier of related object |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $identifierGetter; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Type of related resource |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $resourceType; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Relationship constructor. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @param string $resourceType |
|
70
|
|
|
* @param int $relationType |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct(string $name, string $resourceType, int $relationType) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->name = $name; |
|
75
|
|
|
$this->resourceType = $resourceType; |
|
76
|
|
|
$this->relationType = $relationType; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get name |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getName(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set link |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param LinkInterface $link |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setLink(string $name, LinkInterface $link) |
|
96
|
|
|
{ |
|
97
|
|
|
if (isset($this->links[$name])) { |
|
98
|
|
|
throw new \LogicException(sprintf('A link name by "%s" is already exists for the relationship')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->links[$name] = $link; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get links |
|
106
|
|
|
* [name => link-object] |
|
107
|
|
|
* |
|
108
|
|
|
* @return LinkInterface[] |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getLinks(): array |
|
111
|
|
|
{ |
|
112
|
|
|
$this->links; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get metadata to include into document's relationship |
|
117
|
|
|
* [name => value] |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getMetadata(): array |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->metadata; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Is x-to-many type of relationship ? |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function isCollection(): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->relationType === self::TYPE_X_TO_MANY; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set name of a getter-method to access related data |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $method |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setGetter(string $method) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->getter = $method; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get name of a getter-method to access related data |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getGetter(): string |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->getter; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Set getter-method to access an identifier of related object |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $method |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setIdentifierGetter(string $method) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->identifierGetter = $method; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get getter-method to access an identifier of related object |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getIdentifierGetter(): string |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->identifierGetter; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get type of resource |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getResourceType(): string |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->resourceType; |
|
184
|
|
|
} |
|
185
|
|
|
} |