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 | * Include data with resource-identifier(s) |
||
71 | * |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $includeData = false; |
||
75 | |||
76 | /** |
||
77 | * Limit amount of resource identifiers of collection |
||
78 | * Works only with "x-to-many" type of relation. |
||
79 | * |
||
80 | * @var int |
||
81 | */ |
||
82 | protected $dataLimit = 0; |
||
83 | |||
84 | /** |
||
85 | * Relationship constructor. |
||
86 | * |
||
87 | * @param string $name |
||
88 | * @param int $type |
||
89 | */ |
||
90 | 15 | public function __construct(string $name, int $type) |
|
95 | |||
96 | /** |
||
97 | * Get name |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 10 | public function getName(): string |
|
105 | |||
106 | /** |
||
107 | * Get metadata to include into document's relationship |
||
108 | * [name => value] |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getMetadata(): array |
||
116 | |||
117 | /** |
||
118 | * Is x-to-many type of relationship ? |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | 3 | public function isCollection(): bool |
|
126 | |||
127 | /** |
||
128 | * Get type of relationship |
||
129 | * @see TYPE_X_TO_* constants |
||
130 | * |
||
131 | * @return int |
||
132 | */ |
||
133 | public function getType(): int |
||
137 | |||
138 | /** |
||
139 | * Set name of property contains related object |
||
140 | * |
||
141 | * @param string $name |
||
142 | */ |
||
143 | 10 | public function setPropertyName(string $name) |
|
147 | |||
148 | /** |
||
149 | * Has name of property ? |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 1 | public function hasPropertyName(): bool |
|
157 | |||
158 | /** |
||
159 | * Get name of property contains related object |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 9 | public function getPropertyName(): string |
|
167 | |||
168 | /** |
||
169 | * Set name of a getter-method to access related data |
||
170 | * |
||
171 | * @param string $method |
||
172 | */ |
||
173 | 10 | public function setGetter(string $method) |
|
177 | |||
178 | /** |
||
179 | * Contains name of a getter-method to access related data ? |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1 | public function hasGetter(): bool |
|
187 | |||
188 | /** |
||
189 | * Get name of a getter-method to access related data |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 3 | public function getGetter(): string |
|
197 | |||
198 | /** |
||
199 | * Set include-data option |
||
200 | * |
||
201 | * @param bool $include |
||
202 | */ |
||
203 | 10 | public function setIncludeData(bool $include = true) |
|
207 | |||
208 | /** |
||
209 | * Is data-section with resource-identifier(s) have to be included ? |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 3 | public function isDataIncluded(): bool |
|
217 | |||
218 | /** |
||
219 | * Set limit of amount of resource-objects in data-section |
||
220 | * Works only for "x-to-many" type of relationship with included data allowed |
||
221 | * |
||
222 | * @param int $limit |
||
223 | */ |
||
224 | 10 | public function setDataLimit(int $limit) |
|
228 | |||
229 | /** |
||
230 | * Get limit of amount of resource-objects in data-section |
||
231 | * Has a sense only for "x-to-many" type of relationship with included data allowed |
||
232 | * |
||
233 | * @return int |
||
234 | */ |
||
235 | 3 | public function getDataLimit(): int |
|
239 | |||
240 | /** |
||
241 | * Merge a relationship into this one |
||
242 | * |
||
243 | * @param self $relationship |
||
244 | */ |
||
245 | 1 | public function merge(self $relationship) |
|
249 | } |