Search by

solophp / input

Vitaliy Olos

Typed, validated input DTOs for PHP 8.3+: immutable classes built through their constructor from HTTP input, with attribute-driven validation, casting and processing

Package info

github.com/SoloPHP/Input

pkg:composer/solophp/input

Statistics

Installs: 63

Dependents: 2

Suggesters: 0

Stars: 1

Open Issues: 0

v1.1.0 2026-10-28 14:07 UTC

README

Latest Version on Packagist PHP Version License

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

Full Documentation

License

MIT. See LICENSE.