funeralzone/valueobjects
PHP 7.1+ value object toolkit for fundamental scalar-based VOs. Provides traits for strings/ints/etc, enums via constants, easy native serialization (fromNative/toNative), and encourages domain validation logic. Includes extension library for complex objects.
Money, Email, Percentage), reducing side effects and improving domain modeling in Laravel applications.Money::fromFloat(100.50, 'USD')), which integrates seamlessly with Laravel’s typed dependency injection and modern PHP tooling (e.g., PHPStan, Psalm).Validator::make()) for domain-specific rules (e.g., Percentage::max(100)).DateRange, Coordinate).FormRequest + value objects)?spatie/array-to-object) suffice?Validator, Serializer, and PropertyAccess if needed.Email, Percentage) in a single module.if (!filter_var($email, FILTER_VALID_EMAIL))) with value objects.string → Email) in DTOs/requests.getMoneyAttribute()).rules() with value object validation in authorize() or custom logic.protected $casts = ['price' => Money::class]).JsonSerializable wrapper.Money to Amount.| Phase | Task | Dependencies |
|---|---|---|
| 1. Setup | Install package, configure autoloading. | PHP 7.4+, Composer. |
| 2. Validation | Replace manual validation with value objects in FormRequests. | Laravel Validator (optional). |
| 3. Domain Layer | Refactor services to use value objects. | Step 2 complete. |
| 4. Persistence | Implement database storage strategy (JSON/casts/tables). | Eloquent or custom ORM. |
| 5. API Layer | Serialize value objects in API responses. | Step 3 complete. |
| 6. Testing | Write unit/integration tests for value objects. | PHPUnit/Pest. |
| 7. Rollout | Deploy in stages (e.g., feature flags for new endpoints). | CI/CD pipeline. |
Percentage::fromFloat(101)).Money in API responses.| Risk | Mitigation Strategy |
|---|---|
| Invalid Data Persistence | Use strict database constraints + application-level validation. |
| PHP Version Incompatibility | Fork the package or use a compatibility layer (e.g., php-compat). |
| Performance Bottlenecks | Profile serialization/deserialization; optimize database storage. |
| Team Resistance | Demo benefits (e.g., fewer bugs, cleaner code) and provide scaffolding (e.g., CLI generator). |
| Testing Gaps | Enforce value object coverage in CI; use property-based testing (e.g., Pest). |
| Vendor Lock-in | Design value objects to be framework-agnostic; document interfaces. |
How can I help you explore Laravel packages today?