-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
677 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Plank\Metable\DataType; | ||
|
||
use Carbon\Carbon; | ||
use Carbon\CarbonImmutable; | ||
use DateTimeInterface; | ||
|
||
/** | ||
* Handle serialization of DateTimeInterface objects. | ||
*/ | ||
class DateTimeImmutableHandler implements HandlerInterface | ||
{ | ||
/** | ||
* The date format to use for serializing. | ||
* | ||
* @var string | ||
*/ | ||
public const FORMAT = 'Y-m-d H:i:s.uO'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDataType(): string | ||
{ | ||
return 'datetime_immutable'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function canHandleValue(mixed $value): bool | ||
{ | ||
return $value instanceof \DateTimeImmutable; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function serializeValue(mixed $value): string | ||
{ | ||
return $value->format(self::FORMAT); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unserializeValue(string $serializedValue): mixed | ||
{ | ||
return CarbonImmutable::createFromFormat(self::FORMAT, $serializedValue); | ||
} | ||
|
||
public function getNumericValue(mixed $value): null|int|float | ||
{ | ||
return $value instanceof DateTimeInterface | ||
? $value->getTimestamp() | ||
: null; | ||
} | ||
|
||
public function getStringValue(mixed $value): null|string | ||
{ | ||
return $value instanceof DateTimeInterface | ||
? $value->copy()->setTimezone('UTC')->format(self::FORMAT) | ||
: null; | ||
} | ||
|
||
public function isIdempotent(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function useHmacVerification(): bool | ||
{ | ||
return false; | ||
} | ||
} |
Oops, something went wrong.