- How do I install php-collective/dto in a Laravel application?
- Install it with Composer using composer require php-collective/dto, then check the package's composer.json for its PHP and Laravel constraints before adopting it. Because releases are still 0.1.x, pin an exact version and read the changelog before upgrading.
- How do I hydrate a DTO from a Laravel request?
- DTOs are hydrated from arrays, and the package is described as Laravel-friendly for request data. The usual pattern is to validate input with a form request or validator, then map that validated array into a DTO at the controller edge and pass the typed object inward. Validating first keeps the DTO receiving known keys and predictable scalar types.
- Does php-collective/dto support nested DTOs and collections?
- Yes. Nested DTOs and collections are core features, with automatic casting from arrays into child DTO objects, which suits complex payloads such as webhook bodies and third-party API responses. Test nested and collection casting through both the runtime and generated hydration paths, since the package has dual execution paths.
- How does validation work in php-collective/dto?
- The package supports validation and transformation hooks on DTO fields, letting you reject malformed input at the boundary instead of scattering checks through services. For request-shaped data, prefer Laravel form requests where you already use them. Keep business rules out of global transformer configuration.
- Which PHP and Laravel versions does php-collective/dto support?
- The package overview does not list explicit PHP or Laravel version ranges, so inspect composer.json and the CI matrix in the canonical php-collective/dto repository. Confirm which versions are tested for the specific release you plan to use, especially 0.1.20. Treat untested combinations as unsupported until you add them to your own test matrix.
- What are the generated fast-path files and how should I build and deploy them?
- The package compiles hydration into generated files for a faster path, and those files must be produced and refreshed as part of your build. Decide whether to commit them or generate them per environment, and make that step explicit in CI and deploy scripts. Strict types, callable resolution, and stale artifacts can change behavior, so run a smoke test after every build.
- What is TransformerRegistry and how does it scope input versus output transforms?
- TransformerRegistry holds casters used during input hydration and serializers used for output, and 0.1.20 scoped the guards so casters apply on input while serializers apply on output. Globally registered casters should now apply consistently during generated fromArray() hydration, which was the bug fixed in that release. Keep registry configuration declarative rather than hiding business logic there, and isolate registrations in tests.
- How do I test parity between generated and runtime hydration?
- Add contract tests that run the same fixtures through both generated and runtime hydration and compare the results. Cover enums, dates, nulls versus absent keys, unknown and missing keys, scalar coercion, nested DTOs, and collections. Assert that fromArray() and toArray() or toJson() round-trip symmetrically for the cases your application relies on. Also test a missing or renamed transform callable to confirm fallback behavior is observable.
- Is php-collective/dto production ready?
- It is still pre-1.0, with unproven adoption, maintainer depth, security policy, and long-term support, so a conditional pilot is the sensible stance. Gate broader rollout on reproducible builds, generated-versus-runtime parity tests, and a performance check with realistic collection sizes. Have a fork or internal-maintenance plan in case upstream stalls.
- How does php-collective/dto compare with plain arrays or other Laravel DTO packages?
- Compared with passing loose arrays, php-collective/dto gives you typed properties, IDE support, casting, and validation at the cost of an extra dependency and a build step for generated code. Compared with heavier Laravel DTO packages, it positions itself as lightweight. For a handful of simple payloads, a few hand-written readonly classes may be enough, so benchmark and review maintenance signals before choosing.