Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Class Builder Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Dynamic Object Construction: Enables rapid prototyping and runtime object creation from structured data (e.g., APIs, configs, or user inputs), reducing boilerplate code for DTOs, entities, or service objects.
  • Type Safety & Flexibility: Supports complex type hints (unions, intersections, arrays) and abstract interfaces, aligning with modern PHP (8.1+) practices while avoiding manual instantiation logic.
  • Build vs. Buy: Justifies buying this package over custom solutions if:
    • Your team lacks time/resources to build a robust object mapper.
    • You need runtime polymorphism (e.g., resolving interfaces to concrete classes dynamically).
    • The package’s attributes (e.g., BuildIf, ArrayType) align with your validation/transformation needs.
  • Use Cases:
    • API/ORM Layers: Convert JSON/XML to typed PHP objects (e.g., Laravel API responses → Eloquent models).
    • Configuration Systems: Load app settings from YAML/JSON into strongly typed classes.
    • Testing: Generate test data for complex objects without hardcoding factories.
    • Legacy Systems: Retrofit type safety to older codebases by wrapping constructor logic.

When to Consider This Package

Adopt if:

  • You’re using PHP 8.1+ (for attributes, unions, and intersections).
  • Your objects have complex constructors (nested types, abstract classes, or variadic params).
  • You need runtime flexibility (e.g., resolving interfaces based on input data).
  • Your team prioritizes developer velocity over micro-optimizations (this isn’t a high-performance library).

Look elsewhere if:

  • You need high-performance serialization (e.g., for bulk data processing; consider jms/serializer or spatie/array-to-object).
  • Your objects are simple (use native new Class(...) or array_map).
  • You require deep nesting support (e.g., recursive object graphs; this package focuses on single-level construction).
  • Your project uses strict dependency injection (e.g., Laravel’s container) where manual instantiation is discouraged.
  • The MIT license conflicts with your open-source policy (though rare, some orgs avoid permissive licenses).

How to Pitch It (Stakeholders)

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:

  • Attributes for control: Use @AvailableInheritors to resolve interfaces dynamically (e.g., PaymentProcessorStripeProcessor or PayPalProcessor based on input).
  • Type safety: Handles unions (int|string), intersections, and typed arrays out of the box.
  • Validation hooks: @BuildIf lets you add custom logic (e.g., ‘only build AdminUser if role === ‘admin’’).
  • Scalar support: Build objects from single values (e.g., 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]."*

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky