- Does funeralzone/valueobjects work with Laravel 9/10 and PHP 8.2+?
- The package officially supports PHP 7.1+, but since its last release was in 2020, it may not fully support PHP 8.2+ features like union types or named arguments. Test thoroughly in your environment, or consider forking for updates. For Laravel 9/10, it should work as a standalone library, but avoid PHP 8.2+ specific syntax in custom value objects.
- How do I store value objects like Money or Email in a Laravel database?
- Use JSON columns for simplicity (e.g., `json` type in MySQL) or normalize into separate tables for type safety. For Eloquent, implement accessors/mutators (e.g., `getMoneyAttribute()`) or use casts. Avoid raw serialization to avoid losing type safety during retrieval.
- Can I replace Laravel’s built-in Validator with this package?
- Yes, but it’s often better to coexist. Use value objects for domain-specific logic (e.g., `Email::fromString()`) and Laravel’s Validator for generic rules (e.g., `required`). FormRequests can validate inputs with value objects in `authorize()` or `rules()`, then convert to value objects in controllers.
- What’s the best way to test value objects in CI?
- Test value objects as pure units with PHPUnit, focusing on edge cases (e.g., invalid emails, negative Money). Mock external dependencies (e.g., database) but avoid mocking the value objects themselves. Use data providers for validation scenarios. Immutable objects reduce flakiness in tests.
- How do I create custom value objects beyond the built-in ones (e.g., DateRange)?
- Extend the `ValueObject` interface and use traits like `StringTrait` or `NumericTrait` for scalars. For complex types, combine traits or write custom logic in the constructor (e.g., validate ranges). Check the [extensions library](https://github.com/funeralzone/valueobject-extensions) for inspiration.
- Will this package slow down my API or database queries?
- Minimal overhead for simple objects, but complex nested value objects may impact serialization/deserialization (e.g., JSON payloads). Benchmark in your stack. For databases, JSON columns add slight parsing overhead, while normalized tables improve query performance but increase complexity.
- Are there alternatives to funeralzone/valueobjects for Laravel?
- For lightweight needs, use custom value objects with `spatie/array-to-object` for DTOs. For DDD, consider `thephpleague/value-object` (more modern) or `moneyphp/money` (financial focus). Laravel’s built-in validation suffices for simple cases, but value objects add domain clarity.
- How do I integrate value objects with Laravel Eloquent models?
- Use accessors/mutators (e.g., `getEmailAttribute()`) or casts to convert between strings/JSON and value objects. For example: `protected $casts = ['email' => Email::class];`. Avoid storing value objects directly in database columns unless using JSON serialization.
- Is this package actively maintained? Should I fork it?
- The last release was in 2020, with no PHP 8.x/9.x updates. If you need modern PHP support, fork the repo and update dependencies (e.g., `symfony/assert` for PHP 8.2). Check for open issues or community forks before investing time.
- How do I migrate from manual validation (e.g., `filter_var($email)`) to value objects?
- Start with non-critical fields (e.g., `Email`, `Percentage`). Replace manual checks in controllers with value objects (e.g., `Email::fromString($request->email)`). Use FormRequests to validate inputs, then convert to value objects in services. Gradually extend to domain logic.