2016-01-08 16:00:28 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* Request.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 14:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-01-08 16:00:28 +01:00
|
|
|
|
|
|
|
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;
|
2019-03-31 13:36:49 +02:00
|
|
|
use Exception;
|
2015-02-06 04:39:52 +01:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2019-02-08 07:14:45 +01:00
|
|
|
use Log;
|
2015-02-06 04:39:52 +01:00
|
|
|
|
2016-01-09 08:20:55 +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
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren)
|
2016-01-09 08:20:55 +01:00
|
|
|
*/
|
2017-01-20 19:49:35 +01:00
|
|
|
class Request extends FormRequest
|
2015-02-11 07:35:10 +01:00
|
|
|
{
|
2019-03-31 13:36:49 +02: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;
|
|
|
|
}
|
|
|
|
|
2017-01-21 08:32:23 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Return a boolean value.
|
|
|
|
*
|
2017-01-21 08:32:23 +01:00
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-08-18 14:45:42 +02:00
|
|
|
public function boolean(string $field): bool
|
2017-01-21 08:32:23 +01:00
|
|
|
{
|
2018-07-08 07:59:58 +02:00
|
|
|
if ('true' === (string)$this->input($field)) {
|
2018-04-13 17:28:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-08 07:59:58 +02:00
|
|
|
if ('false' === (string)$this->input($field)) {
|
2018-04-13 17:28:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
return 1 === (int)$this->input($field);
|
2017-01-21 08:32:23 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 16:03:05 +01:00
|
|
|
/**
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-03-31 13:36:49 +02:00
|
|
|
public function convertBoolean(?string $value): bool
|
2018-12-07 16:03:05 +01:00
|
|
|
{
|
2019-03-31 13:36:49 +02:00
|
|
|
if (null === $value) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-07 16:03:05 +01:00
|
|
|
if ('true' === $value) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-31 13:36:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-31 13:36:49 +02:00
|
|
|
/**
|
|
|
|
* @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-06-16 21:47:51 +02:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Return floating value.
|
|
|
|
*
|
2018-06-16 21:47:51 +02:00
|
|
|
* @param string $field
|
|
|
|
*
|
2018-07-27 04:46:21 +02:00
|
|
|
* @return float|null
|
2018-06-16 21:47:51 +02:00
|
|
|
*/
|
2018-07-27 04:46:21 +02:00
|
|
|
public function float(string $field): ?float
|
2018-06-16 21:47:51 +02:00
|
|
|
{
|
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
|
|
|
|
2018-07-27 04:46:21 +02:00
|
|
|
return (float)$res;
|
2018-06-16 21:47:51 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-04-02 15:10:40 +02:00
|
|
|
return (int)$this->get($field);
|
2018-02-09 19:11:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 13:36:49 +02:00
|
|
|
/**
|
|
|
|
* Parse to integer
|
|
|
|
*
|
|
|
|
* @param string|null $string
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function integerFromValue(?string $string): ?int
|
|
|
|
{
|
|
|
|
if (null === $string) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)$string;
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:49:35 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Return string value.
|
|
|
|
*
|
2017-01-20 19:49:35 +01:00
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return string
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
2017-01-20 19:49:35 +01:00
|
|
|
*/
|
2017-08-21 07:13:03 +02:00
|
|
|
public function string(string $field): string
|
2017-01-20 19:49:35 +01:00
|
|
|
{
|
2018-09-27 05:57:06 +02:00
|
|
|
return app('steam')->cleanString((string)($this->get($field) ?? ''));
|
2017-01-20 19:49:35 +01:00
|
|
|
}
|
2017-09-09 06:41:45 +02:00
|
|
|
|
2019-03-31 13:36:49 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
2019-03-31 13:36:49 +02:00
|
|
|
* @throws Exception
|
2017-09-09 06:41:45 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
protected function date(string $field): ?Carbon
|
2017-09-09 06:41:45 +02:00
|
|
|
{
|
|
|
|
return $this->get($field) ? new Carbon($this->get($field)) : null;
|
|
|
|
}
|
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);
|
|
|
|
if (10 === \strlen($value)) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-02-06 04:39:52 +01:00
|
|
|
}
|