| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DutchCodingCompany\CursorPagination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Query\Builder as QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\ServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CursorPaginationServiceProvider extends ServiceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Bootstrap the application services. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     public function boot() | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         if ($this->app->runningInConsole()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             $this->publishes([ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                 __DIR__ . '/../config/config.php' => config_path('cursor-pagination.php'), | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             ], 'config'); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->registerMacro(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Create Macros for the Builders. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function registerMacro() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $macro = function ($first, $currentCursor = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $queryOrders = isset($this->query) ? collect($this->query->orders) : collect($this->orders); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $cursor = $currentCursor instanceof Cursor ? $currentCursor : Cursor::decode($currentCursor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             if ($cursor->isPresent()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $apply = function ($query, $queryOrders, $cursor) use (&$apply) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     $query->where(function ($query) use ($queryOrders, $cursor, $apply) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                         $order = array_shift($queryOrders); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                         $column = Arr::get($order, 'column'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         $direction = Arr::get($order, 'direction'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                         $value = $cursor->get($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                         $query->where($column, $direction === 'asc' ? '>' : '<', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                         if (!empty($queryOrders)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                             $query->orWhere($column, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                             $apply($query, $queryOrders, $cursor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $apply($this, $queryOrders->toArray(), collect($cursor->getAfterCursor())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $items = $this->limit($first + 1)->get(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $hasAfter = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             // Check if there are items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             if ($items->count() === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 return new CursorPaginator($items, $currentCursor, $hasAfter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             // Check if there is a next page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             if ($items->count() > $first) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $items->pop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $hasAfter = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             // Get last item and calculate afterCursor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $lastItem = $items->last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $afterCursor = $queryOrders->mapWithKeys(function ($item) use ($lastItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $column = Arr::get($item, 'column'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $value =  $lastItem->{$column}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 if ($value instanceof Carbon) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $value = $value->toIso8601String(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 return [$column => $value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             })->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $afterCursor = new Cursor($afterCursor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             return new CursorPaginator($items, $afterCursor, $hasAfter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         // Register macros | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         QueryBuilder::macro('cursorPaginate', $macro); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         EloquentBuilder::macro('cursorPaginate', $macro); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Register the application services. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // Automatically apply the package configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'cursor-pagination'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // Register the main class to use with the facade | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->app->singleton('cursor-pagination', function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return new CursorPaginator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: