boson-php/value-object-contracts
ValueObject interfaces, type safety, and behavioral constraints). Ideal for systems where domain integrity is critical (e.g., financial, healthcare, or e-commerce).supports(), equals()), which could slow initial development.ValueObject → Money, Email) for dependency injection.Email::supports($request->email)).ValueObject behaviors).boson-php/boson).Money, Address) or is CRUD sufficient?Illuminate\Support\Collection + custom validation) suffice?spatie/laravel-model-states for stateful value objects.laravel-validator for contract-based validation.pestphp/pest for behavior-driven tests of value objects.Arrayable/Jsonable traits) unless explicitly designed.Email, PhoneNumber).class Email { public string $value; }) with contract-based implementations.EmailContract, MoneyContract).jsonb recommended).// Migration
Schema::table('orders', function (Blueprint $table) {
$table->json('shipping_address')->nullable();
});
// Model
class Order extends Model {
public function getShippingAddressAttribute(): Address {
return Address::fromArray($this->attributes['shipping_address']);
}
}
ValueObject classes.Value objects (e.g., Illuminate\Support\ValueObject).| Phase | Task | Dependencies |
|---|---|---|
| 1. Setup | Install package, configure autoloading. | PHP 8.1+, Composer |
| 2. Contracts | Define ValueObject interfaces (e.g., EmailContract). |
Domain analysis |
| 3. Core | Replace DTOs with value objects in services. | Contracts defined |
| 4. Persistence | Integrate with Eloquent (JSON columns or custom tables). | Database schema changes |
| 5. API | Adapt API responses (e.g., JSON:API serializers). | API contract definitions |
| 6. Testing | Write unit/integration tests for value objects. | Test suite |
Email validation).dd() or Xdebug to inspect value object state.CurrencyRates).| Failure Scenario | Impact | Mitigation Strategy |
|---|---|---|
| Invalid value object creation | Runtime exceptions (e.g., InvalidArgumentException). |
Use factory methods (e.g., Email::fromString()). |
| Serialization errors | API/database failures. | Implement JsonSerializable or custom encoders. |
| Contract violations | Inconsistent domain state. | Add runtime checks (e.g., assert($email->isValid())). |
| Database schema mismatches | Data corruption. | Use migrations with rollback safety. |
| Team resistance | Adoption failure. | Pilot with a small team, demonstrate ROI. |
How can I help you explore Laravel packages today?