Hydration
Hydration turns request values into a PHP object. When a route handler asks for a class marked #[Dto], Atom builds that object from the current request before calling the handler. Hydration maps and converts values. Validation checks rules such as whether a subject is required or a priority is allowed.
Constructor DTOs
Here is the same TicketInput from Validation, adapted to a ticket form on /projects/{projectId}/tickets. The source attributes tell Atom where to get each value.
<?php
namespace App\Requests;
use Atom\Hydrator\Attributes\Dto;
use Atom\Hydrator\Attributes\FromBody;
use Atom\Hydrator\Attributes\FromRoute;
use Atom\Validation\Rules\In;
use Atom\Validation\Rules\MaxLength;
use Atom\Validation\Rules\Required;
#[Dto]
final readonly class TicketInput
{
public function __construct(
#[FromRoute] public int $projectId,
#[FromBody]
#[Required("Enter a subject.")]
#[MaxLength(80, "Use 80 characters or fewer.")]
public string $subject = "",
#[FromBody]
#[Required("Choose a priority.")]
#[In(["normal", "urgent"], "Choose normal or urgent.")]
public string $priority = "",
) {
}
}
| Declaration | What Atom reads |
|---|---|
#[FromRoute] |
The projectId placeholder from the matched URL. |
#[FromBody] |
A form field with the same name as the parameter. |
#[FromBody("ticket_subject")] |
An explicitly named form field, if the input uses a different name. |
#[FromQuery] |
A query string value. |
Hydration also supports writable properties. With a constructor DTO, declare defaults for fields you want validation to check when they are missing from the request. Here an omitted subject becomes an empty string, which Required can report with a field-specific message.
Validate the hydrated input
Ask for TicketInput by type in the route handler. Atom supplies the populated object. Call validate() inside the handler to apply the rules.
use App\Requests\TicketInput;
use Atom\Router\Route;
use Atom\Validation\Validator;
Route::post("/projects/{projectId}/tickets", static function (TicketInput $input): array {
return Validator::for()->validate($input)->messages();
});
For /projects/42/tickets, Atom converts the route value to the integer 42 and passes the submitted subject and priority into the constructor. A submitted priority of high is a string, so hydration succeeds. Validation then reports that the priority must be normal or urgent.
Coercion
An input that cannot be converted to the declared type fails during hydration. For example, /projects/abc/tickets supplies a value that cannot become an int. Atom raises a HydrationException before calling the handler.
To exercise the mapping without a route, pass the source values directly to RequestHydrator:
use App\Requests\TicketInput;
use Atom\Hydrator\HydrationContext;
use Atom\Hydrator\RequestHydrator;
$input = (new RequestHydrator())->hydrate(
TicketInput::class,
new HydrationContext(
body: ["subject" => "Printer offline", "priority" => "urgent"],
route: ["projectId" => "42"],
),
);
This produces a TicketInput whose projectId is an integer and whose subject and priority come from the body.