Architecture Fit
The spiral/marshaller package is a lightweight, attribute-driven object-to-array/array-to-object marshaling tool that excels in Laravel ecosystems requiring structured data transformation without heavy dependencies. It aligns with Laravel’s dependency injection, Eloquent, and API resource patterns, offering a declarative alternative to manual serialization (e.g., toArray() methods or Fractal). Key strengths:
#[Marshal]) for zero-boilerplate mapping of public/private properties, enums, and nested objects.DateTimeImmutable, enums) via custom type handlers (e.g., EnumType).delivery_addresses), reducing manual recursion logic.Integration Feasibility
toArray() with Marshaller::marshal($model) for consistent serialization.Illuminate\Http\Resources\Json\Resource or alongside Spatie’s Fractal.unmarshal($request->all(), new UserDto())).spiral/attributes, spiral/core) and Laravel’s service container via bindings.Technical Risk
Illuminate\Validation rules, Illuminate\Http\Request decorators).jms/serializer, manual mapping). Test with production-scale payloads.Key Questions
User->orders->user)?Arrayable)?jms/serializer) to compare?Stack Fit
toArray(): Use Marshaller::marshal($model) in AppServiceProvider or model traits.toArray() to delegate to Marshaller for nested objects.Request input into DTOs (e.g., unmarshal($request->all(), new CreateUserDto())).Illuminate\Validation by unmarshaling validated data into objects.Migration Path
Marshaller::marshal().toArray() implementations for consistency.toArray() methods in critical models/resources (e.g., User, Order).MarshallerDecorator) to customize behavior without modifying core logic.Marshaller to Laravel’s container in AppServiceProvider:
$this->app->singleton(Marshaller::class, fn() => new Marshaller(
new AttributeMapperFactory(new AttributeReader())
));
trait MarshallsAttributes
{
public function toArray(): array
{
return app(Marshaller::class)->marshal($this);
}
}
Compatibility
Sequencing
Marshaller::marshal($object)).toArray() methods.toArray()).User->addresses).Carbon instances).Maintenance
toArray() methods.Support
#[Marshal], #[MarshalArray]).Carbon).Illuminate\Http\Request.Scaling
User profiles).Failure Modes
| Scenario | Impact | Mitigation Strategy |
|---|---|---|
| Invalid input array | Unmarshaling fails silently | Add validation (e.g., Validator::make($data)) before unmarshaling. |
| Circular references | Infinite recursion | Implement a depth limit or use a decorator to detect cycles. |
| Unsupported type | Serialization skips property | Register custom type handlers (e.g., for Carbon). |
| Attribute parsing errors | Runtime exceptions | Use try-catch blocks or decorators to log errors gracefully. |
| PHP 8.1+ incompatibility | Package fails to load | Upgrade PHP version or use a polyfill. |
Ramp-Up
Marshaller-integrated Laravel app.toArray() boilerplate.Monitoring
MarshallerException (e.g., unsupported types).How can I help you explore Laravel packages today?