solophp / input
Typed, validated input DTOs for PHP 8.3+: immutable classes built through their constructor from HTTP input, with attribute-driven validation, casting and processing
Requires
- php: ^8.3
- psr/container: ^2.0
- psr/http-message: ^2.0
- solophp/contracts: ^1.8
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.3
- solophp/validator: ^4.2
- squizlabs/php_codesniffer: ^3.13
Suggests
- solophp/validator: ^4.2
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-28 19:48:36 UTC
README
Typed, validated input DTOs for PHP 8.3+. Mark a class #[Input] and declare its fields in the constructor: the factory reads them from the request, validates and casts the whole input, then calls the constructor. You get a complete, typed object, and as a readonly class it cannot change.
Installation
composer require solophp/input
Requires PHP 8.3+, a PSR-7 implementation and a validator implementing Solo\Contracts\Validator\ValidatorInterface that checks a missing or null field by presence rules only, as the interface says since solophp/contracts 1.8 — such as solophp/validator 4.2 or later.
Example
use Solo\Input\Attributes\Input; use Solo\Input\Attributes\Source\FromRoute; use Solo\Input\Attributes\Validate; use Solo\Input\InputFactory; #[Input] final readonly class UpdateProductInput { public function __construct( #[FromRoute] #[Validate('required|integer')] public int $id, #[Validate('required|string|max:255')] public string $name, #[Validate('required|numeric|min_value:0')] public float $price, #[Validate('integer|min_value:0')] public int $stock = 0, ) { } } $factory = new InputFactory($validator); // PUT /products/{id} $input = $factory->fromBody(UpdateProductInput::class, $request, route: ['id' => $productId]); $input->id; // int, from the URL path $input->name; // string, from the body $input->stock; // int, 0 when the client left it out or sent no value
Invalid input throws one ValidationException with every error, keyed by the field the client sent.
Documentation
- Installation
- Quick Start
- Configuration
- Upgrading from 1.0
- Attributes
- Type Casting
- Processing Stages
- Generators
- Nested Objects & Items
- Field Grouping
- Validation
- Schema Export
- API Reference
License
MIT. See LICENSE.