Update TypeInterface to extend JsonSerializable and Serializable

This commit is contained in:
Ben Ramsey
2020-03-04 16:44:05 -06:00
parent 158d34625b
commit a21eb6c3ca
10 changed files with 273 additions and 38 deletions
+42
View File
@@ -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);
}
}