]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/ApiController.php
Refactored existing user API work
[bookstack] / app / Http / Controllers / Api / ApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Api\ListingResponseBuilder;
6 use BookStack\Http\Controllers\Controller;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Http\JsonResponse;
9
10 abstract class ApiController extends Controller
11 {
12     protected $rules = [];
13     protected $fieldsToExpose = [];
14
15     /**
16      * Provide a paginated listing JSON response in a standard format
17      * taking into account any pagination parameters passed by the user.
18      */
19     protected function apiListingResponse(Builder $query, array $fields, array $modifiers = []): JsonResponse
20     {
21         $listing = new ListingResponseBuilder($query, request(), $fields);
22
23         foreach ($modifiers as $modifier) {
24             $listing->modifyResults($modifier);
25         }
26
27         return $listing->toResponse();
28     }
29
30     /**
31      * Get the validation rules for this controller.
32      * Defaults to a $rules property but can be a rules() method.
33      */
34     public function getValdationRules(): array
35     {
36         if (method_exists($this, 'rules')) {
37             return $this->rules();
38         }
39
40         return $this->rules;
41     }
42 }