1515namespace Storyblok \Api ;
1616
1717use Storyblok \Api \Domain \Value \Id ;
18+ use Storyblok \Api \Domain \Value \Resolver \LinkType ;
1819use Storyblok \Api \Domain \Value \Uuid ;
1920use Storyblok \Api \Request \StoriesRequest ;
2021use Storyblok \Api \Request \StoryRequest ;
3031 public function __construct (
3132 private StoriesApiInterface $ storiesApi ,
3233 private ResolverInterface $ resolver ,
34+ private bool $ resolveRelations = false ,
35+ private bool $ resolveLinks = false ,
3336 ) {
3437 }
3538
3639 public function all (?StoriesRequest $ request = null ): StoriesResponse
3740 {
3841 $ response = $ this ->storiesApi ->all ($ request );
3942
40- if (null === $ request || 0 === $ request -> withRelations -> count () ) {
43+ if (null === $ request ) {
4144 return $ response ;
4245 }
4346
4447 $ stories = [];
4548
4649 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 ;
4876 }
4977
5078 return new StoriesResponse (
@@ -63,14 +91,39 @@ public function allByContentType(string $contentType, ?StoriesRequest $request =
6391 {
6492 $ response = $ this ->storiesApi ->allByContentType ($ contentType , $ request );
6593
66- if (null === $ request || 0 === $ request -> withRelations -> count () ) {
94+ if (null === $ request ) {
6795 return $ response ;
6896 }
6997
7098 $ stories = [];
7199
72100 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 ;
74127 }
75128
76129 return new StoriesResponse (
@@ -89,14 +142,39 @@ public function allByUuids(array $uuids, bool $keepOrder = true, ?StoriesRequest
89142 {
90143 $ response = $ this ->storiesApi ->allByUuids ($ uuids , $ keepOrder , $ request );
91144
92- if (null === $ request || 0 === $ request -> withRelations -> count () ) {
145+ if (null === $ request ) {
93146 return $ response ;
94147 }
95148
96149 $ stories = [];
97150
98151 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 ;
100178 }
101179
102180 return new StoriesResponse (
@@ -115,41 +193,122 @@ public function bySlug(string $slug, ?StoryRequest $request = null): StoryRespon
115193 {
116194 $ response = $ this ->storiesApi ->bySlug ($ slug , $ request );
117195
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+ }
119224
120225 return new StoryResponse ([
121226 'cv ' => $ response ->cv ,
122227 'rels ' => $ response ->rels ,
123228 'links ' => $ response ->links ,
124- 'story ' => $ story ,
229+ 'story ' => $ resolvedStory ,
125230 ]);
126231 }
127232
128233 public function byUuid (Uuid $ uuid , ?StoryRequest $ request = null ): StoryResponse
129234 {
130235 $ response = $ this ->storiesApi ->byUuid ($ uuid , $ request );
131236
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+ }
133265
134266 return new StoryResponse ([
135267 'cv ' => $ response ->cv ,
136268 'rels ' => $ response ->rels ,
137269 'links ' => $ response ->links ,
138- 'story ' => $ story ,
270+ 'story ' => $ resolvedStory ,
139271 ]);
140272 }
141273
142274 public function byId (Id $ id , ?StoryRequest $ request = null ): StoryResponse
143275 {
144276 $ response = $ this ->storiesApi ->byId ($ id , $ request );
145277
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+ }
147306
148307 return new StoryResponse ([
149308 'cv ' => $ response ->cv ,
150309 'rels ' => $ response ->rels ,
151310 'links ' => $ response ->links ,
152- 'story ' => $ story ,
311+ 'story ' => $ resolvedStory ,
153312 ]);
154313 }
155314}
0 commit comments