. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers; use Carbon\Carbon; use Carbon\Exceptions\InvalidDateException; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller as BaseController; use Log; use Symfony\Component\HttpFoundation\ParameterBag; /** * Class Controller. * * @codeCoverageIgnore * @SuppressWarnings(PHPMD.NumberOfChildren) */ class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; /** @var ParameterBag Parameters from the URI are stored here. */ protected $parameters; /** * Controller constructor. * */ public function __construct() { // get global parameters $this->parameters = $this->getParameters(); } /** * Method to help build URI's. * * @return string * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ 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 .= http_build_query($params); return $return; } /** * Method to grab all parameters from the URI. * * @return ParameterBag * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function getParameters(): ParameterBag { $bag = new ParameterBag; $page = (int)request()->get('page'); if (0 === $page) { $page = 1; } $bag->set('page', $page); // some date fields: $dates = ['start', 'end', 'date']; foreach ($dates as $field) { $date = request()->query->get($field); $obj = null; if (null !== $date) { try { $obj = new Carbon($date); } catch (InvalidDateException $e) { // don't care Log::error(sprintf('Invalid date exception in API controller: %s', $e->getMessage())); } } $bag->set($field, $obj); } // integer fields: $integers = ['limit']; foreach ($integers as $integer) { $value = request()->query->get($integer); if (null !== $value) { $bag->set($integer, (int)$value); } } return $bag; } }