andrew-gos/class-builder
PHP library to construct objects from arrays or scalars using constructor reflection. Supports interfaces/abstracts via AvailableInheritors, conditional building with BuildIf checkers, typed arrays, variadics, and union/intersection parameter types.
BuildIf, ArrayType) align with your validation/transformation needs.Adopt if:
Look elsewhere if:
jms/serializer or spatie/array-to-object).new Class(...) or array_map).For Executives:
"This package lets us build PHP objects from raw data (like JSON or configs) with zero manual boilerplate. For example, instead of writing a factory class to create a User object from an API response, we’d use this library to auto-map the data into a typed object—saving dev time and reducing bugs. It’s ideal for APIs, settings management, and testing, and aligns with modern PHP’s type system. The tradeoff? A small dependency (MIT-licensed) with minimal runtime overhead."
For Engineers: *"ClassBuilder solves the ‘how do I turn this array into a strongly typed object?’ problem elegantly. Key features:
@AvailableInheritors to resolve interfaces dynamically (e.g., PaymentProcessor → StripeProcessor or PayPalProcessor based on input).int|string), intersections, and typed arrays out of the box.@BuildIf lets you add custom logic (e.g., ‘only build AdminUser if role === ‘admin’’).new ClassBuilder()->build(User::class, 123) for IDs).Use case: Replace this:
$user = new User(
$data['id'],
new Address($data['address']['street']),
...
);
With this:
$user = $builder->build(User::class, $data);
Pros: Cleaner code, fewer bugs, reusable across projects. Cons: Tiny community (1 star), but the code is straightforward to extend if needed. I’d recommend a spike to validate it meets our needs for [specific use case]."*
How can I help you explore Laravel packages today?