This commit is contained in:
James Cole
2023-05-17 07:02:08 +02:00
parent 3c082dcf0e
commit 63984f1c37
14 changed files with 68 additions and 21 deletions

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Request;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Support\Facades\Log;
@@ -255,6 +256,44 @@ trait ConvertsDataTypes
return $carbon;
}
protected function convertDateTime(?string $string): ?Carbon
{
$value = $this->get($string);
if (null === $value) {
return null;
}
if ('' === $value) {
return null;
}
if (10 === strlen($value)) {
// probably a date format.
try {
$carbon = Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) {
Log::error(sprintf('[1] "%s" is not a valid date: %s', $value, $e->getMessage()));
return null;
} catch (InvalidFormatException $e) {
Log::error(sprintf('[2] "%s" is of an invalid format: %s', $value, $e->getMessage()));
return null;
}
return $carbon;
}
// is an atom string, I hope?
try {
$carbon = Carbon::parse($value);
} catch (InvalidDateException $e) {
Log::error(sprintf('[3] "%s" is not a valid date or time: %s', $value, $e->getMessage()));
return null;
} catch (InvalidFormatException $e) {
Log::error(sprintf('[4] "%s" is of an invalid format: %s', $value, $e->getMessage()));
return null;
}
return $carbon;
}
/**
* Returns all data in the request, or omits the field if not set,
* according to the config from the request. This is the way.