Skip to content

Commit 7b771ee

Browse files
committed
Merge branch 'release2.10.0'
2 parents 14389fa + caab75c commit 7b771ee

File tree

6 files changed

+299
-16
lines changed

6 files changed

+299
-16
lines changed

src/Folder.php

+65-16
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,38 @@
44
namespace Riclep\Storyblok;
55

66

7+
use Illuminate\Pagination\LengthAwarePaginator;
8+
use Illuminate\Support\Collection;
79
use Illuminate\Support\Facades\Cache;
810
use Riclep\Storyblok\Traits\HasChildClasses;
9-
use Storyblok\Client;
1011

1112
abstract class Folder
1213
{
1314
use HasChildClasses;
1415

16+
1517
/**
16-
* @var bool should we request the start / index page
18+
* @var int Current pagination page
1719
*/
18-
protected $startPage = false;
20+
public $currentPage = 0;
1921

2022

2123
/**
22-
* @var int Current pagination page
24+
* @var int the total number of stories matching the request
25+
*/
26+
public $totalStories;
27+
28+
29+
/**
30+
* @var null|Collection the collection of stories in the folder
2331
*/
24-
protected $currentPage = 0;
32+
public $stories;
33+
34+
35+
/**
36+
* @var bool should we request the start / index page
37+
*/
38+
protected $startPage = false;
2539

2640

2741
/**
@@ -47,25 +61,42 @@ abstract class Folder
4761
*/
4862
protected $settings = [];
4963

64+
65+
public function paginate($page = null, $pageName = 'page')
66+
{
67+
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
68+
69+
return new LengthAwarePaginator(
70+
$this->stories,
71+
$this->totalStories,
72+
$this->perPage,
73+
$page,
74+
[
75+
'path' => LengthAwarePaginator::resolveCurrentPath(),
76+
'pageName' => $pageName,
77+
]
78+
);
79+
}
80+
81+
5082
/**
5183
* Reads a content of the returned stories, processing each one
5284
*
53-
* @return \Illuminate\Support\Collection
85+
* @return Folder
5486
*/
5587
public function read() {
56-
$response = $this->get();
57-
58-
$stories = collect($response);
59-
60-
$stories->transform(function ($story) {
88+
$stories = $this->get()->transform(function ($story) {
6189
$blockClass = $this->getChildClassName('Page', $story['content']['component']);
6290

6391
return new $blockClass($story);
6492
});
6593

66-
return $stories;
94+
$this->stories = $stories;
95+
96+
return $this;
6797
}
6898

99+
69100
/**
70101
* Sets the slug of the folder to request
71102
*
@@ -75,6 +106,7 @@ public function slug($slug) {
75106
$this->slug = $slug;
76107
}
77108

109+
78110
/**
79111
* The order in which we want the items in the response to be returned
80112
*
@@ -84,6 +116,7 @@ public function sort($sortBy) {
84116
$this->sortBy = $sortBy;
85117
}
86118

119+
87120
/**
88121
* Define the settings for the API call
89122
*
@@ -94,10 +127,20 @@ public function settings($settings) {
94127
}
95128

96129

130+
/**
131+
* Returns the total number of stories for this page
132+
*
133+
* @return int
134+
*/
135+
public function count() {
136+
return $this->stories->count() ?? 0;
137+
}
138+
139+
97140
/**
98141
* Caches the response and returns just the bit we want
99142
*
100-
* @return array
143+
* @return Collection
101144
*/
102145
protected function get()
103146
{
@@ -111,13 +154,16 @@ protected function get()
111154
});
112155
}
113156

114-
return $response['stories'];
157+
$this->totalStories = $response['headers']['Total'][0];
158+
159+
return collect($response['stories']);
115160
}
116161

162+
117163
/**
118164
* Makes the actual request
119165
*
120-
* @return array|\Storyblok\stdClass
166+
* @return array
121167
*/
122168
private function makeRequest() {
123169
$storyblokClient = resolve('Storyblok\Client');
@@ -130,6 +176,9 @@ private function makeRequest() {
130176
'per_page' => $this->perPage,
131177
], $this->settings));
132178

133-
return $storyblokClient->getBody();
179+
return [
180+
'headers' => $storyblokClient->getHeaders(),
181+
'stories' => $storyblokClient->getBody()['stories'],
182+
];
134183
}
135184
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
4+
namespace Riclep\Storyblok\Tests\Fixtures\Folders;
5+
6+
7+
use Illuminate\Support\Collection;
8+
9+
class EmptyFolder extends \Riclep\Storyblok\Folder
10+
{
11+
/**
12+
* Override the default folder as we don’t want to make an API
13+
* request within our test.
14+
*
15+
* @return Collection
16+
*/
17+
protected function get()
18+
{
19+
$this->totalStories = 0;
20+
21+
return collect(json_decode(file_get_contents(__DIR__ . '/../empty-folder.json'), true)['stories']);
22+
}
23+
}

tests/Fixtures/Folders/Folder.php

+13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
namespace Riclep\Storyblok\Tests\Fixtures\Folders;
55

66

7+
use Illuminate\Support\Collection;
8+
79
class Folder extends \Riclep\Storyblok\Folder
810
{
11+
/**
12+
* Override the default folder as we don’t want to make an API
13+
* request within our test.
14+
*
15+
* @return Collection
16+
*/
17+
protected function get()
18+
{
19+
$this->totalStories = 15;
920

21+
return collect(json_decode(file_get_contents(__DIR__ . '/../folder.json'), true)['stories']);
22+
}
1023
}

tests/Fixtures/empty-folder.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stories": []
3+
}

tests/Fixtures/folder.json

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"stories": [
3+
{
4+
"name": "2018 Guernsey Literary Festival better than ever",
5+
"created_at": "2022-01-31T14:31:12.149Z",
6+
"published_at": "2022-02-09T13:52:16.000Z",
7+
"alternates": [],
8+
"id": 104025975,
9+
"uuid": "4107c0d0-d203-43d6-b4c5-610b30f85717",
10+
"content": {
11+
"_uid": "28e9c8c4-3edf-4ac2-91ff-2bfe206d3c02",
12+
"date": "2018-06-19 07:58:59",
13+
"image": "",
14+
"title": "2018 Guernsey Literary Festival better than ever",
15+
"component": "news",
16+
"legacy_id": 84,
17+
"_editable": "\u003c!--#storyblok#{\"name\": \"news\", \"space\": \"134844\", \"uid\": \"28e9c8c4-3edf-4ac2-91ff-2bfe206d3c02\", \"id\": \"104025975\"}--\u003e"
18+
},
19+
"slug": "84-2018-guernsey-literary-festival-better-than-ever",
20+
"full_slug": "news/84-2018-guernsey-literary-festival-better-than-ever",
21+
"default_full_slug": null,
22+
"sort_by_date": null,
23+
"position": -1720,
24+
"tag_list": [],
25+
"is_startpage": false,
26+
"parent_id": 97473458,
27+
"meta_data": null,
28+
"group_id": "e8df9ffe-99ae-478b-bba3-5389c24867c2",
29+
"first_published_at": "2022-01-31T14:31:12.193Z",
30+
"release_id": null,
31+
"lang": "default",
32+
"path": null,
33+
"translated_slugs": []
34+
},
35+
{
36+
"name": "2019 WriteStuff Competition ",
37+
"created_at": "2022-01-31T14:31:19.271Z",
38+
"published_at": "2022-02-09T13:52:16.000Z",
39+
"alternates": [],
40+
"id": 104026044,
41+
"uuid": "3d15de7b-c1ce-42bd-9aff-a7adbfab6148",
42+
"content": {
43+
"_uid": "487f3065-e31e-4dad-81f8-427431f26435",
44+
"date": "2019-01-21 15:06:44",
45+
"image": "",
46+
"title": "2019 WriteStuff Competition ",
47+
"component": "news",
48+
"legacy_id": 89,
49+
"_editable": "\u003c!--#storyblok#{\"name\": \"news\", \"space\": \"134844\", \"uid\": \"487f3065-e31e-4dad-81f8-427431f26435\", \"id\": \"104026044\"}--\u003e"
50+
},
51+
"slug": "89-2019-writestuff-competition",
52+
"full_slug": "news/89-2019-writestuff-competition",
53+
"default_full_slug": null,
54+
"sort_by_date": null,
55+
"position": -1820,
56+
"tag_list": [],
57+
"is_startpage": false,
58+
"parent_id": 97473458,
59+
"meta_data": null,
60+
"group_id": "22c60d93-e87f-4bd1-ab0a-6c00cc3f91de",
61+
"first_published_at": "2022-01-31T14:31:19.308Z",
62+
"release_id": null,
63+
"lang": "default",
64+
"path": null,
65+
"translated_slugs": []
66+
},
67+
{
68+
"name": "2021 Literary Festival line-up revealed",
69+
"created_at": "2022-01-31T14:33:09.767Z",
70+
"published_at": "2022-02-09T13:52:16.000Z",
71+
"alternates": [],
72+
"id": 104026588,
73+
"uuid": "3f3f763e-4709-4c7a-b968-fe656c8371f3",
74+
"content": {
75+
"_uid": "e4e7cc8d-b7b5-4db6-b99f-42e4be319c94",
76+
"date": "2021-03-30 00:00",
77+
"tags": [],
78+
"image": {
79+
"id": "3744118",
80+
"alt": "",
81+
"name": "",
82+
"focus": "",
83+
"title": "",
84+
"filename": "https://a.storyblok.com/f/134844/c00a3de04f/128-joanne-harris-author-photo-use-this-one-credit-kyte-photography.jpg",
85+
"copyright": "",
86+
"fieldtype": "asset"
87+
},
88+
"title": "2021 Literary Festival line-up revealed",
89+
"component": "news",
90+
"legacy_id": 128,
91+
"related_news": [],
92+
"related_events": [],
93+
"related_speakers": [],
94+
"_editable": "\u003c!--#storyblok#{\"name\": \"news\", \"space\": \"134844\", \"uid\": \"e4e7cc8d-b7b5-4db6-b99f-42e4be319c94\", \"id\": \"104026588\"}--\u003e"
95+
},
96+
"slug": "128-2021-literary-festival-line-up-revealed",
97+
"full_slug": "news/128-2021-literary-festival-line-up-revealed",
98+
"default_full_slug": null,
99+
"sort_by_date": null,
100+
"position": -2560,
101+
"tag_list": [],
102+
"is_startpage": false,
103+
"parent_id": 97473458,
104+
"meta_data": null,
105+
"group_id": "0fca7735-2c4d-4663-8c48-e5d4ff1c8143",
106+
"first_published_at": "2022-01-31T14:33:09.812Z",
107+
"release_id": null,
108+
"lang": "default",
109+
"path": null,
110+
"translated_slugs": []
111+
},
112+
{
113+
"name": "A Book that Changed my Life",
114+
"created_at": "2022-01-31T14:30:41.847Z",
115+
"published_at": "2022-02-09T13:52:16.000Z",
116+
"alternates": [],
117+
"id": 104025713,
118+
"uuid": "6fdc11c8-f629-46fa-8113-c4624351008c",
119+
"content": {
120+
"_uid": "3548cb67-bda2-4a57-a73a-1da913cc5640",
121+
"date": "2017-02-15 16:12:45",
122+
"image": "",
123+
"title": "A Book that Changed my Life",
124+
"component": "news",
125+
"legacy_id": 67,
126+
"_editable": "\u003c!--#storyblok#{\"name\": \"news\", \"space\": \"134844\", \"uid\": \"3548cb67-bda2-4a57-a73a-1da913cc5640\", \"id\": \"104025713\"}--\u003e"
127+
},
128+
"slug": "67-a-book-that-changed-my-life",
129+
"full_slug": "news/67-a-book-that-changed-my-life",
130+
"default_full_slug": null,
131+
"sort_by_date": null,
132+
"position": -1380,
133+
"tag_list": [],
134+
"is_startpage": false,
135+
"parent_id": 97473458,
136+
"meta_data": null,
137+
"group_id": "f4e0ca89-13ca-4244-99a1-2b541c5fff16",
138+
"first_published_at": "2022-01-31T14:30:41.884Z",
139+
"release_id": null,
140+
"lang": "default",
141+
"path": null,
142+
"translated_slugs": []
143+
}
144+
]
145+
}

0 commit comments

Comments
 (0)