15
15
namespace Storyblok \Api ;
16
16
17
17
use Storyblok \Api \Domain \Value \Id ;
18
+ use Storyblok \Api \Domain \Value \Resolver \LinkType ;
18
19
use Storyblok \Api \Domain \Value \Uuid ;
19
20
use Storyblok \Api \Request \StoriesRequest ;
20
21
use Storyblok \Api \Request \StoryRequest ;
30
31
public function __construct (
31
32
private StoriesApiInterface $ storiesApi ,
32
33
private ResolverInterface $ resolver ,
34
+ private bool $ resolveRelations = false ,
35
+ private bool $ resolveLinks = false ,
33
36
) {
34
37
}
35
38
36
39
public function all (?StoriesRequest $ request = null ): StoriesResponse
37
40
{
38
41
$ response = $ this ->storiesApi ->all ($ request );
39
42
40
- if (null === $ request || 0 === $ request -> withRelations -> count () ) {
43
+ if (null === $ request ) {
41
44
return $ response ;
42
45
}
43
46
44
47
$ stories = [];
45
48
46
49
foreach ($ response ->stories as $ story ) {
47
- $ stories [] = $ this ->resolver ->resolve ($ story , $ response ->rels );
50
+ $ resolvedStory = $ story ;
51
+
52
+ if ($ this ->resolveRelations && 0 !== $ request ->withRelations ->count ()) {
53
+ /**
54
+ * There is a limit of possible resolvable relations.
55
+ *
56
+ * @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
57
+ */
58
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->rels , 0 , 50 ));
59
+ }
60
+
61
+ if ($ this ->resolveLinks && null !== $ request ->resolveLinks ->type ) {
62
+ /**
63
+ * There is a limit of possible resolvable relations.
64
+ *
65
+ * @see https://www.storyblok.com/docs/guide/in-depth/rendering-the-link-field
66
+ */
67
+ $ limit = match ($ request ->resolveLinks ->type ) {
68
+ LinkType::Story => 50 ,
69
+ LinkType::Link, LinkType::Url => 500 ,
70
+ };
71
+
72
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->links , 0 , $ limit ));
73
+ }
74
+
75
+ $ stories [] = $ resolvedStory ;
48
76
}
49
77
50
78
return new StoriesResponse (
@@ -63,14 +91,39 @@ public function allByContentType(string $contentType, ?StoriesRequest $request =
63
91
{
64
92
$ response = $ this ->storiesApi ->allByContentType ($ contentType , $ request );
65
93
66
- if (null === $ request || 0 === $ request -> withRelations -> count () ) {
94
+ if (null === $ request ) {
67
95
return $ response ;
68
96
}
69
97
70
98
$ stories = [];
71
99
72
100
foreach ($ response ->stories as $ story ) {
73
- $ stories [] = $ this ->resolver ->resolve ($ story , $ response ->rels );
101
+ $ resolvedStory = $ story ;
102
+
103
+ if ($ this ->resolveRelations && 0 !== $ request ->withRelations ->count ()) {
104
+ /**
105
+ * There is a limit of possible resolvable relations.
106
+ *
107
+ * @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
108
+ */
109
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->rels , 0 , 50 ));
110
+ }
111
+
112
+ if ($ this ->resolveLinks && null !== $ request ->resolveLinks ->type ) {
113
+ /**
114
+ * There is a limit of possible resolvable relations.
115
+ *
116
+ * @see https://www.storyblok.com/docs/guide/in-depth/rendering-the-link-field
117
+ */
118
+ $ limit = match ($ request ->resolveLinks ->type ) {
119
+ LinkType::Story => 50 ,
120
+ LinkType::Link, LinkType::Url => 500 ,
121
+ };
122
+
123
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->links , 0 , $ limit ));
124
+ }
125
+
126
+ $ stories [] = $ resolvedStory ;
74
127
}
75
128
76
129
return new StoriesResponse (
@@ -89,14 +142,39 @@ public function allByUuids(array $uuids, bool $keepOrder = true, ?StoriesRequest
89
142
{
90
143
$ response = $ this ->storiesApi ->allByUuids ($ uuids , $ keepOrder , $ request );
91
144
92
- if (null === $ request || 0 === $ request -> withRelations -> count () ) {
145
+ if (null === $ request ) {
93
146
return $ response ;
94
147
}
95
148
96
149
$ stories = [];
97
150
98
151
foreach ($ response ->stories as $ story ) {
99
- $ stories [] = $ this ->resolver ->resolve ($ story , $ response ->rels );
152
+ $ resolvedStory = $ story ;
153
+
154
+ if ($ this ->resolveRelations && 0 !== $ request ->withRelations ->count ()) {
155
+ /**
156
+ * There is a limit of possible resolvable relations.
157
+ *
158
+ * @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
159
+ */
160
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->rels , 0 , 50 ));
161
+ }
162
+
163
+ if ($ this ->resolveLinks && null !== $ request ->resolveLinks ->type ) {
164
+ /**
165
+ * There is a limit of possible resolvable relations.
166
+ *
167
+ * @see https://www.storyblok.com/docs/guide/in-depth/rendering-the-link-field
168
+ */
169
+ $ limit = match ($ request ->resolveLinks ->type ) {
170
+ LinkType::Story => 50 ,
171
+ LinkType::Link, LinkType::Url => 500 ,
172
+ };
173
+
174
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->links , 0 , $ limit ));
175
+ }
176
+
177
+ $ stories [] = $ resolvedStory ;
100
178
}
101
179
102
180
return new StoriesResponse (
@@ -115,41 +193,122 @@ public function bySlug(string $slug, ?StoryRequest $request = null): StoryRespon
115
193
{
116
194
$ response = $ this ->storiesApi ->bySlug ($ slug , $ request );
117
195
118
- $ story = $ this ->resolver ->resolve ($ response ->story , $ response ->rels );
196
+ if (null === $ request ) {
197
+ return $ response ;
198
+ }
199
+
200
+ $ resolvedStory = $ response ->story ;
201
+
202
+ if ($ this ->resolveRelations && 0 !== $ request ->withRelations ->count ()) {
203
+ /**
204
+ * There is a limit of possible resolvable relations.
205
+ *
206
+ * @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
207
+ */
208
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->rels , 0 , 50 ));
209
+ }
210
+
211
+ if ($ this ->resolveLinks && null !== $ request ->resolveLinks ->type ) {
212
+ /**
213
+ * There is a limit of possible resolvable relations.
214
+ *
215
+ * @see https://www.storyblok.com/docs/guide/in-depth/rendering-the-link-field
216
+ */
217
+ $ limit = match ($ request ->resolveLinks ->type ) {
218
+ LinkType::Story => 50 ,
219
+ LinkType::Link, LinkType::Url => 500 ,
220
+ };
221
+
222
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->links , 0 , $ limit ));
223
+ }
119
224
120
225
return new StoryResponse ([
121
226
'cv ' => $ response ->cv ,
122
227
'rels ' => $ response ->rels ,
123
228
'links ' => $ response ->links ,
124
- 'story ' => $ story ,
229
+ 'story ' => $ resolvedStory ,
125
230
]);
126
231
}
127
232
128
233
public function byUuid (Uuid $ uuid , ?StoryRequest $ request = null ): StoryResponse
129
234
{
130
235
$ response = $ this ->storiesApi ->byUuid ($ uuid , $ request );
131
236
132
- $ story = $ this ->resolver ->resolve ($ response ->story , $ response ->rels );
237
+ if (null === $ request ) {
238
+ return $ response ;
239
+ }
240
+
241
+ $ resolvedStory = $ response ->story ;
242
+
243
+ if ($ this ->resolveRelations && 0 !== $ request ->withRelations ->count ()) {
244
+ /**
245
+ * There is a limit of possible resolvable relations.
246
+ *
247
+ * @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
248
+ */
249
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->rels , 0 , 50 ));
250
+ }
251
+
252
+ if ($ this ->resolveLinks && null !== $ request ->resolveLinks ->type ) {
253
+ /**
254
+ * There is a limit of possible resolvable relations.
255
+ *
256
+ * @see https://www.storyblok.com/docs/guide/in-depth/rendering-the-link-field
257
+ */
258
+ $ limit = match ($ request ->resolveLinks ->type ) {
259
+ LinkType::Story => 50 ,
260
+ LinkType::Link, LinkType::Url => 500 ,
261
+ };
262
+
263
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->links , 0 , $ limit ));
264
+ }
133
265
134
266
return new StoryResponse ([
135
267
'cv ' => $ response ->cv ,
136
268
'rels ' => $ response ->rels ,
137
269
'links ' => $ response ->links ,
138
- 'story ' => $ story ,
270
+ 'story ' => $ resolvedStory ,
139
271
]);
140
272
}
141
273
142
274
public function byId (Id $ id , ?StoryRequest $ request = null ): StoryResponse
143
275
{
144
276
$ response = $ this ->storiesApi ->byId ($ id , $ request );
145
277
146
- $ story = $ this ->resolver ->resolve ($ response ->story , $ response ->rels );
278
+ if (null === $ request ) {
279
+ return $ response ;
280
+ }
281
+
282
+ $ resolvedStory = $ response ->story ;
283
+
284
+ if ($ this ->resolveRelations && 0 !== $ request ->withRelations ->count ()) {
285
+ /**
286
+ * There is a limit of possible resolvable relations.
287
+ *
288
+ * @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
289
+ */
290
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->rels , 0 , 50 ));
291
+ }
292
+
293
+ if ($ this ->resolveLinks && null !== $ request ->resolveLinks ->type ) {
294
+ /**
295
+ * There is a limit of possible resolvable relations.
296
+ *
297
+ * @see https://www.storyblok.com/docs/guide/in-depth/rendering-the-link-field
298
+ */
299
+ $ limit = match ($ request ->resolveLinks ->type ) {
300
+ LinkType::Story => 50 ,
301
+ LinkType::Link, LinkType::Url => 500 ,
302
+ };
303
+
304
+ $ resolvedStory = $ this ->resolver ->resolve ($ resolvedStory , \array_slice ($ response ->links , 0 , $ limit ));
305
+ }
147
306
148
307
return new StoryResponse ([
149
308
'cv ' => $ response ->cv ,
150
309
'rels ' => $ response ->rels ,
151
310
'links ' => $ response ->links ,
152
- 'story ' => $ story ,
311
+ 'story ' => $ resolvedStory ,
153
312
]);
154
313
}
155
314
}
0 commit comments