ramsey / uuid

:snowflake: A PHP library for generating universally unique identifiers (UUIDs).

Home Page:https://uuid.ramsey.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to force Uuid serialize to string instead of bytes ?

e-belair opened this issue · comments

Is there a way to force Uuid serialize to string instead of bytes ?

When I serialize an object containing uuid properties, these uuid values are serialized as bytes.
Exemple produced serialized uuid:

"Application\Entity\CCDL2":2:{s:5:" * id";O:16:"Ramsey\Uuid\Uuid":1:{s:5:"bytes";s:16:"???x?�Nh??X?Y?x";}

I'm getting issues when trying to unserialize data because binary values are stored and unproperly handled by sgbd or unproperly serialized by php. So the better way should be to serialize as string by default as I don't need to store the value as binary.

Before I try to troubleshoot this, what is "sgbd?" I assume it's a database engine, but my search isn't turning up any good leads. Thanks!

Ho, I'm sorry, I used this French acronym by reflex lol, but yeah it means database engine.

Which database engine are you using? Can you provide more details about how you're storing the serialized value? You might need to change the type of column or collation.

I'm using Firebird database. The database uuid are binary uuid by default. The field that stores serialized data is a blob subtype 1 (text blob) as you can get a description here and the default character set is UTF-8. I store a serialized entity in that field, the entity contains some uuids and some of them can't be unserialized. The last error I got was the binary string length was 15 when it should be 16. This database field is used in the app to store temporary data that aims to be moderated by a user. So instead of looking for how to resolve Firebird/PHP issues (that I know there is no support everywhere in the universe for Firebird), maybe the best to do imo could be to configure Uuid to serialize uuid as string by default.

I'm getting a database error in production when serializing uuid

Statement could not be executed (HY000 - -104 - Malformed string )

With an older version of the library, uuids were serialized as string so the customer could load properly the form, but when he tries to save, the new serialization process serialize as bytes and raise the error above.

I wonder if I could use LazyUuidFromString by default in the app ...

Finally I've made a patch to force conversion of Uuid to LazyUuidFromString to ensure serialization:

    protected function checkForLazyUuid(mixed $object): void
    {
        if (is_object($object)) {
            $ref = new ReflectionClass($object);
            $props = $ref->getProperties();
            foreach ($props as $prop) {
                $prop->setAccessible(true);
                $value = $prop->getValue($object);
                if ($value instanceof Uuid) {
                    $prop->setValue($object, new LazyUuidFromString($value->toString()));
                } else {
                    $this->checkForLazyUuid($value);
                }
            }
            return;
        }
        if (is_array($object)) {
            array_walk($object, fn ($v) => $this->checkForLazyUuid($v));
        }
    }