From b517fe4e2a15a8a5c92d5980b1d47eecc68f9f8b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 8 Dec 2021 09:06:58 +0100 Subject: [PATCH] Bugfix/php8.1 deprecations (#19) --- .github/workflows/build.yaml | 5 ++++- src/Shortid.php | 21 +++++++++++++++++---- tests/ShortidTest.php | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 89502c0..e79558c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -36,6 +36,7 @@ jobs: - '7.3' - '7.4' - '8.0' + - '8.1' include: - description: 'lowest' php: '7.1' @@ -46,8 +47,10 @@ jobs: php: '7.3' - description: '7.4' php: '7.4' - - description: 'latest' + - description: '8.0' php: '8.0' + - description: 'latest' + php: '8.1' name: PHP ${{ matrix.php }} tests steps: - name: Checkout diff --git a/src/Shortid.php b/src/Shortid.php index 02a74fe..78fb472 100644 --- a/src/Shortid.php +++ b/src/Shortid.php @@ -58,10 +58,7 @@ public static function isValid(string $value, int $length = null, string $alphab return $ok > 0 && \strlen($matches[0]) === $length; } - /** - * @return string - */ - public function jsonSerialize() + public function jsonSerialize(): string { return $this->id; } @@ -78,4 +75,20 @@ public function unserialize($serialized): void { $this->id = $serialized; } + + /** + * @return array{id: string} + */ + public function __serialize(): array + { + return ['id' => $this->id]; + } + + /** + * @param array{id: string} $serialized + */ + public function __unserialize(array $serialized): void + { + $this->id = $serialized['id']; + } } diff --git a/tests/ShortidTest.php b/tests/ShortidTest.php index e58a6f6..9dc4eeb 100644 --- a/tests/ShortidTest.php +++ b/tests/ShortidTest.php @@ -98,4 +98,19 @@ public function testUnserialize(): void $this->assertSame('shortid', (string) $shortid); } + + public function testMagicSerialize(): void + { + $shortid = new Shortid('shortid'); + + $this->assertSame(['id' => 'shortid'], $shortid->__serialize()); + } + + public function testMagicUnserialize(): void + { + $shortid = Shortid::generate(); + $shortid->__unserialize(['id' => 'shortid']); + + $this->assertSame('shortid', (string) $shortid); + } }