Files
firefly-iii/app/Http/Requests/Request.php

441 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* Request.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* 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.
2017-10-21 08:40:00 +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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Requests;
2015-02-06 04:39:52 +01:00
2017-01-21 08:32:23 +01:00
use Carbon\Carbon;
2019-02-08 07:14:45 +01:00
use Carbon\Exceptions\InvalidDateException;
use Exception;
2015-02-06 04:39:52 +01:00
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
2019-02-08 07:14:45 +01:00
use Log;
2015-02-06 04:39:52 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class Request.
2018-12-07 16:03:05 +01:00
*
2018-09-15 13:44:36 +02:00
* @codeCoverageIgnore
2018-07-20 14:34:56 +02:00
*
*/
class Request extends FormRequest
2015-02-11 07:35:10 +01:00
{
/**
* @param $array
*
* @return array|null
*/
public function arrayFromValue($array): ?array
{
if (is_array($array)) {
return $array;
}
if (null === $array) {
return null;
}
if (is_string($array)) {
return explode(',', $array);
}
return null;
}
2018-12-07 16:03:05 +01:00
/**
* @param string $value
*
* @return bool
*/
public function convertBoolean(?string $value): bool
2018-12-07 16:03:05 +01:00
{
if (null === $value) {
return false;
}
2018-12-07 16:03:05 +01:00
if ('true' === $value) {
return true;
}
if ('yes' === $value) {
return true;
}
2018-12-07 16:03:05 +01:00
if (1 === $value) {
return true;
}
if ('1' === $value) {
return true;
}
if (true === $value) {
return true;
}
return false;
}
/**
* @param string|null $string
*
* @return Carbon|null
*/
public function dateFromValue(?string $string): ?Carbon
{
if (null === $string) {
return null;
}
if ('' === $string) {
return null;
}
try {
$carbon = new Carbon($string);
} catch (Exception $e) {
Log::debug(sprintf('Invalid date: %s: %s', $string, $e->getMessage()));
return null;
}
return $carbon;
}
/**
2018-07-22 08:10:16 +02:00
* Return floating value.
*
* @param string $field
*
2018-07-27 04:46:21 +02:00
* @return float|null
*/
2018-07-27 04:46:21 +02:00
public function float(string $field): ?float
{
2018-07-27 04:46:21 +02:00
$res = $this->get($field);
2018-08-06 19:14:30 +02:00
if (null === $res) {
2018-07-27 04:46:21 +02:00
return null;
}
2018-08-06 19:14:30 +02:00
return (float) $res;
}
2018-02-09 19:11:55 +01:00
/**
2018-07-22 08:10:16 +02:00
* Return integer value.
*
2018-02-09 19:11:55 +01:00
* @param string $field
*
* @return int
*/
public function integer(string $field): int
{
return (int) $this->get($field);
2018-02-09 19:11:55 +01:00
}
/**
* Parse to integer
*
* @param string|null $string
*
* @return int|null
*/
public function integerFromValue(?string $string): ?int
{
if (null === $string) {
return null;
}
if ('' === $string) {
return null;
}
return (int) $string;
}
/**
* Return string value, but keep newlines.
*
* @param string $field
*
* @return string
*/
public function nlString(string $field): string
{
return app('steam')->nlCleanString((string) ($this->get($field) ?? ''));
}
/**
* Parse and clean a string, but keep the newlines.
*
* @param string|null $string
*
* @return string|null
*/
public function nlStringFromValue(?string $string): ?string
{
if (null === $string) {
return null;
}
$result = app('steam')->nlCleanString($string);
return '' === $result ? null : $result;
}
/**
* Return integer value, or NULL when it's not set.
*
* @param string $field
*
* @return int|null
*/
public function nullableInteger(string $field): ?int
{
if (!$this->has($field)) {
return null;
}
$value = (string) $this->get($field);
if ('' === $value) {
2020-03-15 18:05:24 +01:00
return null;
}
return (int) $value;
}
/**
* Return string value, but keep newlines, or NULL if empty.
2018-07-22 08:10:16 +02:00
*
* @param string $field
*
* @return string
*/
public function nullableNlString(string $field): ?string
{
if (!$this->has($field)) {
return null;
}
return app('steam')->nlCleanString((string) ($this->get($field) ?? ''));
}
2017-09-09 06:41:45 +02:00
2019-09-20 06:14:08 +02:00
/**
* Return string value, or NULL if empty.
2019-09-20 06:14:08 +02:00
*
* @param string $field
*
* @return string|null
2019-09-20 06:14:08 +02:00
*/
public function nullableString(string $field): ?string
2019-09-20 06:14:08 +02:00
{
if (!$this->has($field)) {
return null;
}
$res = trim(app('steam')->cleanString((string) ($this->get($field) ?? '')));
if ('' === $res) {
return null;
}
2019-09-20 06:14:08 +02:00
return $res;
}
2019-09-20 06:14:08 +02:00
/**
* Return string value.
*
* @param string $field
*
* @return string
*/
public function string(string $field): string
{
return app('steam')->cleanString((string) ($this->get($field) ?? ''));
}
/**
* Parse and clean a string.
*
* @param string|null $string
*
* @return string|null
*/
public function stringFromValue(?string $string): ?string
{
if (null === $string) {
return null;
}
$result = app('steam')->cleanString($string);
return '' === $result ? null : $result;
}
2019-09-20 06:14:08 +02:00
/**
* Read the submitted Request data and add new or updated Location data to the array.
2019-09-20 06:14:08 +02:00
*
* @param array $data
2019-09-20 06:14:08 +02:00
*
* @param string|null $prefix
*
* @return array
2019-09-20 06:14:08 +02:00
*/
protected function appendLocationData(array $data, ?string $prefix): array
2019-09-20 06:14:08 +02:00
{
Log::debug(sprintf('Now in appendLocationData("%s")', $prefix), $data);
$data['store_location'] = false;
$data['update_location'] = false;
$data['longitude'] = null;
$data['latitude'] = null;
$data['zoom_level'] = null;
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
$latitudeKey = $this->getLocationKey($prefix, 'latitude');
$zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
$hasLocationKey = $this->getLocationKey($prefix, 'has_location');
// for a POST (store, all fields must be present and accounted for:
if (
('POST' === $this->method() && $this->routeIs('*.store'))
&& ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey))
) {
Log::debug('Method is POST and all fields present.');
$data['store_location'] = $this->boolean($hasLocationKey);
$data['longitude'] = $this->nullableString($longitudeKey);
$data['latitude'] = $this->nullableString($latitudeKey);
$data['zoom_level'] = $this->nullableString($zoomLevelKey);
}
if (
($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey))
&& (
('PUT' === $this->method() && $this->routeIs('*.update'))
|| ('POST' === $this->method() && $this->routeIs('*.update'))
)
) {
Log::debug('Method is PUT and all fields present.');
$data['update_location'] = true;
$data['longitude'] = $this->nullableString($longitudeKey);
$data['latitude'] = $this->nullableString($latitudeKey);
$data['zoom_level'] = $this->nullableString($zoomLevelKey);
}
if (null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) {
Log::debug('One of the fields is NULL, wont save.');
$data['store_location'] = false;
$data['update_location'] = false;
2019-09-20 06:14:08 +02:00
}
Log::debug(sprintf('Returning longitude: "%s", latitude: "%s", zoom level: "%s"', $data['longitude'], $data['latitude'], $data['zoom_level']));
2019-09-20 06:14:08 +02:00
return $data;
2019-09-20 06:14:08 +02:00
}
2017-09-09 06:41:45 +02:00
/**
2018-07-22 08:10:16 +02:00
* Return date or NULL.
*
2017-09-09 06:41:45 +02:00
* @param string $field
*
* @return Carbon|null
*/
2018-07-08 12:28:42 +02:00
protected function date(string $field): ?Carbon
2017-09-09 06:41:45 +02:00
{
$result = null;
try {
$result = $this->get($field) ? new Carbon($this->get($field)) : null;
} catch (Exception $e) {
Log::debug(sprintf('Exception when parsing date. Not interesting: %s', $e->getMessage()));
}
return $result;
2017-09-09 06:41:45 +02:00
}
2019-02-08 07:14:45 +01:00
/**
* Return date time or NULL.
*
* @param string $field
*
* @return Carbon|null
*/
protected function dateTime(string $field): ?Carbon
{
if (null === $this->get($field)) {
return null;
}
$value = (string) $this->get($field);
2019-06-07 17:58:11 +02:00
if (10 === strlen($value)) {
2019-02-08 07:14:45 +01:00
// probably a date format.
try {
$result = Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
return null;
}
return $result;
}
// is an atom string, I hope?
try {
$result = Carbon::parse($value);
} catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
return null;
}
return $result;
}
/**
* @param Validator $validator
*/
protected function validateAutoBudgetAmount(Validator $validator): void
{
2020-03-14 07:30:55 +01:00
$data = $validator->getData();
$type = $data['auto_budget_type'] ?? '';
$amount = $data['auto_budget_amount'] ?? '';
$period = (string) ($data['auto_budget_period'] ?? '');
2020-03-14 07:30:55 +01:00
$currencyId = $data['auto_budget_currency_id'] ?? '';
$currencyCode = $data['auto_budget_currency_code'] ?? '';
if (is_numeric($type)) {
$type = (int) $type;
2020-03-14 07:30:55 +01:00
}
if (0 === $type || 'none' === $type || '' === $type) {
2020-03-14 08:03:43 +01:00
return;
}
// basic float check:
if ('' === $amount) {
$validator->errors()->add('auto_budget_amount', (string) trans('validation.amount_required_for_auto_budget'));
2020-03-14 08:03:43 +01:00
}
if (1 !== bccomp((string) $amount, '0')) {
$validator->errors()->add('auto_budget_amount', (string) trans('validation.auto_budget_amount_positive'));
2020-03-14 08:03:43 +01:00
}
if ('' === $period) {
$validator->errors()->add('auto_budget_period', (string) trans('validation.auto_budget_period_mandatory'));
2020-03-14 08:03:43 +01:00
}
if ('' === $currencyCode && '' === $currencyId) {
$validator->errors()->add('auto_budget_amount', (string) trans('validation.require_currency_info'));
}
}
2020-03-15 18:05:24 +01:00
/**
* @param string|null $prefix
* @param string $key
*
* @return string
*/
private function getLocationKey(?string $prefix, string $key): string
{
if (null === $prefix) {
return $key;
}
return sprintf('%s_%s', $prefix, $key);
}
2020-03-14 07:30:55 +01:00
2015-02-06 04:39:52 +01:00
}