HasToArray
Reflection-based toArray() trait for DTOs implementing Arrayable.
Signature
php
<?php
namespace Studiometa\Foehn\Concerns;
trait HasToArray
{
public function toArray(): array;
protected function propertyToKey(string $name): string;
}Behavior
- Reads all public instance properties via reflection
- Converts camelCase names to snake_case keys (e.g.,
backgroundImage→background_image) - Recursively flattens nested
Arrayableobjects - Recursively flattens
Arrayableitems in arrays - Skips uninitialized and static properties
Customization
Override propertyToKey() to change the key mapping:
php
final readonly class MyContext implements Arrayable
{
use HasToArray;
public function __construct(
public string $firstName,
) {}
// Keep camelCase keys
protected function propertyToKey(string $name): string
{
return $name;
}
}
(new MyContext('John'))->toArray();
// ['firstName' => 'John'] instead of ['first_name' => 'John']