Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows backward compatible support for the new __serialize and __unserialize methods #107

Merged
merged 2 commits into from
Nov 22, 2021

Conversation

goetas
Copy link
Collaborator

@goetas goetas commented Nov 22, 2021

Allows backward compatible support for the new __serialize and __unserialize methods and plan upgrade path to remove the \Serialize interface in 3.0.

This pull request introduces two new methods serializeToArray and unserializeFromArray to handle in a backward compatible way (php 8.1) the serialization process.

If you were extending the metadata classes, you custom serialization methods were looking probably as something as this:

class MyMetadata extends PropertyMetadata 
{
    // ... more code
    public function serialize()
    {
        $data = parent::serialize();

        return \serialize([$data, $this->moreCustomMetadataProps]);
    }

    public function unserialize($data)
    {
        list($data, $this->moreCustomMetadataProps) = \unserialize($data);

        parent::unserialize($data);
    }
}

After this change, your code should look like this:

class MyMetadata extends PropertyMetadata 
{
    // ... more code
    protected function serializeToArray(): array
    {
        $data = parent::serializeToArray();

        return [$data, $this->moreCustomMetadataProps];
    }

    protected function unserializeFromArray(array $data): void
    {
        list($data, $this->moreCustomMetadataProps) = $data;

        parent::unserializeFromArray($data);
    }
}

Note, this is something you should change only if you plan to upgrade to the upcoming 3.0 version of this package (and plan support for php 9).

…rialize methods and upgrade path to remove the \Serialize interface in 3.0
@alexander-schranz
Copy link
Contributor

alexander-schranz commented Nov 22, 2021

You got a typo in the example in the PR description:

-    protected function serializeToArray():
+    protected function serializeToArray(): array
    {
-        $data = parent::serializeToArray(): array
+        $data = parent::serializeToArray();

Thx for adding this, it makes it easier for us to keep it backward compatible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants