77use Rareloop \Lumberjack \Post ;
88use Spatie \Macroable \Macroable ;
99use Tightenco \Collect \Support \Collection ;
10+ use Timber \PostQuery ;
1011use Timber \Timber ;
1112
1213class QueryBuilder implements QueryBuilderContract
@@ -28,33 +29,33 @@ class QueryBuilder implements QueryBuilderContract
2829 const OR = 'OR ' ;
2930 const AND = 'AND ' ;
3031
31- public function getParameters () : array
32+ public function getParameters (): array
3233 {
3334 return $ this ->params ;
3435 }
3536
36- public function wherePostType ($ postType ) : QueryBuilderContract
37+ public function wherePostType ($ postType ): QueryBuilderContract
3738 {
3839 $ this ->params ['post_type ' ] = $ postType ;
3940
4041 return $ this ;
4142 }
4243
43- public function limit ($ limit ) : QueryBuilderContract
44+ public function limit ($ limit ): QueryBuilderContract
4445 {
4546 $ this ->params ['posts_per_page ' ] = $ limit ;
4647
4748 return $ this ;
4849 }
4950
50- public function offset ($ offset ) : QueryBuilderContract
51+ public function offset ($ offset ): QueryBuilderContract
5152 {
5253 $ this ->params ['offset ' ] = $ offset ;
5354
5455 return $ this ;
5556 }
5657
57- public function orderBy ($ orderBy , string $ order = QueryBuilder::ASC ) : QueryBuilderContract
58+ public function orderBy ($ orderBy , string $ order = QueryBuilder::ASC ): QueryBuilderContract
5859 {
5960 $ order = strtoupper ($ order );
6061
@@ -64,7 +65,7 @@ public function orderBy($orderBy, string $order = QueryBuilder::ASC) : QueryBuil
6465 return $ this ;
6566 }
6667
67- public function orderByMeta ($ metaKey , string $ order = QueryBuilder::ASC , string $ type = null ) : QueryBuilderContract
68+ public function orderByMeta ($ metaKey , string $ order = QueryBuilder::ASC , string $ type = null ): QueryBuilderContract
6869 {
6970 $ order = strtoupper ($ order );
7071
@@ -75,21 +76,21 @@ public function orderByMeta($metaKey, string $order = QueryBuilder::ASC, string
7576 return $ this ;
7677 }
7778
78- public function whereIdIn (array $ ids ) : QueryBuilderContract
79+ public function whereIdIn (array $ ids ): QueryBuilderContract
7980 {
8081 $ this ->params ['post__in ' ] = $ ids ;
8182
8283 return $ this ;
8384 }
8485
85- public function whereIdNotIn (array $ ids ) : QueryBuilderContract
86+ public function whereIdNotIn (array $ ids ): QueryBuilderContract
8687 {
8788 $ this ->params ['post__not_in ' ] = $ ids ;
8889
8990 return $ this ;
9091 }
9192
92- public function whereStatus () : QueryBuilderContract
93+ public function whereStatus (): QueryBuilderContract
9394 {
9495 $ args = func_get_args ();
9596
@@ -107,7 +108,7 @@ protected function initialiseMetaQuery()
107108 $ this ->params ['meta_query ' ] = $ this ->params ['meta_query ' ] ?? [];
108109 }
109110
110- public function whereMeta ($ key , $ value , $ compare = '= ' , $ type = null ) : QueryBuilderContract
111+ public function whereMeta ($ key , $ value , $ compare = '= ' , $ type = null ): QueryBuilderContract
111112 {
112113 $ meta = [
113114 'key ' => $ key ,
@@ -125,7 +126,7 @@ public function whereMeta($key, $value, $compare = '=', $type = null) : QueryBui
125126 return $ this ;
126127 }
127128
128- public function whereMetaRelationshipIs (string $ relation ) : QueryBuilderContract
129+ public function whereMetaRelationshipIs (string $ relation ): QueryBuilderContract
129130 {
130131 $ relation = strtoupper ($ relation );
131132
@@ -141,14 +142,14 @@ public function whereMetaRelationshipIs(string $relation) : QueryBuilderContract
141142 return $ this ;
142143 }
143144
144- public function as ($ postClass ) : QueryBuilderContract
145+ public function as ($ postClass ): QueryBuilderContract
145146 {
146147 $ this ->postClass = $ postClass ;
147148
148149 return $ this ;
149150 }
150151
151- public function get () : Collection
152+ public function get (): Collection
152153 {
153154 $ posts = Timber::get_posts ($ this ->getParameters (), $ this ->postClass );
154155
@@ -159,12 +160,30 @@ public function get() : Collection
159160 return collect ($ posts );
160161 }
161162
163+ public function paginate ($ perPage = 10 , $ page = 1 ): PostQuery
164+ {
165+ global $ paged ;
166+
167+ if (isset ($ page )) {
168+ $ paged = $ page ;
169+ }
170+
171+ if (!isset ($ paged ) || !$ paged ) {
172+ $ paged = 1 ;
173+ }
174+
175+ $ this ->limit ($ perPage );
176+ $ this ->params ['paged ' ] = $ paged ;
177+
178+ return new PostQuery ($ this ->getParameters (), $ this ->postClass );
179+ }
180+
162181 /**
163182 * Get the first Post that matches the current query. If no Post matches then return `null`.
164183 *
165184 * @return \Rareloop\Lumberjack\Post|null
166185 */
167- public function first () : ?Post
186+ public function first (): ?Post
168187 {
169188 $ params = array_merge ($ this ->getParameters (), [
170189 'limit ' => 1 ,
@@ -179,7 +198,7 @@ public function first() : ?Post
179198 return collect ($ posts )->first ();
180199 }
181200
182- public function clone () : QueryBuilderContract
201+ public function clone (): QueryBuilderContract
183202 {
184203 $ clone = clone $ this ;
185204
0 commit comments