media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
uuid
/
src
/
Fields
/
SerializableFieldsTrait.php
SerializableFieldsTrait.php in Media Cloud Sync 1.4.2, at includes/sdk/google/ramsey/uuid/src/Fields/SerializableFieldsTrait.php
| 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 <[email protected]> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Fields; |
| 14 | |
| 15 | use ValueError; |
| 16 | use function base64_decode; |
| 17 | use function sprintf; |
| 18 | use function strlen; |
| 19 | /** |
| 20 | * Provides common serialization functionality to fields |
| 21 | * |
| 22 | * @immutable |
| 23 | */ |
| 24 | trait SerializableFieldsTrait |
| 25 | { |
| 26 | /** |
| 27 | * @param string $bytes The bytes that comprise the fields |
| 28 | */ |
| 29 | public abstract function __construct(string $bytes); |
| 30 | /** |
| 31 | * Returns the bytes that comprise the fields |
| 32 | */ |
| 33 | public abstract function getBytes() : string; |
| 34 | /** |
| 35 | * Returns a string representation of the object |
| 36 | */ |
| 37 | public function serialize() : string |
| 38 | { |
| 39 | return $this->getBytes(); |
| 40 | } |
| 41 | /** |
| 42 | * @return array{bytes: string} |
| 43 | */ |
| 44 | public function __serialize() : array |
| 45 | { |
| 46 | return ['bytes' => $this->getBytes()]; |
| 47 | } |
| 48 | /** |
| 49 | * Constructs the object from a serialized string representation |
| 50 | * |
| 51 | * @param string $data The serialized string representation of the object |
| 52 | */ |
| 53 | public function unserialize(string $data) : void |
| 54 | { |
| 55 | if (strlen($data) === 16) { |
| 56 | $this->__construct($data); |
| 57 | } else { |
| 58 | $this->__construct(base64_decode($data)); |
| 59 | } |
| 60 | } |
| 61 | /** |
| 62 | * @param array{bytes?: string} $data |
| 63 | */ |
| 64 | public function __unserialize(array $data) : void |
| 65 | { |
| 66 | // @codeCoverageIgnoreStart |
| 67 | if (!isset($data['bytes'])) { |
| 68 | throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); |
| 69 | } |
| 70 | // @codeCoverageIgnoreEnd |
| 71 | $this->unserialize($data['bytes']); |
| 72 | } |
| 73 | } |
| 74 |