From b65f5c3f273e594e321f9e260204f326c919629f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 16:14:30 +0200 Subject: [PATCH] Extract helper method for automatic serialization --- .../framework/src/Support/Concerns/Serializable.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Support/Concerns/Serializable.php b/packages/framework/src/Support/Concerns/Serializable.php index e799278074f..9fc66cb744a 100644 --- a/packages/framework/src/Support/Concerns/Serializable.php +++ b/packages/framework/src/Support/Concerns/Serializable.php @@ -17,9 +17,7 @@ trait Serializable /** 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. - - return get_object_vars(...)->__invoke($this); + return $this->automaticallySerialize(); } /** Recursively serialize Arrayables */ @@ -39,4 +37,12 @@ public function toJson($options = 0): string { return json_encode($this->jsonSerialize(), $options); } + + /** Automatically serialize all public properties. */ + protected function automaticallySerialize(): array + { + // Calling the function from a different scope means we only get the public properties. + + return get_object_vars(...)->__invoke($this); + } }