2018-02-04 06:41:42 -06:00
|
|
|
<?php
|
2018-05-11 03:08:34 -05:00
|
|
|
|
2018-02-04 06:41:42 -06:00
|
|
|
/**
|
|
|
|
* Controller.php
|
2020-01-23 12:44:52 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-04 06:41:42 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-04 06:41:42 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 06:41:42 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-04 06:41:42 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-04 06:41:42 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 06:41:42 -06:00
|
|
|
*/
|
|
|
|
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-04 06:41:42 -06:00
|
|
|
namespace FireflyIII\Api\V1\Controllers;
|
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Carbon\Exceptions\InvalidDateException;
|
2018-02-04 06:41:42 -06:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
2019-09-04 10:39:39 -05:00
|
|
|
use League\Fractal\Manager;
|
|
|
|
use League\Fractal\Serializer\JsonApiSerializer;
|
2018-03-26 12:19:11 -05:00
|
|
|
use Log;
|
2018-02-11 13:45:33 -06:00
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
2018-02-04 06:41:42 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Controller.
|
2018-03-10 15:38:20 -06:00
|
|
|
*
|
2018-02-18 03:31:15 -06:00
|
|
|
* @codeCoverageIgnore
|
2018-02-04 06:41:42 -06:00
|
|
|
*/
|
2020-07-31 08:12:26 -05:00
|
|
|
abstract class Controller extends BaseController
|
2018-02-04 06:41:42 -06:00
|
|
|
{
|
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
|
2020-10-04 00:20:48 -05:00
|
|
|
protected const CONTENT_TYPE = 'application/vnd.api+json';
|
2020-10-18 01:01:10 -05:00
|
|
|
protected ParameterBag $parameters;
|
2020-07-31 02:24:08 -05:00
|
|
|
|
2018-02-04 06:41:42 -06:00
|
|
|
/**
|
|
|
|
* Controller constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-02-11 13:45:33 -06:00
|
|
|
// get global parameters
|
|
|
|
$this->parameters = $this->getParameters();
|
2020-08-22 12:27:16 -05:00
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
if (auth()->check()) {
|
|
|
|
$language = app('steam')->getLanguage();
|
|
|
|
app()->setLocale($language);
|
|
|
|
}
|
2020-10-23 12:11:25 -05:00
|
|
|
|
2020-08-22 12:27:16 -05:00
|
|
|
return $next($request);
|
2020-10-23 12:11:25 -05:00
|
|
|
}
|
|
|
|
);
|
2020-08-22 12:27:16 -05:00
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 00:15:42 -05:00
|
|
|
* Method to grab all parameters from the URI.
|
|
|
|
*
|
2018-02-11 13:45:33 -06:00
|
|
|
* @return ParameterBag
|
|
|
|
*/
|
|
|
|
private function getParameters(): ParameterBag
|
|
|
|
{
|
|
|
|
$bag = new ParameterBag;
|
2020-10-23 12:11:25 -05:00
|
|
|
$page = (int)request()->get('page');
|
2018-07-05 14:18:53 -05:00
|
|
|
if (0 === $page) {
|
2018-02-11 13:45:33 -06:00
|
|
|
$page = 1;
|
|
|
|
}
|
|
|
|
$bag->set('page', $page);
|
|
|
|
|
2018-02-13 11:24:06 -06:00
|
|
|
// some date fields:
|
2018-02-16 07:51:59 -06:00
|
|
|
$dates = ['start', 'end', 'date'];
|
|
|
|
foreach ($dates as $field) {
|
2018-12-07 00:41:12 -06:00
|
|
|
$date = request()->query->get($field);
|
2018-02-16 07:51:59 -06:00
|
|
|
$obj = null;
|
2018-04-02 07:17:11 -05:00
|
|
|
if (null !== $date) {
|
2018-02-13 11:24:06 -06:00
|
|
|
try {
|
2019-02-08 00:14:45 -06:00
|
|
|
$obj = Carbon::parse($date);
|
2018-02-13 11:24:06 -06:00
|
|
|
} catch (InvalidDateException $e) {
|
|
|
|
// don't care
|
2018-03-26 12:19:11 -05:00
|
|
|
Log::error(sprintf('Invalid date exception in API controller: %s', $e->getMessage()));
|
2018-02-13 11:24:06 -06:00
|
|
|
}
|
2018-02-11 13:45:33 -06:00
|
|
|
}
|
2018-02-13 11:24:06 -06:00
|
|
|
$bag->set($field, $obj);
|
2018-02-11 13:45:33 -06:00
|
|
|
}
|
|
|
|
|
2019-01-26 05:09:46 -06:00
|
|
|
// integer fields:
|
|
|
|
$integers = ['limit'];
|
|
|
|
foreach ($integers as $integer) {
|
|
|
|
$value = request()->query->get($integer);
|
|
|
|
if (null !== $value) {
|
2020-10-23 12:11:25 -05:00
|
|
|
$bag->set($integer, (int)$value);
|
2019-01-26 05:09:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
return $bag;
|
|
|
|
|
2018-02-04 06:41:42 -06:00
|
|
|
}
|
2020-10-18 01:01:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to help build URI's.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-23 12:11:25 -05:00
|
|
|
return $return . http_build_query($params);
|
2020-10-18 01:01:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Manager
|
|
|
|
*/
|
|
|
|
final protected function getManager(): Manager
|
|
|
|
{
|
|
|
|
// create some objects:
|
|
|
|
$manager = new Manager;
|
|
|
|
$baseUrl = request()->getSchemeAndHttpHost() . '/api/v1';
|
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
|
|
|
|
|
|
|
return $manager;
|
|
|
|
}
|
2018-02-04 06:41:42 -06:00
|
|
|
}
|