. */ 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 */ class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; /** @var ParameterBag */ protected $parameters; /** * Controller constructor. * */ public function __construct() { // get global parameters $this->parameters = $this->getParameters(); } /** * @return string */ protected function buildParams(): string { $return = '?'; $params = []; foreach ($this->parameters as $key => $value) { if ($key === 'page') { continue; } if ($value instanceof Carbon) { $params[$key] = $value->format('Y-m-d'); } if (!$value instanceof Carbon) { $params[$key] = $value; } } $return .= http_build_query($params); if (\strlen($return) === 1) { return ''; } return $return; } /** * @return ParameterBag */ private function getParameters(): ParameterBag { $bag = new ParameterBag; $page = (int)request()->get('page'); if ($page === 0) { $page = 1; } $bag->set('page', $page); // some date fields: $dates = ['start', 'end', 'date']; foreach ($dates as $field) { $date = request()->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); } return $bag; } }