Search by

PHP 8.5+ DTO and Data library with immutable DTOs and mutable Data objects

Package info

github.com/jooservices/dto

pkg:composer/jooservices/dto

Statistics

Installs: 9 733

Dependents: 9

Suggesters: 0

Stars: 0

Open Issues: 0

v4.0.0 2026-11-28 19:44 UTC

README

CI Coverage (develop) Quality Gate (master) OpenSSF Scorecard PHP Version GitHub Release Packagist Version Total Downloads License: MIT

A PHP 8.5+ attribute-driven DTO and Data library: immutable Dto and mutable Data objects, constructor-first hydration, opt-in validation, serialization control, collections, and JSON Schema / OpenAPI generation. One third-party runtime package: psr/http-message (interface-only).

Warning

v3.0.0 is a complete ground-up rebuild of this package and is NOT backward compatible with any previous version (v1.x, v2.x). Rewrite DTO classes for the v3 API before upgrading; there are no legacy shims or deprecation bridges. See the changelog.

Upgrade highlights

  • v3 has a new DTO/Data engine, constructor-first hydration, and explicit key-space rules.
  • jooservices/exceptions is no longer a runtime dependency; psr/http-message supports fromRequest().
  • with(), merge(), and clone() use property names and rebuild through the constructor.

Features

  • Immutable Dto and mutable Data objects with typed, constructor-first hydration.
  • Factories for arrays, JSON, objects, and PSR-7 requests; collection and partial-payload support.
  • Attribute-driven mapping, casting, transforms, validation, and serialization control.
  • Immutable copies, merging, comparison, hashing, and lazy derived properties.
  • JSON Schema and OpenAPI generation, structured exceptions, and cached reflection metadata.

Requirements

  • PHP >= 8.5
  • Extensions: dom, libxml, mbstring
  • psr/http-message (fromRequest()), optional: psr/log (Engine debug logging)
  • Docker with Docker Compose v2 (required for local development and quality gates)

Installation

composer require jooservices/dto:^4.0

Projects requiring ^3.0 remain on the v3 line until they update their Composer constraint. Version 4 requires mbstring in the installation environment.

Quick start

use JOOservices\Dto\Attributes\MapFrom;
use JOOservices\Dto\Core\Dto;

final class UserDto extends Dto
{
    public function __construct(
        public readonly string $id,
        #[MapFrom('email_address')]
        public readonly string $email,
        public readonly \DateTimeImmutable $createdAt,
    ) {}
}

// External input — source keys resolved through MapFrom / naming strategy
$user = UserDto::from([
    'id' => 'u_123',
    'email_address' => 'john@example.com',
    'createdAt' => '2026-01-15T10:30:00+00:00',
]);

// Immutable copy — property-name keys only, patched value cast to string
$updated = $user->with(email: 'other@example.com');

$updated->toArray();  // ['id' => 'u_123', 'email' => 'other@example.com', ...]
$updated->toJson();

Design notes

  • Every DTO declares a constructor with public promoted properties; constructor-less classes are unsupported.
  • Each entry point owns exactly one key space:
Entry point Key space
new UserDto(...) Property names — already-typed values
UserDto::from() / fromArray() / fromJson() / fromObject() / fromRequest() Source keys — resolved via MapFrom / naming strategy
$dto->with(...) / merge(...) Property names only — immutable copies built through the constructor
  • Validation is opt-in via Context; casting and pipelines never silently change scope when a Context argument is omitted.

Documentation

Development

PHP tooling runs inside the DTO Project Docker image (php:8.5-cli-trixie). Local security, workflow, and Dockerfile scanners run in separate one-shot Project Docker services. SonarQube Community Build runs on demand in JOOservices Shared Docker; the scanner remains in Project Docker. No host PHP, Composer, Gitleaks, or scanner installation is required.

make install          # build PHP + Sonar scanner images and install dependencies in Docker
make hooks-install    # install Docker-backed Git hooks
make shell            # interactive PHP container shell

Local SonarQube setup

The local pre-scan sends analysis only to the shared SonarQube Community Build service. Its server and persistent data live in Shared Docker, while the scanner runs in the DTO Project Docker image and joins the jooservices-shared network for that scan. Local analysis never reads the SonarCloud token and never uploads to SonarQube Cloud. The shared server is opt-in and binds only to loopback.

On first use, start the server:

make sonar-up

Open http://localhost:9000. On first use, sign in with admin / admin, immediately change the password, and save it under JOOSERVICES_SONAR_LOCAL_ADMIN_PASSWORD in Sites/.env. Create a project with key jooservices_dto, then generate a Project analysis token for that project. Store the project token as JOOSERVICES_DTO_SONAR_LOCAL_TOKEN in the ignored Sites/.env file:

JOOSERVICES_DTO_SONAR_LOCAL_TOKEN=<local-project-analysis-token>

Do not use the SonarCloud token for this key. make sonar starts the shared server if it is stopped, waits for it to become ready, runs coverage analysis, and fails when the local Quality Gate fails. Use make sonar-stop to stop the shared server while preserving its named volumes. Other JOOservices projects can use this server with their own analysis token under JOOSERVICES_<PROJECT>_SONAR_LOCAL_TOKEN in Sites/.env.

GitHub Actions uses the repository SONAR_TOKEN secret for SonarCloud PR analysis. That Cloud upload is confined to CI. The optional shared SonarQube service reserves up to 4 GB RAM and keeps project data in named volumes.

Command Purpose
make validate composer validate --strict
make lint Pint, PHPCS, PHPStan, PHPMD, PHP-CS-Fixer
make test PHPUnit (Unit + Integration)
make test-coverage PHPUnit with Clover coverage
make audit Composer audit
make bench phpbench
make ci Composer lint and combined coverage check
make precheck Fast local gate: dependency validation, lint, unit/integration tests, hook tests, and secret scans
make verify Complete local gate using the same Make checks as PR CI, plus the local SonarQube quality gate
make scan Gitleaks, OSV, Semgrep, Trivy, actionlint, zizmor, ShellCheck, and Hadolint in Project Docker
make sonar-up Start the shared local SonarQube Community Build server
make sonar Local SonarQube analysis and blocking quality-gate result; requires JOOSERVICES_DTO_SONAR_LOCAL_TOKEN in Sites/.env
make sonar-stop Stop the shared SonarQube server without deleting its data
make preflight Full pre-commit quality gate (make verify)

Every linter runs at maximum strictness with no ignore lists — fix issues at the source instead of suppressing them.

Git hooks run make preflight before commits and scan each pushed ref update before pushes. Newly created refs use origin/develop as the scan base unless DTO_GITLEAKS_BASE_REF names another ancestor. Hooks fail closed when Docker tools, the local Sonar token, the GitHub token for online zizmor audits, or the required Git history is unavailable, or when a blocking finding is reported. PR CI invokes the same Make targets for shared checks. SonarCloud uses make sonar-cloud with GitHub Actions PR metadata; make sonar keeps the local SonarQube server and quality gate. GitHub-only dependency review and result uploads still run on the PR.

IDE setup: Cursor / VS Code — install recommended workspace extensions; format-on-save runs Pint via tools/pint (Docker). PHPStorm — inspection profile + Pint file watcher.

Community

License

MIT — see LICENSE.