Skip to content

Commit

Permalink
Fix deprecated Serializable usage (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Aug 28, 2024
1 parent b1f7c33 commit 67de830
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions tests/ArrayCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Serializable;

use function json_decode;
use function json_encode;
use function serialize;
use function unserialize;

use const JSON_THROW_ON_ERROR;

/**
* Tests for {@see \Doctrine\Common\Collections\ArrayCollection}.
*
Expand Down Expand Up @@ -44,19 +39,23 @@ public function testUnserializeEmptyArrayCollection(): void
}

/**
* We can't implement Serializable interface on anonymous class
* @template TKey of array-key
* @template TValue
* @extends ArrayCollection<TKey, TValue>
*/
class SerializableArrayCollection extends ArrayCollection implements Serializable
class SerializableArrayCollection extends ArrayCollection
{
public function serialize(): string
/** @return array<TKey, TValue> */
public function __serialize(): array
{
return json_encode($this->getKeys(), JSON_THROW_ON_ERROR);
return $this->toArray();
}

public function unserialize(string $serialized): void
/** @param array<TKey, TValue> $data */
public function __unserialize(array $data): void
{
foreach (json_decode(json: $serialized, flags: JSON_THROW_ON_ERROR) as $value) {
parent::add($value);
foreach ($data as $key => $value) {
$this->set($key, $value);
}
}
}

0 comments on commit 67de830

Please sign in to comment.