1 | <?php |
||
15 | class Relationship implements LinksAwareInterface |
||
16 | { |
||
17 | use LinksContainer; |
||
18 | |||
19 | const TYPE_X_TO_ONE = 1; |
||
20 | const TYPE_X_TO_MANY = 2; |
||
21 | |||
22 | /** |
||
23 | * Name unique name of resource-object inside of relationships-object |
||
24 | * |
||
25 | * { |
||
26 | * "author": { <--- Name of resource-object |
||
27 | * "data": { |
||
28 | * "id": "12345", |
||
29 | * "type": "Author" |
||
30 | * } |
||
31 | * } |
||
32 | * } |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $name; |
||
37 | |||
38 | /** |
||
39 | * Type or relationship: "x to one" or "x to many". |
||
40 | * |
||
41 | * @see for TYPE_* constants. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $type; |
||
46 | |||
47 | /** |
||
48 | * Extra metadata |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $metadata = []; |
||
53 | |||
54 | /** |
||
55 | * Name of property contains related object. |
||
56 | * Value is optional. Can be set only for real properties. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $propertyName; |
||
61 | |||
62 | /** |
||
63 | * Getter-method to access related data |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $getter; |
||
68 | |||
69 | /** |
||
70 | * Getter-method to access an identifier of related object |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $identifierGetter; |
||
75 | |||
76 | /** |
||
77 | * Type of related resource |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $resourceType; |
||
82 | |||
83 | /** |
||
84 | * Include data with resource-identifier(s) |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $includeData = false; |
||
89 | |||
90 | /** |
||
91 | * Limit amount of resource identifiers of collection |
||
92 | * Works only with "x-to-many" type of relation. |
||
93 | * |
||
94 | * @var int |
||
95 | */ |
||
96 | protected $dataLimit = 0; |
||
97 | |||
98 | /** |
||
99 | * Relationship constructor. |
||
100 | * |
||
101 | * @param string $name |
||
102 | * @param int $type |
||
103 | */ |
||
104 | 12 | public function __construct(string $name, int $type) |
|
109 | |||
110 | /** |
||
111 | * Get name |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 6 | public function getName(): string |
|
119 | |||
120 | /** |
||
121 | * Get metadata to include into document's relationship |
||
122 | * [name => value] |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getMetadata(): array |
||
130 | |||
131 | /** |
||
132 | * Is x-to-many type of relationship ? |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 3 | public function isCollection(): bool |
|
140 | |||
141 | /** |
||
142 | * Set name of property contains related object |
||
143 | * |
||
144 | * @param string $name |
||
145 | */ |
||
146 | 6 | public function setPropertyName(string $name) |
|
150 | |||
151 | /** |
||
152 | * Has name of property ? |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | 1 | public function hasPropertyName(): bool |
|
160 | |||
161 | /** |
||
162 | * Get name of property contains related object |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | 5 | public function getPropertyName(): string |
|
170 | |||
171 | /** |
||
172 | * Set name of a getter-method to access related data |
||
173 | * |
||
174 | * @param string $method |
||
175 | */ |
||
176 | 6 | public function setGetter(string $method) |
|
180 | |||
181 | /** |
||
182 | * Contains name of a getter-method to access related data ? |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 1 | public function hasGetter(): bool |
|
190 | |||
191 | /** |
||
192 | * Get name of a getter-method to access related data |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | 3 | public function getGetter(): string |
|
200 | |||
201 | /** |
||
202 | * Set getter-method to access an identifier of related object |
||
203 | * |
||
204 | * @param string $method |
||
205 | */ |
||
206 | 5 | public function setIdentifierGetter(string $method) |
|
210 | |||
211 | /** |
||
212 | * Has identifier type defined ? |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 3 | public function hasIdentifierGetter(): bool |
|
220 | |||
221 | /** |
||
222 | * Get getter-method to access an identifier of related object |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 3 | public function getIdentifierGetter(): string |
|
230 | |||
231 | /** |
||
232 | * Set type of resource |
||
233 | * |
||
234 | * @param string $type |
||
235 | */ |
||
236 | 5 | public function setResourceType(string $type) |
|
240 | |||
241 | /** |
||
242 | * Has resource type defined ? |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | 3 | public function hasResourceType(): bool |
|
250 | |||
251 | /** |
||
252 | * Get type of resource |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 3 | public function getResourceType(): string |
|
260 | |||
261 | /** |
||
262 | * Set include-data option |
||
263 | * |
||
264 | * @param bool $include |
||
265 | */ |
||
266 | 1 | public function setIncludeData(bool $include = true) |
|
270 | |||
271 | /** |
||
272 | * Is data-section with resource-identifier(s) have to be included ? |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | 1 | public function isDataIncluded(): bool |
|
280 | |||
281 | /** |
||
282 | * Set limit of amount of resource-objects in data-section |
||
283 | * Works only for "x-to-many" type of relationship with included data allowed |
||
284 | * |
||
285 | * @param int $limit |
||
286 | */ |
||
287 | 1 | public function setDataLimit(int $limit) |
|
291 | |||
292 | /** |
||
293 | * Get limit of amount of resource-objects in data-section |
||
294 | * Has a sense only for "x-to-many" type of relationship with included data allowed |
||
295 | * |
||
296 | * @return int |
||
297 | */ |
||
298 | 1 | public function getDataLimit(): int |
|
302 | } |