Skip to content

Commit

Permalink
Extract helper method for automatic serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 6, 2024
1 parent c2f9d5d commit b65f5c3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/framework/src/Support/Concerns/Serializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
}
}

0 comments on commit b65f5c3

Please sign in to comment.