-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make to array return same case as properties
- Loading branch information
1 parent
7402a28
commit e2b94b2
Showing
3 changed files
with
48 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Jasara\AmznSPA\Data\Base; | ||
|
||
use Illuminate\Support\Collection; | ||
use ReflectionClass; | ||
use ReflectionProperty; | ||
|
||
trait ToArray | ||
{ | ||
public function toArray(): array | ||
{ | ||
$class = new ReflectionClass(static::class); | ||
|
||
$data = []; | ||
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { | ||
$value = $property->getValue($this); | ||
if ($value instanceof Collection) { | ||
$value = $value->map(fn ($item) => $item->toArray())->toArray(); | ||
} | ||
|
||
$data[$property->getName()] = $value; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters