Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #42 from riasvdv/main
Browse files Browse the repository at this point in the history
Limit nesting level of augmentation
  • Loading branch information
edalzell authored May 7, 2021
2 parents a653b35 + ce8050c commit 6044b02
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Concerns/AugmentsValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@

trait AugmentsValues
{
protected function getAugmentedValue($data)
protected function getAugmentedValue($data, $level = 0)
{
if ($level > 2) {
return $data;
}

$level++;

if ($data instanceof Carbon) {
return $data;
}

if ($data instanceof JsonSerializable || $data instanceof Collection) {
return $this->getAugmentedValue($data->jsonSerialize());
return $this->getAugmentedValue($data->jsonSerialize(), $level);
}

if (is_array($data)) {
return collect($data)
->map(fn ($value) => $this->getAugmentedValue($value))
->map(fn ($value) => $this->getAugmentedValue($value, $level))
->all();
}

Expand All @@ -30,7 +36,7 @@ protected function getAugmentedValue($data)
}

if (is_object($data) && method_exists($data, 'toAugmentedArray')) {
return $this->getAugmentedValue($data->toAugmentedArray());
return $this->getAugmentedValue($data->toAugmentedArray(), $level);
}

return $data;
Expand Down

0 comments on commit 6044b02

Please sign in to comment.