Skip to content

Commit

Permalink
Merge pull request #1793 from hydephp/automatic-serialization-method
Browse files Browse the repository at this point in the history
Extract helper method for automatic serialization
  • Loading branch information
caendesilva authored Jul 6, 2024
2 parents c2f9d5d + b65f5c3 commit d2c74bf
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 d2c74bf

Please sign in to comment.