. */ declare(strict_types=1); namespace FireflyIII\Rules; use Carbon\Carbon; use Carbon\Exceptions\InvalidDateException; use Carbon\Exceptions\InvalidFormatException; use Illuminate\Contracts\Validation\ValidationRule; /** * Class IsDateOrTime */ class IsDateOrTime implements ValidationRule { /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function validate(string $attribute, mixed $value, \Closure $fail): void { $value = (string)$value; if ('' === $value) { $fail('validation.date_or_time')->translate(); return; } if (10 === strlen($value)) { // probably a date format. try { Carbon::createFromFormat('Y-m-d', $value); } catch (InvalidDateException $e) { // @phpstan-ignore-line app('log')->error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage())); $fail('validation.date_or_time')->translate(); return; } catch (InvalidFormatException $e) { // @phpstan-ignore-line app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage())); $fail('validation.date_or_time')->translate(); return; } return; } // is an atom string, I hope? try { Carbon::parse($value); } catch (InvalidDateException $e) { // @phpstan-ignore-line app('log')->error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage())); $fail('validation.date_or_time')->translate(); return; } catch (InvalidFormatException $e) { app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage())); $fail('validation.date_or_time')->translate(); return; } } }