Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Concerns/TransformableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

trait TransformableData
{
/**
* Transform the data object into an array.
*
* @param null|TransformationContextFactory|TransformationContext $transformationContext
* @return array<string, mixed>
*/
public function transform(
null|TransformationContextFactory|TransformationContext $transformationContext = null,
): array {
Expand All @@ -39,11 +45,21 @@ public function transform(
return $resolver->execute($this, $transformationContext);
}

/**
* Get all data as an array without transforming values.
*
* @return array<string, mixed>
*/
public function all(): array
{
return $this->transform(TransformationContextFactory::create()->withValueTransformation(false));
}

/**
* Get the data object as an array.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return $this->transform();
Expand All @@ -54,6 +70,11 @@ public function toJson($options = 0): string
return json_encode($this->transform(), $options);
}

/**
* Get the data object as an array for JSON serialization.
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return $this->transform();
Expand Down