From ec61726fe502a420182f68e661991d472622fbd5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:48:33 +0200 Subject: [PATCH 01/13] Remove abstract modifier from Serializable toArray method --- packages/framework/src/Support/Concerns/Serializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index f76c123f52a..b3ab16e1c72 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -15,7 +15,7 @@ trait Serializable { /** @inheritDoc */ - abstract public function toArray(): array; + public function toArray(): array; /** Recursively serialize Arrayables */ public function arraySerialize(): array From 1b809e0f523d09313e2cd87837e21545f6ad81e8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:48:57 +0200 Subject: [PATCH 02/13] Add an automatically serializable test class --- .../framework/tests/Unit/SerializableTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/framework/tests/Unit/SerializableTest.php b/packages/framework/tests/Unit/SerializableTest.php index fde100a2c88..528990ae011 100644 --- a/packages/framework/tests/Unit/SerializableTest.php +++ b/packages/framework/tests/Unit/SerializableTest.php @@ -82,3 +82,24 @@ public function toArray(): array return ['foo' => 'bar']; } } + +class AutomaticallySerializableTestClass implements SerializableContract +{ + use Serializable; + + public string $foo; + public string $bar; + public array $baz; + + protected string $hidden; + private string $private; + + public function __construct() + { + $this->foo = 'foo'; + $this->bar = 'bar'; + $this->baz = ['baz' => 'baz']; + $this->hidden = 'hidden'; + $this->private = 'private'; + } +} From 6da5fb82fc444b6f031686141fbf9077d45bbeb2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:49:09 +0200 Subject: [PATCH 03/13] Draft method body --- packages/framework/src/Support/Concerns/Serializable.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index b3ab16e1c72..7a531445b5e 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -15,7 +15,10 @@ trait Serializable { /** @inheritDoc */ - public function toArray(): array; + public function toArray(): array + { + // + } /** Recursively serialize Arrayables */ public function arraySerialize(): array From 319d729447813d89001d4c5fd15dda4a38186b7e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:50:37 +0200 Subject: [PATCH 04/13] Draft method implementation --- packages/framework/src/Support/Concerns/Serializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index 7a531445b5e..6e7028ec20d 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -17,7 +17,7 @@ trait Serializable /** @inheritDoc */ public function toArray(): array { - // + return collect($this)->toArray(); } /** Recursively serialize Arrayables */ From f30f3618091c328141435138f34eedce559bb6d6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:50:40 +0200 Subject: [PATCH 05/13] Revert "Draft method implementation" This reverts commit 319d729447813d89001d4c5fd15dda4a38186b7e as it leads to a recursion loop by the collect call --- packages/framework/src/Support/Concerns/Serializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index 6e7028ec20d..7a531445b5e 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -17,7 +17,7 @@ trait Serializable /** @inheritDoc */ public function toArray(): array { - return collect($this)->toArray(); + // } /** Recursively serialize Arrayables */ From 4a13f46338411057f3455290d716cd6d7dfe69df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:51:11 +0200 Subject: [PATCH 06/13] Test automatically serialization --- packages/framework/tests/Unit/SerializableTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/framework/tests/Unit/SerializableTest.php b/packages/framework/tests/Unit/SerializableTest.php index 528990ae011..01f5fcc12ae 100644 --- a/packages/framework/tests/Unit/SerializableTest.php +++ b/packages/framework/tests/Unit/SerializableTest.php @@ -53,6 +53,15 @@ public function testToJsonWithArrayable() { $this->assertSame('{"foo":"bar","arrayable":{"foo":"bar"}}', (new SerializableTestClassWithArrayable)->toJson()); } + + public function testAutomaticallySerialization() + { + $this->assertSame([ + 'foo' => 'foo', + 'bar' => 'bar', + 'baz' => ['baz' => 'baz'], + ], (new AutomaticallySerializableTestClass)->toArray()); + } } class SerializableTestClass implements SerializableContract From e747b016566b92f1fd9d97c1d891e2f62972e516 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:55:35 +0200 Subject: [PATCH 07/13] Add static property to test class --- packages/framework/tests/Unit/SerializableTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/tests/Unit/SerializableTest.php b/packages/framework/tests/Unit/SerializableTest.php index 01f5fcc12ae..3f1dfb79a93 100644 --- a/packages/framework/tests/Unit/SerializableTest.php +++ b/packages/framework/tests/Unit/SerializableTest.php @@ -103,6 +103,8 @@ class AutomaticallySerializableTestClass implements SerializableContract protected string $hidden; private string $private; + public static string $static; + public function __construct() { $this->foo = 'foo'; @@ -110,5 +112,6 @@ public function __construct() $this->baz = ['baz' => 'baz']; $this->hidden = 'hidden'; $this->private = 'private'; + static::$static = 'static'; } } From 3f7f8d8de6cb1496e3111028b03b6ea9a1a2ff0e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:57:06 +0200 Subject: [PATCH 08/13] Add uninitialized property to test class --- packages/framework/tests/Unit/SerializableTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/tests/Unit/SerializableTest.php b/packages/framework/tests/Unit/SerializableTest.php index 3f1dfb79a93..021f6a4a246 100644 --- a/packages/framework/tests/Unit/SerializableTest.php +++ b/packages/framework/tests/Unit/SerializableTest.php @@ -100,6 +100,8 @@ class AutomaticallySerializableTestClass implements SerializableContract public string $bar; public array $baz; + public string $uninitialized; + protected string $hidden; private string $private; From c99baa52905e79057b8a7f91dccabbbadad79625 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 15:59:23 +0200 Subject: [PATCH 09/13] Return `get_object_vars()` --- packages/framework/src/Support/Concerns/Serializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index 7a531445b5e..d66d7e5b9de 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -17,7 +17,7 @@ trait Serializable /** @inheritDoc */ public function toArray(): array { - // + return get_object_vars($this); } /** Recursively serialize Arrayables */ From 827a89bd9c2535106923d038d007915c7fe8d263 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 16:01:56 +0200 Subject: [PATCH 10/13] Call `get_object_vars()` from a closure scope This lets us get only the public properties --- packages/framework/src/Support/Concerns/Serializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index d66d7e5b9de..571f2162e42 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -17,7 +17,7 @@ trait Serializable /** @inheritDoc */ public function toArray(): array { - return get_object_vars($this); + return get_object_vars(...)->__invoke($this); } /** Recursively serialize Arrayables */ From 6196fa77660391b3512c9878956342848b87bfeb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 16:03:56 +0200 Subject: [PATCH 11/13] Document why we call from a different scope --- packages/framework/src/Support/Concerns/Serializable.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index 571f2162e42..b90c85244e5 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -17,6 +17,8 @@ trait Serializable /** @inheritDoc */ public function toArray(): array { + // Calling the function from a different scope means we only get the public properties. + return get_object_vars(...)->__invoke($this); } From ed13490b1f557b19fa2d2d3be8f9974e032d61c6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 16:05:58 +0200 Subject: [PATCH 12/13] Add new method documentation --- packages/framework/src/Support/Concerns/Serializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index b90c85244e5..e799278074f 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -14,7 +14,7 @@ */ trait Serializable { - /** @inheritDoc */ + /** Default implementation to dynamically serialize all public properties. Can be overridden for increased control. */ public function toArray(): array { // Calling the function from a different scope means we only get the public properties. From 9022c2ae7c0dee07550d805a718a6dbd15199a36 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 16:09:09 +0200 Subject: [PATCH 13/13] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 63d1942a640..d15863f1304 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -13,7 +13,7 @@ This serves two purposes: - Updated the `HydeKernel` array representation to include the Hyde version in https://github.com/hydephp/develop/pull/1786 ### Changed -- for changes in existing functionality. +- Updated the `Serializable` trait to provide a default automatic `toArray` method in https://github.com/hydephp/develop/pull/1791 ### Deprecated - for soon-to-be removed features.