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 | * @param string $getter |
||
90 | */ |
||
91 | 14 | public function __construct(string $name, int $type, string $getter) |
|
97 | |||
98 | /** |
||
99 | * Get name |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 10 | public function getName(): string |
|
107 | |||
108 | /** |
||
109 | * Get metadata to include into document's relationship |
||
110 | * [name => value] |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | public function getMetadata(): array |
||
118 | |||
119 | /** |
||
120 | * Is x-to-many type of relationship ? |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | 3 | public function isCollection(): bool |
|
128 | |||
129 | /** |
||
130 | * Get type of relationship |
||
131 | * @see TYPE_X_TO_* constants |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | public function getType(): int |
||
139 | |||
140 | /** |
||
141 | * Set name of property contains related object |
||
142 | * |
||
143 | * @param string $name |
||
144 | */ |
||
145 | 10 | public function setPropertyName(string $name) |
|
149 | |||
150 | /** |
||
151 | * Has name of property ? |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | 1 | public function hasPropertyName(): bool |
|
159 | |||
160 | /** |
||
161 | * Get name of property contains related object |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 2 | public function getPropertyName(): string |
|
169 | |||
170 | /** |
||
171 | * Get name of a getter-method to access related data |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | 3 | public function getGetter(): string |
|
179 | |||
180 | /** |
||
181 | * Set include-data option |
||
182 | * |
||
183 | * @param bool $include |
||
184 | */ |
||
185 | 10 | public function setIncludeData(bool $include = true) |
|
189 | |||
190 | /** |
||
191 | * Is data-section with resource-identifier(s) have to be included ? |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 3 | public function isDataIncluded(): bool |
|
199 | |||
200 | /** |
||
201 | * Set limit of amount of resource-objects in data-section |
||
202 | * Works only for "x-to-many" type of relationship with included data allowed |
||
203 | * |
||
204 | * @param int $limit |
||
205 | */ |
||
206 | 10 | public function setDataLimit(int $limit) |
|
210 | |||
211 | /** |
||
212 | * Get limit of amount of resource-objects in data-section |
||
213 | * Has a sense only for "x-to-many" type of relationship with included data allowed |
||
214 | * |
||
215 | * @return int |
||
216 | */ |
||
217 | 3 | public function getDataLimit(): int |
|
221 | |||
222 | /** |
||
223 | * Merge a relationship into this one |
||
224 | * |
||
225 | * @param self $relationship |
||
226 | */ |
||
227 | 1 | public function merge(self $relationship) |
|
231 | } |