msstc4php / vo-base-laravel
Laravel Eloquent cast, validation rule, request input and route parameters for msstc4php/vo-base value objects.
Requires
- php: >=8.4
- illuminate/container: ^13.40
- illuminate/contracts: ^13.40
- illuminate/database: ^13.40
- illuminate/support: ^13.40
- illuminate/validation: ^13.40
- msstc4php/vo-base: ^1.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.76
- illuminate/translation: ^13.40
- laravel/framework: ^13.40
- orchestra/testbench: ^11.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: >=11.0
- rector/rector: ^2.1
- roave/security-advisories: dev-latest
Suggests
- laravel/framework: For $request->valueObject(), the ValidatedValueObjects form request trait and value objects as route parameters.
Provides
None
Conflicts
- illuminate/http: <13.40
- illuminate/routing: <13.40
- laravel/framework: <13.40
Replaces
None
README
Laravel glue for msstc4php/vo-base value objects:
Casts\AsValueObject— an Eloquent cast for every value object the core'sValueObjectMappersupports.Rules\ValidValueObject— a validation rule that asks the value object whether the input is valid.$request->valueObject('email', EmailAddress::class)— request input as a value object, invalid input a 422.Http\ValidatedValueObjects— the same for a form request, over its validated data.- Route parameters typed with a value object (
/users/{email}intoEmailAddress $email), invalid values a 404.
The last three come from ValueObjectServiceProvider, which Laravel's package discovery registers.
Requires PHP >= 8.4, msstc4php/vo-base ^1.4 and Laravel 13.40+ (illuminate/database, illuminate/validation
^13.40; laravel/framework below 13.40 has a security advisory). The cast and the rule work with those components
alone (Capsule included); request input, form requests and route parameters need an application on
laravel/framework. Laravel 12 cannot be supported: every 12.x release caps brick/math below 0.15,
which the core declares as a conflict.
Install
Neither this package nor the core is on Packagist yet, so register both GitHub repositories:
{
"repositories": [
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-base" },
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-base-laravel" }
],
"require": {
"msstc4php/vo-base-laravel": "^1.0"
}
}
Eloquent cast
use MSSTC4PHP\ValueObject\Laravel\Casts\AsValueObject; final class Customer extends Model { protected function casts(): array { return [ 'email' => AsValueObject::of(EmailAddress::class), 'country' => AsValueObject::of(CountryCode::class), 'location' => AsValueObject::of(GeoCoordinates::class), ]; } }
AsValueObject::of(X::class) is the string AsValueObject::class.':'.X::class, so the $casts property form works
the same way: protected $casts = ['email' => AsValueObject::class.':'.EmailAddress::class];.
| Value object kind | Example | Stored as | Column |
|---|---|---|---|
| string-backed | EmailAddress, Uuid |
the string | string / text |
| int-backed | Port, PositiveInt |
the integer | integer |
| float-backed | Percentage |
the number | float / double |
| backed enum | CountryCode, CurrencyCode |
the enum code (->value) |
string / integer |
| composite | GeoCoordinates, IntRange, DateRange, BoundedString |
JSON text, e.g. {"lat":52.52,"lng":13.405} |
text / json |
The formats are those of the core's payload layer, so a column written by this cast reads back through the
msstc4php/vo-base-doctrine types and the other bridges.
Behaviour:
nullstaysnullboth ways.- Reading a stored value that is not valid throws (
InvalidValueObjectExceptionor its subclassInvalidPayloadException); it never turns intonull. Numeric strings from the driver are accepted for int- and float-backed value objects and int enums. - Assigning a value object stores its payload. Assigning a raw payload is accepted too and validated through the
value object:
$customer->email = 'jane@example.com'stores the canonical form,$customer->port = 70000throws. A composite accepts an array or its JSON text. A raw payload counts as untrusted input (it may come from$model->fill($request->all())):BoundedString, whose payload carries its own bounds, must be assigned as an instance — a raw array or JSON for it throwsInvalidPayloadException. Reading it from the database is fine. - Query expressions (
DB::raw(...), anyExpression) are not payloads: assigning one throwsInvalidPayloadException. Write such updates with the query builder instead. toArray()/toJson()use the payload form: a composite becomes a nested array, not a JSON string.- Dirty checking compares payloads, so a
jsonbcolumn that reformats the JSON does not make the model dirty. - An unsupported class (
AsValueObject::of(stdClass::class)) throwsLogicExceptionthe first time the model uses the cast.
A cast or rule built without a mapper uses the container's ValueObjectMapper binding (see "Custom codecs"), so a
value object of your own with a payload codec works with AsValueObject::of(Money::class) once that binding has its
codec. To give one cast a mapper of its own, use a small Castable:
final readonly class AsMoney implements Castable { public static function castUsing(array $arguments): AsValueObject { return new AsValueObject(Money::class, new ValueObjectMapper([new MoneyCodec()])); } } // casts(): 'price' => AsMoney::class
The rule takes a mapper the same way: new ValidValueObject(Money::class, mapper: $mapper).
Validation rule
use MSSTC4PHP\ValueObject\Laravel\Rules\ValidValueObject; $request->validate([ 'email' => ['required', new ValidValueObject(EmailAddress::class)], 'port' => ['nullable', new ValidValueObject(Port::class)], 'country' => [new ValidValueObject(CountryCode::class, 'Pick a country from the list.')], ]);
nulland''pass; emptiness is the job ofrequired.- The input goes through the same mapper as the cast: form strings like
"8080"pass forPort, JSON numbers too. Composites accept an array ({"lat": 1, "lng": 2}in a JSON body) or JSON text (a form field), exactly like the cast; invalid JSON fails the rule. An instance of the value object passes as well. - Invalid content and a wrong input type both fail with one message:
The :attribute field is not a valid :type., where:typeis the short class name as lowercase words (EmailAddress→ "email address",Ipv4Address→ "ipv4 address"), never a class name. The text is also the translation key: add it tolang/<locale>.jsonto translate it. A custom message (second argument) is translated the same way. - A class the core marks as trusted-source only (
ValueObjectMapper::supportsUntrusted()is false — todayBoundedString) is refused with aLogicException: its payload carries its own length bounds, so request input would choose the limits it is checked against. Validate length withmin/maxor useNonEmptyString. - An unsupported class throws
LogicExceptionin the constructor.
Request input
public function store(Request $request) { $email = $request->valueObject('email', EmailAddress::class); // EmailAddress|null $location = $request->valueObject('location', GeoCoordinates::class); }
-
Reads
$request->input($key): query string, form fields and JSON body alike (dot notation works). -
Missing,
nullor''returnsnull; addrequiredvalidation (or check fornull) when the field must be there. -
Invalid input throws
ValidationExceptionfor that key, which Laravel answers with 422 and{"errors": {"email": ["The email field is not a valid email address."]}}(a redirect back with errors for a form). The message is the rule's (ValidValueObject::DEFAULT_MESSAGE, translated throughlang/<locale>.json), with the field named as Laravel's validator names it: avalidation.attributes.<key>translation if there is one, else the key in words (user_emailanduserEmailboth read "user email"). If the translator returns no string for the message, the untranslated message is used with both placeholders filled in. -
An array where a string is expected is invalid input, not an error page. Unlike
$request->enum(), there is no silent fallback to a default: that would hide a client error. -
Composites take an array (JSON body) or JSON text (a form field or a query string).
-
BoundedStringand any other trusted-source-only class are always a field error: their payload carries its own constraints. -
An unsupported class (
stdClass) is aLogicException, whether the field is filled or not. -
If the application already registered a
valueObjectmacro onRequest, the package leaves it in place.
The macro is untyped for static analysis (PHPStan sees it only through Larastan, and then as mixed); the form
request trait below is the typed path.
Form requests
use MSSTC4PHP\ValueObject\Laravel\Http\ValidatedValueObjects; final class StoreCustomerRequest extends FormRequest { use ValidatedValueObjects; public function rules(): array { return [ 'email' => ['required', new ValidValueObject(EmailAddress::class)], 'location' => ['nullable', new ValidValueObject(GeoCoordinates::class)], ]; } } public function store(StoreCustomerRequest $request) { $email = $request->valueObject('email', EmailAddress::class); }
- Reads
validated($key): the rules have already vetted the value, so a request reaching the controller converts cleanly. A key that is not inrules()returnsnull, even when the client sent it. - Same rules as the request macro otherwise (
nullfor empty, composites from an array or JSON text), and it is typed for PHPStan:valueObject(string $key, class-string<T> $class): ?T. - A field error names the field through the form request's
attributes()first, as the validator does. - On a form request using the trait,
valueObject()is the trait's (validated data), not the request macro. $request->safe()returns Laravel'sValidatedInput, which is not macroable, so there is nosafe()->valueObject().
Route parameters
Route::get('/users/{email}', fn (EmailAddress $email) => User::where('email', $email->value)->firstOrFail()); Route::get('/teams/{team_slug}', [TeamController::class, 'show']); // show(Slug $teamSlug) Route::get('/ports/{port?}', fn (?Port $port = null) => $port?->value ?? 80);
- An action argument (closure or controller method) typed with a scalar-backed value object receives it, built from the route parameter of the same name or its snake_case form, as Laravel's implicit binding matches them.
- An invalid value is a 404, with the message
The email route parameter is not a valid email address.. The parameter is named as in request input errors: avalidation.attributes.<name>translation, else the name in words ({user_email}→ "The user email route parameter …"). - The 404's previous exception is vo-base's, whose message may quote an excerpt of the segment. Laravel shows it only
on debug error pages (
APP_DEBUG=true); the response with debugging off carries the message above. - Nullable arguments for optional parameters need a default (
?Port $port = null). This is the same rule as Laravel's optional model binding: without a default, Laravel drops the missing parameter and tries to build the class from the container, which is a 500 that the package cannot prevent. - An argument typed with a composite (
GeoCoordinates) or a trusted-source-only class (BoundedString) is aLogicExceptionnaming the argument, on every request to the route, whether or not the segment is present: a URL segment is one string and must not choose its own constraints. An explicitRoute::bind()for that parameter takes it over instead. - Left alone: backed enums (Laravel binds them), parameters with an explicit
Route::bind()/Route::model()(anEmailAddressyour binder returns reaches the argument unchanged), values that already are the value object, union and intersection types, and classes the mapper does not support. - The conversion runs on Laravel's
RouteMatchedevent, before all route middleware. An invalid segment is a 404 even whereauthwould have answered 401 or 403. Middleware that reads$request->route('email')sees the value object, not the string. Invalid-segment requests are also answered beforethrottle, so they do not count against the rate limit. This order differs from Symfony, where the firewall runs before the argument resolvers.
Custom codecs
The provider binds one ValueObjectMapper singleton; the cast, the rule, the request macro, the form request trait
and the route binding resolve it from the container. Bind your own in a service provider's register() to add codecs,
or a package's whole set through its ProvidesCodecs class:
$this->app->singleton(ValueObjectMapper::class, fn () => new ValueObjectMapper([new MoneyCodec()])); // vo-documents' composite names: new ValueObjectMapper(\MSSTC4PHP\ValueObject\Documents\Codecs::all())
Bind it as a singleton: casts are kept per class and per mapper, so a rebinding (in tests, say) gets casters of its
own, and a bind() would build a new mapper and new casters on every attribute access. Without a container binding
(Eloquent through Capsule alone) a plain mapper is used; a binding to anything else than a ValueObjectMapper throws
LogicException.
Backward compatibility
1.x follows semantic versioning; a breaking change needs 2.0. Covered by 1.x:
- Code API: these classes and the trait, the constructors with these parameter names (named arguments), the
methods of the Laravel interfaces and base classes they implement or extend:
Casts\AsValueObject(string $class, ?ValueObjectMapper $mapper = null)(templateTValue), itsof(string $class)and Eloquent'scastUsing(),get(),set(),serialize()andcompare();Rules\ValidValueObject(string $class, ?string $message = null, ?ValueObjectMapper $mapper = null), itsDEFAULT_MESSAGEconstant andvalidate();Http\ValidatedValueObjects, the form request trait, withvalueObject(string $key, string $class);ValueObjectServiceProvider, auto-discovered (extra.laravel.providers); itsregister()andboot()are Laravel's to call. It bindsValueObjectMapperas a singleton only if the application has not bound one (a later binding replaces it) and, whenilluminate/http/illuminate/routingare installed, registers the request macro (unless the application has avalueObjectmacro) and the route binding.
- Names users write: the cast definition
AsValueObject::class.':'.X::class(whatof()returns); theRequestmacrovalueObject($key, $class); the container idValueObjectMapper::class, which a cast or rule built without a mapper, the macro, the trait and the route binding use; the route binding by argument name or its snake_case form, for scalar-backed value objects, leaving enums, explicitRoute::bind()/Route::model()bindings, instances, union types and unsupported classes alone, and running onRouteMatched, before the route middleware. - Messages: the translation key
The :attribute field is not a valid :type.(ValidValueObject::DEFAULT_MESSAGE, also the 422 message of the macro and the trait) and its:attributeand:typeplaceholders; field names read from a form request'sattributes(), thenvalidation.attributes.<key>. Changing that default text needs 2.0, or a new key with the old one kept. - Data: the stored formats above. They are vo-base's payload layer, the same as the Doctrine types and the other bridges write, so a row written through one reads through another. Request input and route segments are read in the same forms.
- Exceptions:
- the cast's
get(),set()andserialize(): vo-base'sInvalidValueObjectExceptionor its subclassInvalidPayloadException, unchanged, for an invalid stored or assigned value (including a raw payload of a trusted-source-only class and a query expression);InvalidPayloadExceptionwith theJsonExceptionas previous for composite JSON text that does not parse or a composite that cannot be encoded; Illuminate\Validation\ValidationException(422) for invalid input to the macro and the trait, and for a trusted-source-only class there, with no previous exception;Symfony\Component\HttpKernel\Exception\NotFoundHttpException(404) for an invalid route segment, with vo-base's own exception as previous, passed through unchanged;LogicExceptionfor configuration errors: a cast definition without exactly one value-object class, a class the mapper does not support (cast, rule, macro, trait), a trusted-source-only class in the rule, a macro class name that is no class, a route argument typed with a composite or a trusted-source-only class, and, for a cast or rule built without a mapper, a container binding ofValueObjectMapperto anything else.
- the cast's
- Enums: the cast, the rule and request input handle backed enums; route parameters typed with a backed enum stay with Laravel's implicit binding.
- Versions: a minor may add support for a new Laravel or PHP major; dropping a supported Laravel or PHP version
needs a major, and so does raising the floor of any third-party dependency (Laravel, PHP). A minor may raise the
floor of a
msstc4php/*package within its major (those follow the same promise). The floor of the optionallaravel/framework,illuminate/httpandilluminate/routing(13.40) is enforced byconflictentries.
A minor never refuses a value an earlier 1.x accepted: the cast, the rule, request input and the route binding add no validation of their own, the value objects (vo-base and the domain packages) decide, and they follow the same rule.
Not covered: exception message wording (the package's messages name classes, type labels, input keys or route parameters,
never the value), except the translation key above; anything marked @internal (Internal\, Casts\CasterCache,
Http\ValueObjectInput, and Http\RequestMacros and Routing\RouteValueObjectBinder, which the provider registers).
Development
COMPOSER=composer-local.json composer update --ignore-platform-req=php # against ../vo-base
make check # php -l, PHPStan level max, cs-fixer, composer validate/audit, Rector, deptrac
make test # PHPUnit (Eloquent on SQLite :memory:, the real validator, Orchestra Testbench for HTTP)
make infection