Files
firefly-iii/app/Api/V2/Controllers/Controller.php

198 lines
6.7 KiB
PHP
Raw Normal View History

2022-06-06 16:41:54 +02:00
<?php
2022-10-16 19:22:26 +02:00
2022-06-06 16:41:54 +02:00
/*
* Controller.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*/
2022-10-16 19:22:26 +02:00
declare(strict_types=1);
2022-06-06 16:41:54 +02:00
namespace FireflyIII\Api\V2\Controllers;
2022-07-21 16:41:28 +02:00
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
2022-06-25 14:23:52 +02:00
use FireflyIII\Transformers\V2\AbstractTransformer;
2022-06-06 16:41:54 +02:00
use Illuminate\Database\Eloquent\Model;
2022-06-25 14:23:52 +02:00
use Illuminate\Pagination\LengthAwarePaginator;
2022-06-06 16:41:54 +02:00
use Illuminate\Routing\Controller as BaseController;
2022-07-21 16:41:28 +02:00
use Illuminate\Support\Collection;
2022-06-06 16:41:54 +02:00
use League\Fractal\Manager;
2022-06-25 14:23:52 +02:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
2022-06-06 16:41:54 +02:00
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\JsonApiSerializer;
2022-07-21 16:41:28 +02:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-01-20 22:08:18 +01:00
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
2022-07-21 16:41:28 +02:00
use Symfony\Component\HttpFoundation\ParameterBag;
2022-06-06 16:41:54 +02:00
/**
* Class Controller
*/
class Controller extends BaseController
{
use ValidatesUserGroupTrait;
2023-12-02 12:56:48 +01:00
protected const string CONTENT_TYPE = 'application/vnd.api+json';
2022-07-21 16:41:28 +02:00
protected ParameterBag $parameters;
2022-06-25 14:23:52 +02:00
/**
*
*/
public function __construct()
{
2023-10-05 19:00:39 +02:00
$this->middleware(
function ($request, $next) {
$this->parameters = $this->getParameters();
return $next($request);
}
);
2022-06-25 14:23:52 +02:00
}
2022-07-21 16:41:28 +02:00
/**
* TODO duplicate from V1 controller
* Method to grab all parameters from the URL.
*
* @return ParameterBag
*/
private function getParameters(): ParameterBag
{
2022-12-29 19:41:57 +01:00
$bag = new ParameterBag();
2023-10-05 19:09:31 +02:00
$bag->set('limit', 50);
2022-07-21 16:41:28 +02:00
try {
2022-12-29 19:41:57 +01:00
$page = (int)request()->get('page');
2023-06-21 12:34:58 +02:00
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
2022-07-21 16:41:28 +02:00
$page = 1;
}
$integers = ['limit', 'administration'];
2022-07-21 16:41:28 +02:00
$dates = ['start', 'end', 'date'];
if ($page < 1) {
$page = 1;
}
if ($page > (2 ^ 16)) {
$page = (2 ^ 16);
}
$bag->set('page', $page);
// some date fields:
foreach ($dates as $field) {
2023-02-12 07:23:57 +01:00
$date = null;
2023-08-09 14:14:33 +02:00
$obj = null;
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
}
2022-07-21 16:41:28 +02:00
if (null !== $date) {
try {
2023-11-15 06:32:41 +01:00
$obj = Carbon::parse((string)$date, config('app.timezone'));
2023-06-21 12:34:58 +02:00
} catch (InvalidDateException | InvalidFormatException $e) {
2022-07-21 16:41:28 +02:00
// don't care
2023-11-15 06:32:41 +01:00
app('log')->warning(sprintf('Ignored invalid date "%s" in API v2 controller parameter check: %s', substr((string)$date, 0, 20), $e->getMessage()));
2022-07-21 16:41:28 +02:00
}
2023-08-09 14:14:33 +02:00
// out of range? set to null.
if (null !== $obj && ($obj->year <= 1900 || $obj->year > 2099)) {
app('log')->warning(sprintf('Refuse to use date "%s" in API v2 controller parameter check: %s', $field, $obj->toAtomString()));
$obj = null;
}
2022-07-21 16:41:28 +02:00
}
$bag->set($field, $obj);
}
// integer fields:
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());
2023-01-20 22:08:18 +01:00
$value = null;
}
2022-07-21 16:41:28 +02:00
if (null !== $value) {
2022-12-29 19:41:57 +01:00
$bag->set($integer, (int)$value);
2022-07-21 16:41:28 +02:00
}
2023-10-05 19:08:17 +02:00
if (null === $value && 'limit' === $integer && auth()->check()) {
2023-10-05 18:52:01 +02:00
// set default for user:
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$bag->set($integer, $pageSize);
}
2022-07-21 16:41:28 +02:00
}
// sort fields:
// return $this->getSortParameters($bag);
return $bag;
}
2023-06-21 12:34:58 +02:00
/**
* @param string $key
* @param LengthAwarePaginator $paginator
* @param AbstractTransformer $transformer
*
* @return array
*/
final protected function jsonApiList(string $key, LengthAwarePaginator $paginator, AbstractTransformer $transformer): array
{
$manager = new Manager();
$baseUrl = request()->getSchemeAndHttpHost() . '/api/v2';
$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.
2023-06-21 12:34:58 +02:00
$transformer->collectMetaData($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 string $key
* @param Model $object
* @param AbstractTransformer $transformer
*
* @return array
*/
final protected function jsonApiObject(string $key, array | Model $object, AbstractTransformer $transformer): array
2023-06-21 12:34:58 +02:00
{
// create some objects:
$manager = new Manager();
$baseUrl = request()->getSchemeAndHttpHost() . '/api/v2';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$transformer->collectMetaData(new Collection([$object]));
$resource = new Item($object, $transformer, $key);
return $manager->createData($resource)->toArray();
}
2022-06-06 16:41:54 +02:00
}