| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Controller.php | 
					
						
							| 
									
										
										
										
											2020-01-23 19:44:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | namespace FireflyIII\Api\V1\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2021-07-04 19:38:17 +02:00
										 |  |  | use Carbon\Exceptions\InvalidFormatException; | 
					
						
							| 
									
										
										
										
											2025-01-25 04:48:51 +01:00
										 |  |  | use FireflyIII\Exceptions\BadHttpHeaderException; | 
					
						
							| 
									
										
										
										
											2023-11-30 17:28:44 +01:00
										 |  |  | use FireflyIII\Models\Preference; | 
					
						
							| 
									
										
										
										
											2024-12-28 18:38:19 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2024-12-28 07:35:20 +01:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							|  |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  | use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait; | 
					
						
							| 
									
										
										
										
											2025-03-14 19:18:17 +01:00
										 |  |  | use FireflyIII\Transformers\AbstractTransformer; | 
					
						
							| 
									
										
										
										
											2023-11-30 17:28:44 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | 
					
						
							|  |  |  | use Illuminate\Foundation\Bus\DispatchesJobs; | 
					
						
							|  |  |  | use Illuminate\Foundation\Validation\ValidatesRequests; | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | use Illuminate\Routing\Controller as BaseController; | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  | use League\Fractal\Manager; | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | use League\Fractal\Pagination\IlluminatePaginatorAdapter; | 
					
						
							|  |  |  | use League\Fractal\Resource\Collection as FractalCollection; | 
					
						
							|  |  |  | use League\Fractal\Resource\Item; | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  | use League\Fractal\Serializer\JsonApiSerializer; | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  | use Symfony\Component\HttpFoundation\Exception\BadRequestException; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use Symfony\Component\HttpFoundation\ParameterBag; | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class Controller. | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-01-03 18:16:27 +01:00
										 |  |  |  * @SuppressWarnings("PHPMD.CouplingBetweenObjects") | 
					
						
							| 
									
										
										
										
											2025-01-03 19:07:29 +01:00
										 |  |  |  * @SuppressWarnings("PHPMD.NumberOfChildren") | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-31 15:12:26 +02:00
										 |  |  | abstract class Controller extends BaseController | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |     use AuthorizesRequests; | 
					
						
							|  |  |  |     use DispatchesJobs; | 
					
						
							|  |  |  |     use ValidatesRequests; | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  |     use ValidatesUserGroupTrait; | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-26 14:33:19 +01:00
										 |  |  |     protected const string CONTENT_TYPE      = 'application/vnd.api+json'; | 
					
						
							| 
									
										
										
										
											2025-01-25 04:48:51 +01:00
										 |  |  |     protected const string JSON_CONTENT_TYPE = 'application/json'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 11:31:14 +01:00
										 |  |  |     /** @var array<int, string> */ | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |     protected array        $allowedSort; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |     protected ParameterBag $parameters; | 
					
						
							| 
									
										
										
										
											2025-01-26 14:33:19 +01:00
										 |  |  |     protected bool        $convertToNative   = false; | 
					
						
							| 
									
										
										
										
											2025-02-01 19:06:02 +01:00
										 |  |  |     protected array $accepts                 = ['application/json', 'application/vnd.api+json']; | 
					
						
							| 
									
										
										
										
											2025-01-22 05:24:12 +01:00
										 |  |  |     protected TransactionCurrency $nativeCurrency; | 
					
						
							| 
									
										
										
										
											2020-07-31 09:24:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Controller constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |         // get global parameters
 | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |         $this->allowedSort = config('firefly.allowed_sort_parameters'); | 
					
						
							| 
									
										
										
										
											2020-08-22 19:27:16 +02:00
										 |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2023-10-05 19:11:17 +02:00
										 |  |  |                 $this->parameters = $this->getParameters(); | 
					
						
							| 
									
										
										
										
											2020-08-22 19:27:16 +02:00
										 |  |  |                 if (auth()->check()) { | 
					
						
							| 
									
										
										
										
											2024-12-28 07:35:20 +01:00
										 |  |  |                     $language              = Steam::getLanguage(); | 
					
						
							|  |  |  |                     $this->convertToNative = Amount::convertToNative(); | 
					
						
							| 
									
										
										
										
											2025-01-26 14:33:19 +01:00
										 |  |  |                     $this->nativeCurrency  = Amount::getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2020-08-22 19:27:16 +02:00
										 |  |  |                     app()->setLocale($language); | 
					
						
							| 
									
										
										
										
											2025-01-25 04:48:51 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-26 05:11:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 04:48:51 +01:00
										 |  |  |                 // filter down what this endpoint accepts.
 | 
					
						
							|  |  |  |                 if (!$request->accepts($this->accepts)) { | 
					
						
							|  |  |  |                     throw new BadHttpHeaderException(sprintf('Sorry, Accept header "%s" is not something this endpoint can provide.', $request->header('Accept'))); | 
					
						
							| 
									
										
										
										
											2020-08-22 19:27:16 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 04:48:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 19:27:16 +02:00
										 |  |  |                 return $next($request); | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |      * Method to grab all parameters from the URL. | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function getParameters(): ParameterBag | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $bag      = new ParameterBag(); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $page     = (int) request()->get('page'); | 
					
						
							| 
									
										
										
										
											2022-06-23 09:33:43 +02:00
										 |  |  |         if ($page < 1) { | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |             $page = 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-30 04:12:18 +01:00
										 |  |  |         if ($page > 2 ** 16) { | 
					
						
							|  |  |  |             $page = 2 ** 16; | 
					
						
							| 
									
										
										
										
											2022-06-23 09:33:43 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |         $bag->set('page', $page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |         // some date fields:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $dates    = ['start', 'end', 'date']; | 
					
						
							| 
									
										
										
										
											2018-02-16 14:51:59 +01:00
										 |  |  |         foreach ($dates as $field) { | 
					
						
							| 
									
										
										
										
											2023-02-12 07:23:57 +01:00
										 |  |  |             $date = null; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $date = request()->query->get($field); | 
					
						
							| 
									
										
										
										
											2023-01-21 12:21:06 +01:00
										 |  |  |             } catch (BadRequestException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                 app('log')->error(sprintf('Request field "%s" contains a non-scalar value. Value set to NULL.', $field)); | 
					
						
							|  |  |  |                 app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |                 app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  |                 $value = null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |             $obj  = null; | 
					
						
							| 
									
										
										
										
											2018-04-02 14:17:11 +02:00
										 |  |  |             if (null !== $date) { | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |                 try { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     $obj = Carbon::parse((string) $date); | 
					
						
							| 
									
										
										
										
											2025-01-04 08:02:05 +01:00
										 |  |  |                 } catch (InvalidFormatException $e) { | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |                     // don't care
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                     app('log')->warning( | 
					
						
							|  |  |  |                         sprintf( | 
					
						
							|  |  |  |                             'Ignored invalid date "%s" in API controller parameter check: %s', | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                             substr((string) $date, 0, 20), | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                             $e->getMessage() | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     ); | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |             $bag->set($field, $obj); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-26 12:09:46 +01:00
										 |  |  |         // integer fields:
 | 
					
						
							|  |  |  |         $integers = ['limit']; | 
					
						
							|  |  |  |         foreach ($integers as $integer) { | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $value = request()->query->get($integer); | 
					
						
							| 
									
										
										
										
											2023-01-21 12:21:06 +01:00
										 |  |  |             } catch (BadRequestException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                 app('log')->error(sprintf('Request field "%s" contains a non-scalar value. Value set to NULL.', $integer)); | 
					
						
							|  |  |  |                 app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |                 app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  |                 $value = null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-01-26 12:09:46 +01:00
										 |  |  |             if (null !== $value) { | 
					
						
							| 
									
										
										
										
											2025-01-25 09:17:21 +01:00
										 |  |  |                 $value = (int) $value; | 
					
						
							|  |  |  |                 if ($value < 1) { | 
					
						
							|  |  |  |                     $value = 1; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if ($value > 2 ** 16) { | 
					
						
							|  |  |  |                     $value = 2 ** 16; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $bag->set($integer, $value); | 
					
						
							| 
									
										
										
										
											2019-01-26 12:09:46 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             if (null === $value | 
					
						
							|  |  |  |                 && 'limit' === $integer // @phpstan-ignore-line
 | 
					
						
							|  |  |  |                 && auth()->check()) { | 
					
						
							| 
									
										
										
										
											2023-10-05 18:52:01 +02:00
										 |  |  |                 // set default for user:
 | 
					
						
							| 
									
										
										
										
											2023-11-30 17:28:44 +01:00
										 |  |  |                 /** @var User $user */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |                 $user     = auth()->user(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 17:28:44 +01:00
										 |  |  |                 /** @var Preference $pageSize */ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $pageSize = (int) app('preferences')->getForUser($user, 'listPageSize', 50)->data; | 
					
						
							| 
									
										
										
										
											2023-10-05 18:52:01 +02:00
										 |  |  |                 $bag->set($integer, $pageSize); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-01-26 12:09:46 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |         // sort fields:
 | 
					
						
							| 
									
										
										
										
											2021-05-24 08:22:41 +02:00
										 |  |  |         return $this->getSortParameters($bag); | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getSortParameters(ParameterBag $bag): ParameterBag | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sortParameters = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $param = (string) request()->query->get('sort'); | 
					
						
							| 
									
										
										
										
											2023-01-21 12:21:06 +01:00
										 |  |  |         } catch (BadRequestException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error('Request field "sort" contains a non-scalar value. Value set to NULL.'); | 
					
						
							|  |  |  |             app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |             app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-01-20 22:08:18 +01:00
										 |  |  |             $param = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |         if ('' === $param) { | 
					
						
							|  |  |  |             return $bag; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $parts          = explode(',', $param); | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |         foreach ($parts as $part) { | 
					
						
							|  |  |  |             $part      = trim($part); | 
					
						
							|  |  |  |             $direction = 'asc'; | 
					
						
							|  |  |  |             if ('-' === $part[0]) { | 
					
						
							|  |  |  |                 $part      = substr($part, 1); | 
					
						
							|  |  |  |                 $direction = 'desc'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (in_array($part, $this->allowedSort, true)) { | 
					
						
							|  |  |  |                 $sortParameters[] = [$part, $direction]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $bag->set('sort', $sortParameters); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |         return $bag; | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Method to help build URL's. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     final protected function buildParams(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return = '?'; | 
					
						
							|  |  |  |         $params = []; | 
					
						
							|  |  |  |         foreach ($this->parameters as $key => $value) { | 
					
						
							|  |  |  |             if ('page' === $key) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($value instanceof Carbon) { | 
					
						
							|  |  |  |                 $params[$key] = $value->format('Y-m-d'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $params[$key] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return.http_build_query($params); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     final protected function getManager(): Manager | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // create some objects:
 | 
					
						
							|  |  |  |         $manager = new Manager(); | 
					
						
							|  |  |  |         $baseUrl = request()->getSchemeAndHttpHost().'/api/v1'; | 
					
						
							|  |  |  |         $manager->setSerializer(new JsonApiSerializer($baseUrl)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $manager; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     final protected function jsonApiList(string $key, LengthAwarePaginator $paginator, AbstractTransformer $transformer): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $manager  = new Manager(); | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $baseUrl  = sprintf('%s/api/v1/', request()->getSchemeAndHttpHost()); | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // TODO add stuff to path?
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $manager->setSerializer(new JsonApiSerializer($baseUrl)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $objects  = $paginator->getCollection(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // the transformer, at this point, needs to collect information that ALL items in the collection
 | 
					
						
							|  |  |  |         // require, like meta-data and stuff like that, and save it for later.
 | 
					
						
							| 
									
										
										
										
											2025-03-14 19:28:44 +01:00
										 |  |  |         // $objects  = $transformer->collectMetaData($objects);
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  |         $paginator->setCollection($objects); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resource = new FractalCollection($objects, $transformer, $key); | 
					
						
							|  |  |  |         $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $manager->createData($resource)->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns a JSON API object and returns it. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array<int, mixed>|Model $object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     final protected function jsonApiObject(string $key, array|Model $object, AbstractTransformer $transformer): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // create some objects:
 | 
					
						
							|  |  |  |         $manager  = new Manager(); | 
					
						
							|  |  |  |         $baseUrl  = sprintf('%s/api/v1', request()->getSchemeAndHttpHost()); | 
					
						
							|  |  |  |         $manager->setSerializer(new JsonApiSerializer($baseUrl)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 19:28:44 +01:00
										 |  |  |         // $transformer->collectMetaData(new Collection([$object]));
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:34:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resource = new Item($object, $transformer, $key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $manager->createData($resource)->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-04 13:41:42 +01:00
										 |  |  | } |