1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Facades\Cache; |
10
|
|
|
use Riclep\Storyblok\Traits\HasChildClasses; |
11
|
|
|
|
12
|
|
|
abstract class Folder |
13
|
|
|
{ |
14
|
|
|
use HasChildClasses; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int Current pagination page |
19
|
|
|
*/ |
20
|
|
|
public $currentPage = 0; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
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 |
31
|
|
|
*/ |
32
|
|
|
public $stories; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool should we request the start / index page |
37
|
|
|
*/ |
38
|
|
|
protected $startPage = false; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int number of items to return |
43
|
|
|
*/ |
44
|
|
|
protected $perPage = 10; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string order to sort the returned stories |
49
|
|
|
*/ |
50
|
|
|
protected $sortBy = 'content.date:desc'; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string the slug to start te request from |
55
|
|
|
*/ |
56
|
|
|
protected $slug; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array additional settings for the request |
61
|
|
|
*/ |
62
|
|
|
protected $settings = []; |
63
|
|
|
|
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
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Reads a content of the returned stories, processing each one |
84
|
|
|
* |
85
|
|
|
* @return Folder |
86
|
|
|
*/ |
87
|
|
|
public function read() { |
88
|
|
|
$stories = $this->get()->transform(function ($story) { |
89
|
|
|
$blockClass = $this->getChildClassName('Page', $story['content']['component']); |
90
|
|
|
|
91
|
|
|
return new $blockClass($story); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
$this->stories = $stories; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sets the slug of the folder to request |
102
|
|
|
* |
103
|
|
|
* @param $slug |
104
|
|
|
*/ |
105
|
|
|
public function slug($slug) { |
106
|
|
|
$this->slug = $slug; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* The order in which we want the items in the response to be returned |
112
|
|
|
* |
113
|
|
|
* @param $sortBy |
114
|
|
|
*/ |
115
|
|
|
public function sort($sortBy) { |
116
|
|
|
$this->sortBy = $sortBy; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Define the settings for the API call |
122
|
|
|
* |
123
|
|
|
* @param $settings |
124
|
|
|
*/ |
125
|
|
|
public function settings($settings) { |
126
|
|
|
$this->settings = $settings; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
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
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets the number of items per page |
142
|
|
|
* |
143
|
|
|
* @param $perPage |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function perPage($perPage) { |
147
|
|
|
$this->perPage = $perPage; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Caches the response and returns just the bit we want |
155
|
|
|
* |
156
|
|
|
* @return Collection |
157
|
|
|
*/ |
158
|
|
|
protected function get() |
159
|
|
|
{ |
160
|
|
|
if (request()->has('_storyblok') || !config('storyblok.cache')) { |
161
|
|
|
$response = $this->makeRequest(); |
162
|
|
|
} else { |
163
|
|
|
$unique_tag = md5(serialize( $this->settings )); |
164
|
|
|
|
165
|
|
|
$response = Cache::remember('folder-' . $this->slug . '-' . $unique_tag, config('storyblok.cache_duration') * 60, function () { |
166
|
|
|
return $this->makeRequest(); |
167
|
|
|
}); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->totalStories = $response['headers']['Total'][0]; |
171
|
|
|
|
172
|
|
|
return collect($response['stories']); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Makes the actual request |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
private function makeRequest() { |
182
|
|
|
$storyblokClient = resolve('Storyblok\Client'); |
183
|
|
|
|
184
|
|
|
$storyblokClient = $storyblokClient->getStories(array_merge([ |
185
|
|
|
'is_startpage' => $this->startPage, |
186
|
|
|
'sort_by' => $this->sortBy, |
187
|
|
|
'starts_with' => $this->slug, |
188
|
|
|
'page' => $this->currentPage, |
189
|
|
|
'per_page' => $this->perPage, |
190
|
|
|
], $this->settings)); |
191
|
|
|
|
192
|
|
|
return [ |
193
|
|
|
'headers' => $storyblokClient->getHeaders(), |
194
|
|
|
'stories' => $storyblokClient->getBody()['stories'], |
195
|
|
|
]; |
196
|
|
|
} |
197
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.