mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-16 16:17:43 +03:00
Update TypeInterface to extend JsonSerializable and Serializable
This commit is contained in:
@@ -14,7 +14,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Type;
|
||||
|
||||
use Ramsey\Uuid\Exception\UnsupportedOperationException;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
use stdClass;
|
||||
|
||||
use function json_decode;
|
||||
use function json_encode;
|
||||
|
||||
/**
|
||||
* A value object representing a timestamp
|
||||
@@ -66,4 +71,41 @@ final class Time implements TypeInterface
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'seconds' => $this->getSeconds()->toString(),
|
||||
'microseconds' => $this->getMicroSeconds()->toString(),
|
||||
];
|
||||
}
|
||||
|
||||
public function serialize(): string
|
||||
{
|
||||
return (string) json_encode($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the object from a serialized string representation
|
||||
*
|
||||
* @param string $serialized The serialized string representation of the object
|
||||
*
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
||||
*/
|
||||
public function unserialize($serialized): void
|
||||
{
|
||||
/** @var stdClass $time */
|
||||
$time = json_decode($serialized);
|
||||
|
||||
if (!isset($time->seconds) || !isset($time->microseconds)) {
|
||||
throw new UnsupportedOperationException(
|
||||
'Attempted to unserialize an invalid value'
|
||||
);
|
||||
}
|
||||
|
||||
$this->__construct($time->seconds, $time->microseconds);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user