fix: Fix conversioni date quando si usa il formato standard ISO8601

This commit is contained in:
Maicol Battistini 2023-07-25 09:59:24 +02:00
parent f230f5954d
commit d98718cedc
No known key found for this signature in database
2 changed files with 6 additions and 3 deletions

View File

@ -23,6 +23,7 @@ use Illuminate\Support\Str;
use function in_array; use function in_array;
use function is_array; use function is_array;
use Nette\Utils\Json; use Nette\Utils\Json;
use function is_string;
/** /**
* @phpstan-type MatchType 'text'|'string'|'bool'|'int'|'integer'|'datetime'|'between'|'array' * @phpstan-type MatchType 'text'|'string'|'bool'|'int'|'integer'|'datetime'|'between'|'array'
@ -356,7 +357,7 @@ abstract class Repository extends RestifyRepository
// Fix dates (JSONAPI uses ISO 8601, DB uses Y-m-d H:i:s) // Fix dates (JSONAPI uses ISO 8601, DB uses Y-m-d H:i:s)
$attributes = array_map( $attributes = array_map(
static function ($value) { static function ($value) {
if (is_string($value) && Carbon::hasFormat($value, 'Y-m-d\TH:i:sP')) { if (is_string($value) && (Carbon::hasFormat($value, 'Y-m-d\TH:i:sP') || Carbon::hasFormat($value, 'Y-m-d\TH:i:s.v\Z'))) {
try { try {
return Carbon::parse($value)->format('Y-m-d H:i:s'); return Carbon::parse($value)->format('Y-m-d H:i:s');
} catch (InvalidFormatException) { } catch (InvalidFormatException) {

View File

@ -82,9 +82,11 @@ export default abstract class Model<A extends ModelAttributes, R extends ModelRe
setAttribute<AN extends keyof A = keyof A>(attributeName: AN, value: ValueOf<A, AN>) { setAttribute<AN extends keyof A = keyof A>(attributeName: AN, value: ValueOf<A, AN>) {
const date = dayjs(value as string | Date | undefined); const date = dayjs(value as string | Date | undefined);
// @ts-expect-error // @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
if (this.isDateAttribute(attributeName) && date.isValid()) { if (this.isDateAttribute(attributeName) && date.isValid()) {
// @ts-expect-error const format = this.constructor.dates[attributeName as string];
value = date.format((this as Model<any>).constructor.dates[attributeName]); // eslint-disable-next-line no-param-reassign
value = (format === 'YYYY-MM-DDTHH:mm:ss.ssssssZ' ? date.toISOString() : date.format(format)) as ValueOf<A, AN>;
} }
// @ts-expect-error — This is needed to parse the dates correctly. // @ts-expect-error — This is needed to parse the dates correctly.
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access