1<?php 2 3/** 4 * This file is part of the ramsey/uuid library 5 * 6 * For the full copyright and license information, please view the LICENSE 7 * file that was distributed with this source code. 8 * 9 * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> 10 * @license http://opensource.org/licenses/MIT MIT 11 */ 12 13declare(strict_types=1); 14 15namespace Ramsey\Uuid\Fields; 16 17use function base64_decode; 18use function strlen; 19 20/** 21 * Provides common serialization functionality to fields 22 * 23 * @psalm-immutable 24 */ 25trait SerializableFieldsTrait 26{ 27 /** 28 * @param string $bytes The bytes that comprise the fields 29 */ 30 abstract public function __construct(string $bytes); 31 32 /** 33 * Returns the bytes that comprise the fields 34 */ 35 abstract public function getBytes(): string; 36 37 /** 38 * Returns a string representation of object 39 */ 40 public function serialize(): string 41 { 42 return $this->getBytes(); 43 } 44 45 /** 46 * Constructs the object from a serialized string representation 47 * 48 * @param string $serialized The serialized string representation of the object 49 * 50 * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint 51 */ 52 public function unserialize($serialized): void 53 { 54 if (strlen($serialized) === 16) { 55 $this->__construct($serialized); 56 } else { 57 $this->__construct(base64_decode($serialized)); 58 } 59 } 60} 61