5
5
use Carbon \Carbon ;
6
6
use Statamic \API \Str ;
7
7
use Statamic \API \Page ;
8
- use Statamic \API \Term ;
9
8
use Statamic \API \Asset ;
10
9
use Statamic \API \Entry ;
11
10
use Statamic \API \Search ;
12
11
use Statamic \API \Content ;
13
- use Statamic \API \Taxonomy ;
14
12
use Statamic \API \GlobalSet ;
15
13
use Statamic \API \Collection ;
16
14
use Statamic \Extend \Extensible ;
17
15
use Statamic \Data \Pages \PageCollection ;
18
16
use Statamic \Data \Pages \Page as PageData ;
19
- use Statamic \Data \Taxonomies \Taxonomy as TaxonomyData ;
20
17
use Illuminate \Support \Collection as IlluminateCollection ;
21
18
22
19
class Fetch
@@ -29,17 +26,16 @@ class Fetch
29
26
public $ depth ;
30
27
public $ locale ;
31
28
public $ nested ;
32
- private $ page ;
33
29
30
+ private $ page ;
34
31
private $ limit ;
35
32
private $ offset ;
36
33
private $ filter ;
37
34
private $ taxonomy ;
38
- private $ index ;
39
35
36
+ private $ index ;
40
37
private $ query ;
41
38
private $ isSearch ;
42
- private $ withData = true ;
43
39
44
40
private $ data ;
45
41
private $ hasNextPage ;
@@ -52,107 +48,28 @@ public function __construct($params = null)
52
48
$ this ->auth = (new FetchAuth )->isAuth ();
53
49
$ this ->deep = $ this ->checkDeep ($ params );
54
50
$ this ->debug = bool (request ('debug ' , $ params ->get ('debug ' )));
55
- $ this ->depth = (int )(request ('depth ' , $ params ->get ('depth ' , null )));
51
+ $ this ->depth = (int ) (request ('depth ' , $ params ->get ('depth ' , null )));
56
52
$ this ->locale = request ('locale ' ) ?: $ params ->get ('locale ' ) ?: default_locale ();
57
53
$ this ->nested = bool (request ('nested ' , $ params ->get ('nested ' , $ this ->getConfigBool ('nested ' ))));
58
54
59
- $ this ->page = (int )(request ('page ' ) ?: $ params ->get ('page ' , 1 ));
60
- $ this ->limit = (int )(request ('limit ' ) ?: $ params ->get ('limit ' ));
61
- $ this ->offset = (int )(request ('offset ' ) ?: $ params ->get ('offset ' ));
55
+ $ this ->page = (int ) (request ('page ' ) ?: $ params ->get ('page ' , 1 ));
56
+ $ this ->limit = (int ) (request ('limit ' ) ?: $ params ->get ('limit ' ));
57
+ $ this ->offset = (int ) (request ('offset ' ) ?: $ params ->get ('offset ' ));
62
58
$ this ->filter = request ('filter ' ) ?: $ params ->get ('filter ' );
63
59
$ this ->taxonomy = request ('taxonomy ' ) ?: $ params ->get ('taxonomy ' );
64
60
65
61
$ this ->index = request ('index ' ) ?: $ params ->get ('index ' );
66
62
$ this ->query = request ('query ' ) ?: $ params ->get ('query ' );
67
63
}
68
64
69
- /**
70
- * Fetch taxonomy
71
- */
72
- public function taxonomy ($ handle = null )
73
- {
74
- $ handle = $ handle ?: request ()->segment (4 );
75
-
76
- if (!$ taxonomy = Taxonomy::whereHandle ($ handle )) {
77
- return "Taxonomy [ $ handle] not found. " ;
78
- }
79
-
80
- return $ this ->handle ($ taxonomy );
81
- }
82
-
83
- /**
84
- * Fetch taxonomies
85
- */
86
- public function taxonomies ($ taxonomies = null )
87
- {
88
- $ taxonomies = $ taxonomies ?: request ('taxonomies ' );
89
-
90
- if (!is_null ($ taxonomies ) && !is_array ($ taxonomies )) {
91
- $ taxonomies = explode (', ' , $ taxonomies );
92
- }
93
-
94
- if ($ taxonomies ) {
95
- $ taxonomies = collect ($ taxonomies )->map (function ($ handle ) {
96
- return Taxonomy::whereHandle ($ handle );
97
- })->filter ();
98
- } else {
99
- $ taxonomies = Taxonomy::all ();
100
- }
101
-
102
- return $ this ->handle ($ taxonomies );
103
- }
104
-
105
- /**
106
- * Fetch term
107
- */
108
- public function term ($ slug = null )
109
- {
110
- if ($ slug ) {
111
- list ($ taxonomy , $ slug ) = explode ('/ ' , $ slug , 2 );
112
- } else {
113
- $ taxonomy = request ()->segment (4 );
114
- $ slug = request ()->segment (5 );
115
- }
116
-
117
- if (!$ term = Term::whereSlug ($ slug , $ taxonomy )) {
118
- return "Term [ $ taxonomy/ $ slug] not found. " ;
119
- }
120
-
121
- return $ this ->handle ($ term );
122
- }
123
-
124
- /**
125
- * Fetch terms
126
- */
127
- public function terms ($ terms = null )
128
- {
129
- $ terms = $ terms ?: request ('terms ' );
130
-
131
- if (!is_null ($ terms ) && !is_array ($ terms )) {
132
- $ terms = explode (', ' , $ terms );
133
- }
134
-
135
- if ($ terms ) {
136
- $ terms = collect ($ terms )->map (function ($ slug ) {
137
- list ($ taxonomy , $ slug ) = explode ('/ ' , $ slug , 2 );
138
-
139
- return Term::whereSlug ($ slug , $ taxonomy );
140
- })->filter ();
141
- } else {
142
- $ terms = Term::all ();
143
- }
144
-
145
- return $ this ->handle ($ terms );
146
- }
147
-
148
65
/**
149
66
* Fetch collection
150
67
*/
151
68
public function collection ($ name = null )
152
69
{
153
70
$ name = $ name ?: request ()->segment (4 );
154
71
155
- if (!$ collection = Collection::whereHandle ($ name )) {
72
+ if (! $ collection = Collection::whereHandle ($ name )) {
156
73
return "Collection [ $ name] not found. " ;
157
74
}
158
75
@@ -165,7 +82,7 @@ public function collection($name = null)
165
82
public function entry ($ id = null )
166
83
{
167
84
if (is_null ($ id ) && request ()->segment (5 )) {
168
- $ id = request ()->segment (4 ) . '/ ' . request ()->segment (5 );
85
+ $ id = request ()->segment (4 ). '/ ' . request ()->segment (5 );
169
86
} else {
170
87
$ id = $ id ?: request ()->segment (4 );
171
88
}
@@ -188,16 +105,16 @@ public function page($uri = null)
188
105
{
189
106
$ uri = $ uri ?: request ()->segment (4 );
190
107
191
- if (!$ uri || $ uri == 'home ' ) {
108
+ if (! $ uri || $ uri == 'home ' ) {
192
109
$ page = Page::whereUri ('/ ' );
193
110
} else {
194
- if (strpos ('/ ' . request ()->path (), $ this ->actionUrl ('page ' )) !== false ) {
111
+ if (strpos ('/ ' . request ()->path (), $ this ->actionUrl ('page ' )) !== false ) {
195
112
$ uri = explode (ltrim ($ this ->actionUrl ('page ' ), '/ ' ), request ()->path ())[1 ];
196
113
}
197
114
198
115
$ uri = Str::ensureLeft (trim ($ uri ), '/ ' );
199
116
200
- if (!$ page = Page::whereUri ($ uri )) {
117
+ if (! $ page = Page::whereUri ($ uri )) {
201
118
return "Page [ $ uri] not found. " ;
202
119
}
203
120
}
@@ -212,7 +129,7 @@ public function pages($pages = null)
212
129
{
213
130
$ pages = $ pages ?: request ('pages ' );
214
131
215
- if (!is_null ($ pages ) && !is_array ($ pages )) {
132
+ if (! is_null ($ pages ) && ! is_array ($ pages )) {
216
133
$ pages = explode (', ' , $ pages );
217
134
}
218
135
@@ -242,7 +159,7 @@ public function global($handle = null)
242
159
{
243
160
$ handle = $ handle ?: request ()->segment (4 );
244
161
245
- if (!$ global = GlobalSet::whereHandle ($ handle )) {
162
+ if (! $ global = GlobalSet::whereHandle ($ handle )) {
246
163
return "Page [ $ handle] not found. " ;
247
164
}
248
165
@@ -256,7 +173,7 @@ public function globals($globals = null)
256
173
{
257
174
$ globals = $ globals ?: request ('globals ' );
258
175
259
- if (!is_null ($ globals ) && !is_array ($ globals )) {
176
+ if (! is_null ($ globals ) && ! is_array ($ globals )) {
260
177
$ globals = explode (', ' , $ globals );
261
178
}
262
179
@@ -314,10 +231,10 @@ private function handle($data)
314
231
}
315
232
316
233
$ result = collect ([
317
- 'data ' => $ this ->data ,
318
- 'page ' => $ this ->page ,
319
- 'limit ' => $ this ->limit ,
320
- 'offset ' => $ this ->offset ,
234
+ 'data ' => $ this ->data ,
235
+ 'page ' => $ this ->page ,
236
+ 'limit ' => $ this ->limit ,
237
+ 'offset ' => $ this ->offset ,
321
238
'has_next_page ' => $ this ->hasNextPage ,
322
239
'total_results ' => $ this ->totalResults ,
323
240
]);
@@ -349,7 +266,7 @@ private function processNestedPages()
349
266
*/
350
267
private function processData ()
351
268
{
352
- if (!$ this ->data instanceof IlluminateCollection) {
269
+ if (! $ this ->data instanceof IlluminateCollection) {
353
270
$ this ->addTaxonomies ($ this ->data );
354
271
355
272
$ this ->data = $ this ->getLocalisedData ($ this ->data );
@@ -400,11 +317,11 @@ private function taxonomizeData()
400
317
*/
401
318
private function filterData ()
402
319
{
403
- if (!in_array ($ this ->filter , ['published ' , 'unpublished ' ])) {
320
+ if (! in_array ($ this ->filter , ['published ' , 'unpublished ' ])) {
404
321
return $ this ;
405
322
}
406
323
407
- $ filter = 'filter ' . Str::ucfirst ($ this ->filter );
324
+ $ filter = 'filter ' . Str::ucfirst ($ this ->filter );
408
325
409
326
if ($ this ->data instanceof IlluminateCollection) {
410
327
$ this ->data = $ this ->data ->filter (function ($ entry ) use ($ filter ) {
@@ -514,18 +431,14 @@ private function getLocalisedData($rawData)
514
431
*/
515
432
private function goDeep ($ item )
516
433
{
517
- if ($ item instanceof TaxonomyData) {
518
- $ item = [$ item ->data ()];
519
- }
520
-
521
434
$ item = collect ($ item )->map (function ($ value , $ key ) {
522
435
if (is_array ($ value )) {
523
436
if ($ key === 'children ' ) {
524
437
return $ value ;
525
438
}
526
439
527
- return collect ($ value )->map (function ($ value, $ key ) {
528
- return $ this ->goDeep ([ $ key => $ value] );
440
+ return collect ($ value )->map (function ($ value ) {
441
+ return $ this ->goDeep ($ value );
529
442
});
530
443
}
531
444
@@ -560,7 +473,7 @@ private function relatedData($value, $key)
560
473
*/
561
474
private function isRelatable ($ value , $ key )
562
475
{
563
- if ($ key === 'id ' && !$ this ->isSearch ) {
476
+ if ($ key === 'id ' && ! $ this ->isSearch ) {
564
477
return false ;
565
478
}
566
479
@@ -601,7 +514,7 @@ private function addChildPagesToPage(PageData $page)
601
514
602
515
private function processPage (array $ page )
603
516
{
604
- if (!empty ($ page ['children ' ])) {
517
+ if (! empty ($ page ['children ' ])) {
605
518
$ page ['children ' ] = collect ($ page ['children ' ])->map (function ($ page ) {
606
519
return $ this ->processPage ($ page );
607
520
})->all ();
0 commit comments