From cb1251ae1476fb88c00858a6139c351ff4059bd4 Mon Sep 17 00:00:00 2001 From: Funky Waddle Date: Wed, 10 Dec 2025 21:09:04 -0600 Subject: [PATCH] =?UTF-8?q?join=E2=80=91based=20eager=20loading=20(SQL)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 112 ++ composer.lock | 1818 +++++++++++++++++++++++ examples/mysql_join_eager_demo.php | 106 ++ src/Model/AbstractDao.php | 301 +++- src/Model/AbstractDto.php | 52 +- src/Model/Casting/CasterInterface.php | 12 + tests/CastersAndAccessorsSqliteTest.php | 93 ++ tests/JoinEagerMysqlTest.php | 125 ++ tests/JoinEagerSqliteTest.php | 127 ++ 9 files changed, 2731 insertions(+), 15 deletions(-) create mode 100644 composer.lock create mode 100644 examples/mysql_join_eager_demo.php create mode 100644 src/Model/Casting/CasterInterface.php create mode 100644 tests/CastersAndAccessorsSqliteTest.php create mode 100644 tests/JoinEagerMysqlTest.php create mode 100644 tests/JoinEagerSqliteTest.php diff --git a/README.md b/README.md index bb39a91..f836be5 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,49 @@ Notes: - Loaded relations are attached onto the DTO under the relation name (e.g., `$user->posts`). - `hasOne` is supported like `hasMany` but attaches a single DTO instead of a list. +### Join‑based eager loading (opt‑in, SQL) + +For single‑level relations on SQL DAOs, you can opt‑in to a join‑based eager loading strategy that fetches parent and related rows in one query using `LEFT JOIN`s. + +Usage: + +```php +// Require explicit projection for related fields when joining +$users = (new UserDao($conn)) + ->fields('id', 'name', 'posts.title') + ->useJoinEager() // opt‑in for the next call + ->with(['posts']) // single‑level only in MVP + ->findAllBy([]); +``` + +Behavior and limitations (MVP): +- Single‑level only: `with(['posts'])` is supported; nested paths like `posts.comments` fall back to the default batched strategy. +- You must specify the fields to load from related tables via `fields('relation.column')` so the ORM can alias columns safely. +- Supported relation types: `hasOne`, `hasMany`, `belongsTo`. +- `belongsToMany` continues to use the portable two‑query pivot strategy. +- Soft deletes on related tables are respected by adding `... AND related.deleted_at IS NULL` to the join condition when configured in the related DAO `schema()`. +- Per‑relation constraints that rely on ordering/limits aren’t applied in join mode in this MVP; prefer the default batched strategy for those cases. + +Tip: If join mode can’t be used (e.g., nested paths or missing relation field projections), Pairity silently falls back to the portable batched eager loader. + +Per‑relation hint (optional): + +You can hint join strategy per relation path. This is useful when you want to selectively join specific relations in a single‑level eager load. The join will be used only when safe (single‑level paths and explicit relation projections are present), otherwise Pairity falls back to the portable strategy. + +```php +$users = (new UserDao($conn)) + ->fields('id','name','posts.title') + ->with([ + // Hint join for posts; you can also pass a callable under 'constraint' alongside 'strategy' + 'posts' => ['strategy' => 'join'] + ]) + ->findAllBy([]); +``` + +Notes: +- If you also call `useJoinEager()` or `eagerStrategy('join')`, that global setting takes precedence. +- Join eager is still limited to single‑level relations in this MVP. Nested paths (e.g., `posts.comments`) will use the portable strategy. + ### belongsToMany (SQL) and pivot helpers Pairity supports many‑to‑many relations for SQL DAOs via a pivot table. Declare `belongsToMany` in your DAO’s `relations()` and use the built‑in pivot helpers `attach`, `detach`, and `sync`. @@ -578,6 +621,75 @@ $deep = array_map(fn($u) => $u->toArray(), $users); // deep (default) $shallow = array_map(fn($u) => $u->toArray(false), $users); // shallow ``` +## Attribute accessors/mutators & custom casters (Milestone C) + +Pairity supports lightweight DTO accessors/mutators and pluggable per‑column casters declared in your DAO `schema()`. + +### DTO attribute accessors/mutators + +- Accessor: define `protected function get{Name}Attribute($value): mixed` to transform a field when reading via property access or `toArray()`. +- Mutator: define `protected function set{Name}Attribute($value): mixed` to normalize a field when the DTO is hydrated from an array (constructor/fromArray). + +Example: + +```php +class UserDto extends \Pairity\Model\AbstractDto { + // Uppercase name when reading + protected function getNameAttribute($value): mixed { + return is_string($value) ? strtoupper($value) : $value; + } + // Trim name on hydration + protected function setNameAttribute($value): mixed { + return is_string($value) ? trim($value) : $value; + } +} +``` + +Accessors are applied for top‑level keys in `toArray(true|false)`. Relations (nested DTOs) apply their own accessors during their own `toArray()`. + +### Custom casters + +In addition to built‑in casts (`int`, `float`, `bool`, `string`, `datetime`, `json`), you can declare a custom caster class per column. A caster implements: + +```php +use Pairity\Model\Casting\CasterInterface; + +final class MoneyCaster implements CasterInterface { + public function fromStorage(mixed $value): mixed { + // DB integer cents -> PHP array/object + return ['cents' => (int)$value]; + } + public function toStorage(mixed $value): mixed { + // PHP array/object -> DB integer cents + return is_array($value) && isset($value['cents']) ? (int)$value['cents'] : (int)$value; + } +} +``` + +Declare it in the DAO `schema()` under the column’s `cast` using its class name: + +```php +protected function schema(): array +{ + return [ + 'primaryKey' => 'id', + 'columns' => [ + 'id' => ['cast' => 'int'], + 'name' => ['cast' => 'string'], + 'price_cents' => ['cast' => MoneyCaster::class], // custom caster + 'meta' => ['cast' => 'json'], + ], + ]; +} +``` + +Behavior: +- On SELECT, Pairity hydrates the DTO and applies `fromStorage()` per column (or built‑ins). +- On INSERT/UPDATE, Pairity applies `toStorage()` per column (or built‑ins) and maintains timestamp/soft‑delete behavior. +- Custom caster class strings are resolved once and cached per DAO instance. + +See the test `tests/CastersAndAccessorsSqliteTest.php` for a complete, runnable example. + ## Pagination Both SQL and Mongo DAOs provide pagination helpers that return DTOs alongside metadata. They honor the usual query modifiers: diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..a610bd3 --- /dev/null +++ b/composer.lock @@ -0,0 +1,1818 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "784d19185209a24a83acf351358e2f7e", + "packages": [ + { + "name": "mongodb/mongodb", + "version": "1.21.3", + "source": { + "type": "git", + "url": "https://github.com/mongodb/mongo-php-library.git", + "reference": "b8f569ec52542d2f1bfca88286f20d14a7f72536" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mongodb/mongo-php-library/zipball/b8f569ec52542d2f1bfca88286f20d14a7f72536", + "reference": "b8f569ec52542d2f1bfca88286f20d14a7f72536", + "shasum": "" + }, + "require": { + "composer-runtime-api": "^2.0", + "ext-mongodb": "^1.21.0", + "php": "^8.1", + "psr/log": "^1.1.4|^2|^3" + }, + "replace": { + "mongodb/builder": "*" + }, + "require-dev": { + "doctrine/coding-standard": "^12.0", + "phpunit/phpunit": "^10.5.35", + "rector/rector": "^2.1.4", + "squizlabs/php_codesniffer": "^3.7", + "vimeo/psalm": "6.5.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "files": [ + "src/functions.php" + ], + "psr-4": { + "MongoDB\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Andreas Braun", + "email": "andreas.braun@mongodb.com" + }, + { + "name": "Jeremy Mikola", + "email": "jmikola@gmail.com" + }, + { + "name": "Jérôme Tamarelle", + "email": "jerome.tamarelle@mongodb.com" + } + ], + "description": "MongoDB driver library", + "homepage": "https://jira.mongodb.org/browse/PHPLIB", + "keywords": [ + "database", + "driver", + "mongodb", + "persistence" + ], + "support": { + "issues": "https://github.com/mongodb/mongo-php-library/issues", + "source": "https://github.com/mongodb/mongo-php-library/tree/1.21.3" + }, + "time": "2025-09-22T12:34:29+00:00" + }, + { + "name": "psr/log", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.2" + }, + "time": "2024-09-11T13:17:53+00:00" + } + ], + "packages-dev": [ + { + "name": "myclabs/deep-copy", + "version": "1.13.4", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/07d290f0c47959fd5eed98c95ee5602db07e0b6a", + "reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "conflict": { + "doctrine/collections": "<1.6.8", + "doctrine/common": "<2.13.3 || >=3 <3.2.2" + }, + "require-dev": { + "doctrine/collections": "^1.6.8", + "doctrine/common": "^2.13.3 || ^3.2.2", + "phpspec/prophecy": "^1.10", + "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" + }, + "type": "library", + "autoload": { + "files": [ + "src/DeepCopy/deep_copy.php" + ], + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "support": { + "issues": "https://github.com/myclabs/DeepCopy/issues", + "source": "https://github.com/myclabs/DeepCopy/tree/1.13.4" + }, + "funding": [ + { + "url": "https://tidelift.com/funding/github/packagist/myclabs/deep-copy", + "type": "tidelift" + } + ], + "time": "2025-08-01T08:46:24+00:00" + }, + { + "name": "nikic/php-parser", + "version": "v5.7.0", + "source": { + "type": "git", + "url": "https://github.com/nikic/PHP-Parser.git", + "reference": "dca41cd15c2ac9d055ad70dbfd011130757d1f82" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/dca41cd15c2ac9d055ad70dbfd011130757d1f82", + "reference": "dca41cd15c2ac9d055ad70dbfd011130757d1f82", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-json": "*", + "ext-tokenizer": "*", + "php": ">=7.4" + }, + "require-dev": { + "ircmaxell/php-yacc": "^0.0.7", + "phpunit/phpunit": "^9.0" + }, + "bin": [ + "bin/php-parse" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.x-dev" + } + }, + "autoload": { + "psr-4": { + "PhpParser\\": "lib/PhpParser" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov" + } + ], + "description": "A PHP parser written in PHP", + "keywords": [ + "parser", + "php" + ], + "support": { + "issues": "https://github.com/nikic/PHP-Parser/issues", + "source": "https://github.com/nikic/PHP-Parser/tree/v5.7.0" + }, + "time": "2025-12-06T11:56:16+00:00" + }, + { + "name": "phar-io/manifest", + "version": "2.0.4", + "source": { + "type": "git", + "url": "https://github.com/phar-io/manifest.git", + "reference": "54750ef60c58e43759730615a392c31c80e23176" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/54750ef60c58e43759730615a392c31c80e23176", + "reference": "54750ef60c58e43759730615a392c31c80e23176", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-libxml": "*", + "ext-phar": "*", + "ext-xmlwriter": "*", + "phar-io/version": "^3.0.1", + "php": "^7.2 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "support": { + "issues": "https://github.com/phar-io/manifest/issues", + "source": "https://github.com/phar-io/manifest/tree/2.0.4" + }, + "funding": [ + { + "url": "https://github.com/theseer", + "type": "github" + } + ], + "time": "2024-03-03T12:33:53+00:00" + }, + { + "name": "phar-io/version", + "version": "3.2.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/version.git", + "reference": "4f7fd7836c6f332bb2933569e566a0d6c4cbed74" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74", + "reference": "4f7fd7836c6f332bb2933569e566a0d6c4cbed74", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Library for handling version information and constraints", + "support": { + "issues": "https://github.com/phar-io/version/issues", + "source": "https://github.com/phar-io/version/tree/3.2.1" + }, + "time": "2022-02-21T01:04:05+00:00" + }, + { + "name": "phpunit/php-code-coverage", + "version": "10.1.16", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "7e308268858ed6baedc8704a304727d20bc07c77" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/7e308268858ed6baedc8704a304727d20bc07c77", + "reference": "7e308268858ed6baedc8704a304727d20bc07c77", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-libxml": "*", + "ext-xmlwriter": "*", + "nikic/php-parser": "^4.19.1 || ^5.1.0", + "php": ">=8.1", + "phpunit/php-file-iterator": "^4.1.0", + "phpunit/php-text-template": "^3.0.1", + "sebastian/code-unit-reverse-lookup": "^3.0.0", + "sebastian/complexity": "^3.2.0", + "sebastian/environment": "^6.1.0", + "sebastian/lines-of-code": "^2.0.2", + "sebastian/version": "^4.0.1", + "theseer/tokenizer": "^1.2.3" + }, + "require-dev": { + "phpunit/phpunit": "^10.1" + }, + "suggest": { + "ext-pcov": "PHP extension that provides line coverage", + "ext-xdebug": "PHP extension that provides line coverage as well as branch and path coverage" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "10.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", + "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.16" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2024-08-22T04:31:57+00:00" + }, + { + "name": "phpunit/php-file-iterator", + "version": "4.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "a95037b6d9e608ba092da1b23931e537cadc3c3c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/a95037b6d9e608ba092da1b23931e537cadc3c3c", + "reference": "a95037b6d9e608ba092da1b23931e537cadc3c3c", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "4.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", + "security": "https://github.com/sebastianbergmann/php-file-iterator/security/policy", + "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/4.1.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-08-31T06:24:48+00:00" + }, + { + "name": "phpunit/php-invoker", + "version": "4.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-invoker.git", + "reference": "f5e568ba02fa5ba0ddd0f618391d5a9ea50b06d7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-invoker/zipball/f5e568ba02fa5ba0ddd0f618391d5a9ea50b06d7", + "reference": "f5e568ba02fa5ba0ddd0f618391d5a9ea50b06d7", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "ext-pcntl": "*", + "phpunit/phpunit": "^10.0" + }, + "suggest": { + "ext-pcntl": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "4.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Invoke callables with a timeout", + "homepage": "https://github.com/sebastianbergmann/php-invoker/", + "keywords": [ + "process" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-invoker/issues", + "source": "https://github.com/sebastianbergmann/php-invoker/tree/4.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T06:56:09+00:00" + }, + { + "name": "phpunit/php-text-template", + "version": "3.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "0c7b06ff49e3d5072f057eb1fa59258bf287a748" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/0c7b06ff49e3d5072f057eb1fa59258bf287a748", + "reference": "0c7b06ff49e3d5072f057eb1fa59258bf287a748", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-text-template/issues", + "security": "https://github.com/sebastianbergmann/php-text-template/security/policy", + "source": "https://github.com/sebastianbergmann/php-text-template/tree/3.0.1" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-08-31T14:07:24+00:00" + }, + { + "name": "phpunit/php-timer", + "version": "6.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "e2a2d67966e740530f4a3343fe2e030ffdc1161d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/e2a2d67966e740530f4a3343fe2e030ffdc1161d", + "reference": "e2a2d67966e740530f4a3343fe2e030ffdc1161d", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "6.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-timer/issues", + "source": "https://github.com/sebastianbergmann/php-timer/tree/6.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T06:57:52+00:00" + }, + { + "name": "phpunit/phpunit", + "version": "10.5.60", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "f2e26f52f80ef77832e359205f216eeac00e320c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f2e26f52f80ef77832e359205f216eeac00e320c", + "reference": "f2e26f52f80ef77832e359205f216eeac00e320c", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", + "ext-xmlwriter": "*", + "myclabs/deep-copy": "^1.13.4", + "phar-io/manifest": "^2.0.4", + "phar-io/version": "^3.2.1", + "php": ">=8.1", + "phpunit/php-code-coverage": "^10.1.16", + "phpunit/php-file-iterator": "^4.1.0", + "phpunit/php-invoker": "^4.0.0", + "phpunit/php-text-template": "^3.0.1", + "phpunit/php-timer": "^6.0.0", + "sebastian/cli-parser": "^2.0.1", + "sebastian/code-unit": "^2.0.0", + "sebastian/comparator": "^5.0.4", + "sebastian/diff": "^5.1.1", + "sebastian/environment": "^6.1.0", + "sebastian/exporter": "^5.1.4", + "sebastian/global-state": "^6.0.2", + "sebastian/object-enumerator": "^5.0.0", + "sebastian/recursion-context": "^5.0.1", + "sebastian/type": "^4.0.0", + "sebastian/version": "^4.0.1" + }, + "suggest": { + "ext-soap": "To be able to generate mocks based on WSDL files" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "10.5-dev" + } + }, + "autoload": { + "files": [ + "src/Framework/Assert/Functions.php" + ], + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/phpunit/issues", + "security": "https://github.com/sebastianbergmann/phpunit/security/policy", + "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.60" + }, + "funding": [ + { + "url": "https://phpunit.de/sponsors.html", + "type": "custom" + }, + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit", + "type": "tidelift" + } + ], + "time": "2025-12-06T07:50:42+00:00" + }, + { + "name": "sebastian/cli-parser", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/cli-parser.git", + "reference": "c34583b87e7b7a8055bf6c450c2c77ce32a24084" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/cli-parser/zipball/c34583b87e7b7a8055bf6c450c2c77ce32a24084", + "reference": "c34583b87e7b7a8055bf6c450c2c77ce32a24084", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library for parsing CLI options", + "homepage": "https://github.com/sebastianbergmann/cli-parser", + "support": { + "issues": "https://github.com/sebastianbergmann/cli-parser/issues", + "security": "https://github.com/sebastianbergmann/cli-parser/security/policy", + "source": "https://github.com/sebastianbergmann/cli-parser/tree/2.0.1" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2024-03-02T07:12:49+00:00" + }, + { + "name": "sebastian/code-unit", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit.git", + "reference": "a81fee9eef0b7a76af11d121767abc44c104e503" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit/zipball/a81fee9eef0b7a76af11d121767abc44c104e503", + "reference": "a81fee9eef0b7a76af11d121767abc44c104e503", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Collection of value objects that represent the PHP code units", + "homepage": "https://github.com/sebastianbergmann/code-unit", + "support": { + "issues": "https://github.com/sebastianbergmann/code-unit/issues", + "source": "https://github.com/sebastianbergmann/code-unit/tree/2.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T06:58:43+00:00" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "5e3a687f7d8ae33fb362c5c0743794bbb2420a1d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/5e3a687f7d8ae33fb362c5c0743794bbb2420a1d", + "reference": "5e3a687f7d8ae33fb362c5c0743794bbb2420a1d", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "support": { + "issues": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/issues", + "source": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/tree/3.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T06:59:15+00:00" + }, + { + "name": "sebastian/comparator", + "version": "5.0.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "e8e53097718d2b53cfb2aa859b06a41abf58c62e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/e8e53097718d2b53cfb2aa859b06a41abf58c62e", + "reference": "e8e53097718d2b53cfb2aa859b06a41abf58c62e", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-mbstring": "*", + "php": ">=8.1", + "sebastian/diff": "^5.0", + "sebastian/exporter": "^5.0" + }, + "require-dev": { + "phpunit/phpunit": "^10.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "5.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "https://github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/comparator/issues", + "security": "https://github.com/sebastianbergmann/comparator/security/policy", + "source": "https://github.com/sebastianbergmann/comparator/tree/5.0.4" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/comparator", + "type": "tidelift" + } + ], + "time": "2025-09-07T05:25:07+00:00" + }, + { + "name": "sebastian/complexity", + "version": "3.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/complexity.git", + "reference": "68ff824baeae169ec9f2137158ee529584553799" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/68ff824baeae169ec9f2137158ee529584553799", + "reference": "68ff824baeae169ec9f2137158ee529584553799", + "shasum": "" + }, + "require": { + "nikic/php-parser": "^4.18 || ^5.0", + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.2-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library for calculating the complexity of PHP code units", + "homepage": "https://github.com/sebastianbergmann/complexity", + "support": { + "issues": "https://github.com/sebastianbergmann/complexity/issues", + "security": "https://github.com/sebastianbergmann/complexity/security/policy", + "source": "https://github.com/sebastianbergmann/complexity/tree/3.2.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-12-21T08:37:17+00:00" + }, + { + "name": "sebastian/diff", + "version": "5.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "c41e007b4b62af48218231d6c2275e4c9b975b2e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/c41e007b4b62af48218231d6c2275e4c9b975b2e", + "reference": "c41e007b4b62af48218231d6c2275e4c9b975b2e", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0", + "symfony/process": "^6.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "5.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff", + "udiff", + "unidiff", + "unified diff" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/diff/issues", + "security": "https://github.com/sebastianbergmann/diff/security/policy", + "source": "https://github.com/sebastianbergmann/diff/tree/5.1.1" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2024-03-02T07:15:17+00:00" + }, + { + "name": "sebastian/environment", + "version": "6.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "8074dbcd93529b357029f5cc5058fd3e43666984" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/8074dbcd93529b357029f5cc5058fd3e43666984", + "reference": "8074dbcd93529b357029f5cc5058fd3e43666984", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "suggest": { + "ext-posix": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "6.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "https://github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/environment/issues", + "security": "https://github.com/sebastianbergmann/environment/security/policy", + "source": "https://github.com/sebastianbergmann/environment/tree/6.1.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2024-03-23T08:47:14+00:00" + }, + { + "name": "sebastian/exporter", + "version": "5.1.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "0735b90f4da94969541dac1da743446e276defa6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/0735b90f4da94969541dac1da743446e276defa6", + "reference": "0735b90f4da94969541dac1da743446e276defa6", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=8.1", + "sebastian/recursion-context": "^5.0" + }, + "require-dev": { + "phpunit/phpunit": "^10.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "5.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "https://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/exporter/issues", + "security": "https://github.com/sebastianbergmann/exporter/security/policy", + "source": "https://github.com/sebastianbergmann/exporter/tree/5.1.4" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/exporter", + "type": "tidelift" + } + ], + "time": "2025-09-24T06:09:11+00:00" + }, + { + "name": "sebastian/global-state", + "version": "6.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "987bafff24ecc4c9ac418cab1145b96dd6e9cbd9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/987bafff24ecc4c9ac418cab1145b96dd6e9cbd9", + "reference": "987bafff24ecc4c9ac418cab1145b96dd6e9cbd9", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "sebastian/object-reflector": "^3.0", + "sebastian/recursion-context": "^5.0" + }, + "require-dev": { + "ext-dom": "*", + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "6.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "https://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "support": { + "issues": "https://github.com/sebastianbergmann/global-state/issues", + "security": "https://github.com/sebastianbergmann/global-state/security/policy", + "source": "https://github.com/sebastianbergmann/global-state/tree/6.0.2" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2024-03-02T07:19:19+00:00" + }, + { + "name": "sebastian/lines-of-code", + "version": "2.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/lines-of-code.git", + "reference": "856e7f6a75a84e339195d48c556f23be2ebf75d0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/856e7f6a75a84e339195d48c556f23be2ebf75d0", + "reference": "856e7f6a75a84e339195d48c556f23be2ebf75d0", + "shasum": "" + }, + "require": { + "nikic/php-parser": "^4.18 || ^5.0", + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library for counting the lines of code in PHP source code", + "homepage": "https://github.com/sebastianbergmann/lines-of-code", + "support": { + "issues": "https://github.com/sebastianbergmann/lines-of-code/issues", + "security": "https://github.com/sebastianbergmann/lines-of-code/security/policy", + "source": "https://github.com/sebastianbergmann/lines-of-code/tree/2.0.2" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-12-21T08:38:20+00:00" + }, + { + "name": "sebastian/object-enumerator", + "version": "5.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "202d0e344a580d7f7d04b3fafce6933e59dae906" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/202d0e344a580d7f7d04b3fafce6933e59dae906", + "reference": "202d0e344a580d7f7d04b3fafce6933e59dae906", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "sebastian/object-reflector": "^3.0", + "sebastian/recursion-context": "^5.0" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "5.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "support": { + "issues": "https://github.com/sebastianbergmann/object-enumerator/issues", + "source": "https://github.com/sebastianbergmann/object-enumerator/tree/5.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T07:08:32+00:00" + }, + { + "name": "sebastian/object-reflector", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-reflector.git", + "reference": "24ed13d98130f0e7122df55d06c5c4942a577957" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/24ed13d98130f0e7122df55d06c5c4942a577957", + "reference": "24ed13d98130f0e7122df55d06c5c4942a577957", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Allows reflection of object attributes, including inherited and non-public ones", + "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "support": { + "issues": "https://github.com/sebastianbergmann/object-reflector/issues", + "source": "https://github.com/sebastianbergmann/object-reflector/tree/3.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T07:06:18+00:00" + }, + { + "name": "sebastian/recursion-context", + "version": "5.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "47e34210757a2f37a97dcd207d032e1b01e64c7a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/47e34210757a2f37a97dcd207d032e1b01e64c7a", + "reference": "47e34210757a2f37a97dcd207d032e1b01e64c7a", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "5.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "https://github.com/sebastianbergmann/recursion-context", + "support": { + "issues": "https://github.com/sebastianbergmann/recursion-context/issues", + "security": "https://github.com/sebastianbergmann/recursion-context/security/policy", + "source": "https://github.com/sebastianbergmann/recursion-context/tree/5.0.1" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/recursion-context", + "type": "tidelift" + } + ], + "time": "2025-08-10T07:50:56+00:00" + }, + { + "name": "sebastian/type", + "version": "4.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/type.git", + "reference": "462699a16464c3944eefc02ebdd77882bd3925bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/462699a16464c3944eefc02ebdd77882bd3925bf", + "reference": "462699a16464c3944eefc02ebdd77882bd3925bf", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "4.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Collection of value objects that represent the types of the PHP type system", + "homepage": "https://github.com/sebastianbergmann/type", + "support": { + "issues": "https://github.com/sebastianbergmann/type/issues", + "source": "https://github.com/sebastianbergmann/type/tree/4.0.0" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-03T07:10:45+00:00" + }, + { + "name": "sebastian/version", + "version": "4.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "c51fa83a5d8f43f1402e3f32a005e6262244ef17" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/c51fa83a5d8f43f1402e3f32a005e6262244ef17", + "reference": "c51fa83a5d8f43f1402e3f32a005e6262244ef17", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "4.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "support": { + "issues": "https://github.com/sebastianbergmann/version/issues", + "source": "https://github.com/sebastianbergmann/version/tree/4.0.1" + }, + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2023-02-07T11:34:05+00:00" + }, + { + "name": "theseer/tokenizer", + "version": "1.3.1", + "source": { + "type": "git", + "url": "https://github.com/theseer/tokenizer.git", + "reference": "b7489ce515e168639d17feec34b8847c326b0b3c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/b7489ce515e168639d17feec34b8847c326b0b3c", + "reference": "b7489ce515e168639d17feec34b8847c326b0b3c", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": "^7.2 || ^8.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + } + ], + "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", + "support": { + "issues": "https://github.com/theseer/tokenizer/issues", + "source": "https://github.com/theseer/tokenizer/tree/1.3.1" + }, + "funding": [ + { + "url": "https://github.com/theseer", + "type": "github" + } + ], + "time": "2025-11-17T20:03:58+00:00" + } + ], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": {}, + "prefer-stable": true, + "prefer-lowest": false, + "platform": { + "php": ">=8.1", + "ext-mongodb": "*" + }, + "platform-dev": {}, + "plugin-api-version": "2.6.0" +} diff --git a/examples/mysql_join_eager_demo.php b/examples/mysql_join_eager_demo.php new file mode 100644 index 0000000..27712e1 --- /dev/null +++ b/examples/mysql_join_eager_demo.php @@ -0,0 +1,106 @@ + 'mysql', + 'host' => getenv('MYSQL_HOST') ?: '127.0.0.1', + 'port' => (int)(getenv('MYSQL_PORT') ?: 3306), + 'database' => getenv('MYSQL_DB') ?: 'app', + 'username' => getenv('MYSQL_USER') ?: 'root', + 'password' => getenv('MYSQL_PASS') ?: 'secret', + 'charset' => 'utf8mb4', +]); + +// Ensure demo tables (idempotent) +$conn->execute('CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(190) NOT NULL +)'); +$conn->execute('CREATE TABLE IF NOT EXISTS posts ( + id INT AUTO_INCREMENT PRIMARY KEY, + user_id INT NOT NULL, + title VARCHAR(190) NOT NULL, + deleted_at DATETIME NULL +)'); + +class UserDto extends AbstractDto {} +class PostDto extends AbstractDto {} + +class PostDao extends AbstractDao { + public function getTable(): string { return 'posts'; } + protected function dtoClass(): string { return PostDto::class; } + protected function schema(): array { return [ + 'primaryKey' => 'id', + 'columns' => [ 'id'=>['cast'=>'int'], 'user_id'=>['cast'=>'int'], 'title'=>['cast'=>'string'], 'deleted_at'=>['cast'=>'datetime'] ], + 'softDeletes' => ['enabled' => true, 'deletedAt' => 'deleted_at'], + ]; } +} + +class UserDao extends AbstractDao { + public function getTable(): string { return 'users'; } + protected function dtoClass(): string { return UserDto::class; } + protected function relations(): array { + return [ + 'posts' => [ + 'type' => 'hasMany', + 'dao' => PostDao::class, + 'foreignKey' => 'user_id', + 'localKey' => 'id', + ], + ]; + } + protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'name'=>['cast'=>'string']]]; } +} + +$userDao = new UserDao($conn); +$postDao = new PostDao($conn); + +// Clean minimal (demo only) +foreach ($userDao->findAllBy() as $u) { $userDao->deleteById((int)$u->toArray(false)['id']); } +foreach ($postDao->findAllBy() as $p) { $postDao->deleteById((int)$p->toArray(false)['id']); } + +// Seed +$u1 = $userDao->insert(['name' => 'Alice']); +$u2 = $userDao->insert(['name' => 'Bob']); +$uid1 = (int)$u1->toArray(false)['id']; +$uid2 = (int)$u2->toArray(false)['id']; +$postDao->insert(['user_id' => $uid1, 'title' => 'P1']); +$postDao->insert(['user_id' => $uid1, 'title' => 'P2']); +$postDao->insert(['user_id' => $uid2, 'title' => 'Hidden', 'deleted_at' => gmdate('Y-m-d H:i:s')]); + +// Baseline portable eager (batched IN) +$batched = $userDao->fields('id','name','posts.title')->with(['posts'])->findAllBy([]); +echo "Batched eager: \n"; +foreach ($batched as $u) { + $arr = $u->toArray(false); + $titles = array_map(fn($p) => $p->toArray(false)['title'] ?? '', $arr['posts'] ?? []); + echo "- {$arr['name']} posts: " . implode(', ', $titles) . "\n"; +} + +// Join-based eager (global opt-in) +$joined = $userDao->fields('id','name','posts.title')->useJoinEager()->with(['posts'])->findAllBy([]); +echo "\nJoin eager (global): \n"; +foreach ($joined as $u) { + $arr = $u->toArray(false); + $titles = array_map(fn($p) => $p->toArray(false)['title'] ?? '', $arr['posts'] ?? []); + echo "- {$arr['name']} posts: " . implode(', ', $titles) . "\n"; +} + +// Per-relation join hint (equivalent in this single-rel case) +$hinted = $userDao->fields('id','name','posts.title') + ->with(['posts' => ['strategy' => 'join']]) + ->findAllBy([]); // will fallback to batched if conditions not met +echo "\nJoin eager (per-relation hint): \n"; +foreach ($hinted as $u) { + $arr = $u->toArray(false); + $titles = array_map(fn($p) => $p->toArray(false)['title'] ?? '', $arr['posts'] ?? []); + echo "- {$arr['name']} posts: " . implode(', ', $titles) . "\n"; +} diff --git a/src/Model/AbstractDao.php b/src/Model/AbstractDao.php index 6ede775..008bd25 100644 --- a/src/Model/AbstractDao.php +++ b/src/Model/AbstractDao.php @@ -5,6 +5,7 @@ namespace Pairity\Model; use Pairity\Contracts\ConnectionInterface; use Pairity\Contracts\DaoInterface; use Pairity\Orm\UnitOfWork; +use Pairity\Model\Casting\CasterInterface; abstract class AbstractDao implements DaoInterface { @@ -30,6 +31,12 @@ abstract class AbstractDao implements DaoInterface * @var array */ private array $withConstraints = []; + /** + * Optional per‑relation eager loading strategies for first‑level relations. + * Keys are relation names; values like 'join'. + * @var array + */ + private array $withStrategies = []; /** Soft delete include flags */ private bool $includeTrashed = false; private bool $onlyTrashed = false; @@ -37,6 +44,12 @@ abstract class AbstractDao implements DaoInterface private array $runtimeScopes = []; /** @var array */ private array $namedScopes = []; + /** + * Optional eager loading strategy for next find* call. + * null (default) uses the portable subquery/batched IN strategy. + * 'join' opts in to join-based eager loading for supported SQL relations (single level). + */ + private ?string $eagerStrategy = null; public function __construct(ConnectionInterface $connection) { @@ -94,6 +107,25 @@ abstract class AbstractDao implements DaoInterface return $this->connection; } + /** + * Opt-in to join-based eager loading for the next find* call (SQL only, single-level relations). + */ + public function useJoinEager(): static + { + $this->eagerStrategy = 'join'; + return $this; + } + + /** + * Set eager strategy explicitly: 'join' or 'subquery'. Resets after next find* call. + */ + public function eagerStrategy(string $strategy): static + { + $strategy = strtolower($strategy); + $this->eagerStrategy = in_array($strategy, ['join', 'subquery'], true) ? $strategy : null; + return $this; + } + /** @param array $criteria */ public function findOneBy(array $criteria): ?AbstractDto { @@ -101,14 +133,24 @@ abstract class AbstractDao implements DaoInterface $this->applyRuntimeScopesToCriteria($criteria); [$where, $bindings] = $this->buildWhere($criteria); $where = $this->appendScopedWhere($where); - $sql = 'SELECT ' . $this->selectList() . ' FROM ' . $this->getTable() . ($where ? ' WHERE ' . $where : '') . ' LIMIT 1'; - $rows = $this->connection->query($sql, $bindings); - $dto = isset($rows[0]) ? $this->hydrate($this->castRowFromStorage($rows[0])) : null; - if ($dto && $this->with) { - $this->attachRelations([$dto]); + $dto = null; + if ($this->with && $this->shouldUseJoinEager()) { + [$sql, $bindings2, $meta] = $this->buildJoinSelect($where, $bindings, 1, 0); + $rows = $this->connection->query($sql, $bindings2); + $list = $this->hydrateFromJoinRows($rows, $meta); + $dto = $list[0] ?? null; + } else { + $sql = 'SELECT ' . $this->selectList() . ' FROM ' . $this->getTable() . ($where ? ' WHERE ' . $where : '') . ' LIMIT 1'; + $rows = $this->connection->query($sql, $bindings); + $dto = isset($rows[0]) ? $this->hydrate($this->castRowFromStorage($rows[0])) : null; + if ($dto && $this->with) { + $this->attachRelations([$dto]); + } } $this->resetFieldSelections(); $this->resetRuntimeScopes(); + $this->eagerStrategy = null; // reset + $this->withStrategies = []; return $dto; } @@ -134,14 +176,22 @@ abstract class AbstractDao implements DaoInterface $this->applyRuntimeScopesToCriteria($criteria); [$where, $bindings] = $this->buildWhere($criteria); $where = $this->appendScopedWhere($where); - $sql = 'SELECT ' . $this->selectList() . ' FROM ' . $this->getTable() . ($where ? ' WHERE ' . $where : ''); - $rows = $this->connection->query($sql, $bindings); - $dtos = array_map(fn($r) => $this->hydrate($this->castRowFromStorage($r)), $rows); - if ($dtos && $this->with) { - $this->attachRelations($dtos); + if ($this->with && $this->shouldUseJoinEager()) { + [$sql2, $bindings2, $meta] = $this->buildJoinSelect($where, $bindings, null, null); + $rows = $this->connection->query($sql2, $bindings2); + $dtos = $this->hydrateFromJoinRows($rows, $meta); + } else { + $sql = 'SELECT ' . $this->selectList() . ' FROM ' . $this->getTable() . ($where ? ' WHERE ' . $where : ''); + $rows = $this->connection->query($sql, $bindings); + $dtos = array_map(fn($r) => $this->hydrate($this->castRowFromStorage($r)), $rows); + if ($dtos && $this->with) { + $this->attachRelations($dtos); + } } $this->resetFieldSelections(); $this->resetRuntimeScopes(); + $this->eagerStrategy = null; // reset + $this->withStrategies = []; return $dtos; } @@ -176,6 +226,8 @@ abstract class AbstractDao implements DaoInterface } $this->resetFieldSelections(); $this->resetRuntimeScopes(); + $this->eagerStrategy = null; // reset + $this->withStrategies = []; $lastPage = (int)max(1, (int)ceil($total / $perPage)); return [ @@ -209,6 +261,8 @@ abstract class AbstractDao implements DaoInterface if ($dtos && $this->with) { $this->attachRelations($dtos); } $this->resetFieldSelections(); $this->resetRuntimeScopes(); + $this->eagerStrategy = null; // reset + $this->withStrategies = []; return [ 'data' => $dtos, @@ -761,9 +815,189 @@ abstract class AbstractDao implements DaoInterface $this->with = []; $this->withTree = []; $this->withConstraints = []; + $this->withStrategies = []; // do not reset relationFields here; they may be reused by subsequent loads in the same call } + // ===== Join-based eager loading (opt-in, single-level) ===== + + /** Determine if join-based eager should be used for current with() selection. */ + private function shouldUseJoinEager(): bool + { + // Determine if join strategy is desired globally or per relation + $globalJoin = ($this->eagerStrategy === 'join'); + $perRelJoin = false; + if (!$globalJoin && $this->with) { + $allMarked = true; + foreach ($this->with as $rel) { + if (($this->withStrategies[$rel] ?? null) !== 'join') { $allMarked = false; break; } + } + $perRelJoin = $allMarked; + } + if (!$globalJoin && !$perRelJoin) return false; + // Only single-level paths supported in join MVP (no nested trees) + foreach ($this->withTree as $rel => $sub) { + if (!empty($sub)) return false; // nested present => fallback + } + // Require relationFields for each relation to know what to select safely + foreach ($this->with as $rel) { + if (!isset($this->relationFields[$rel]) || empty($this->relationFields[$rel])) { + return false; + } + } + return true; + } + + /** + * Build a SELECT with LEFT JOINs for the requested relations. + * Returns [sql, bindings, meta] where meta describes relation aliases and selected columns. + * @param ?int $limit + * @param ?int $offset + * @return array{0:string,1:array,2:array} + */ + private function buildJoinSelect(string $baseWhere, array $bindings, ?int $limit, ?int $offset): array + { + $t0 = 't0'; + $pk = $this->getPrimaryKey(); + // Base select: ensure PK is included + $baseCols = $this->selectedFields ?: ['*']; + if ($baseCols === ['*'] || !in_array($pk, $baseCols, true)) { + // Select * to keep behavior; PK is present implicitly + $baseSelect = "$t0.*"; + } else { + $quoted = array_map(fn($c) => "$t0.$c", $baseCols); + $baseSelect = implode(', ', $quoted); + } + + $selects = [ $baseSelect ]; + $joins = []; + $meta = [ 'rels' => [] ]; + + $relations = $this->relations(); + $aliasIndex = 1; + foreach ($this->with as $name) { + if (!isset($relations[$name])) continue; + $cfg = $relations[$name]; + $type = (string)($cfg['type'] ?? ''); + $daoClass = $cfg['dao'] ?? null; + if (!is_string($daoClass) || $type === '') continue; + /** @var class-string $daoClass */ + $relDao = new $daoClass($this->getConnection()); + $ta = 't' . $aliasIndex++; + $on = ''; + if ($type === 'hasMany' || $type === 'hasOne') { + $foreignKey = (string)($cfg['foreignKey'] ?? ''); + $localKey = (string)($cfg['localKey'] ?? 'id'); + if ($foreignKey === '') continue; + $on = "$ta.$foreignKey = $t0.$localKey"; + } elseif ($type === 'belongsTo') { + $foreignKey = (string)($cfg['foreignKey'] ?? ''); + $otherKey = (string)($cfg['otherKey'] ?? 'id'); + if ($foreignKey === '') continue; + $on = "$ta.$otherKey = $t0.$foreignKey"; + } else { + // belongsToMany not supported in join MVP + continue; + } + // Soft-delete scope for related in JOIN (append in ON) + if ($relDao->hasSoftDeletes()) { + $del = $relDao->softDeleteConfig()['deletedAt'] ?? 'deleted_at'; + $on .= " AND $ta.$del IS NULL"; + } + $joins[] = 'LEFT JOIN ' . $relDao->getTable() . ' ' . $ta . ' ON ' . $on; + // Select related fields with alias prefix + $relCols = $this->relationFields[$name] ?? []; + $pref = $name . '__'; + foreach ($relCols as $col) { + $selects[] = "$ta.$col AS `{$pref}{$col}`"; + } + $meta['rels'][$name] = [ 'alias' => $ta, 'type' => $type, 'dao' => $relDao, 'cols' => $relCols ]; + } + + $sql = 'SELECT ' . implode(', ', $selects) . ' FROM ' . $this->getTable() . ' ' . $t0; + if ($joins) { + $sql .= ' ' . implode(' ', $joins); + } + if ($baseWhere !== '') { + $sql .= ' WHERE ' . $baseWhere; + } + if ($limit !== null) { + $sql .= ' LIMIT ' . (int)$limit; + } + if ($offset !== null) { + $sql .= ' OFFSET ' . (int)$offset; + } + return [$sql, $bindings, $meta]; + } + + /** + * Hydrate DTOs from joined rows with aliased related columns. + * @param array> $rows + * @param array $meta + * @return array + */ + private function hydrateFromJoinRows(array $rows, array $meta): array + { + if (!$rows) return []; + $pk = $this->getPrimaryKey(); + $out = []; + $byId = []; + foreach ($rows as $row) { + // Split base and related segments (related segments are prefixed as rel__col) + $base = []; + $relSegments = []; + foreach ($row as $k => $v) { + if (is_string($k) && str_contains($k, '__')) { + [$rel, $col] = explode('__', $k, 2); + $relSegments[$rel][$col] = $v; + } else { + $base[$k] = $v; + } + } + $idVal = $base[$pk] ?? null; + if ($idVal === null) { + // cannot hydrate without PK; skip row + continue; + } + $idKey = (string)$idVal; + if (!isset($byId[$idKey])) { + $dto = $this->hydrate($this->castRowFromStorage($base)); + $byId[$idKey] = $dto; + $out[] = $dto; + } + $parent = $byId[$idKey]; + // Attach each relation if there are any non-null values + foreach (($meta['rels'] ?? []) as $name => $info) { + $seg = $relSegments[$name] ?? []; + // Detect empty (all null) + $allNull = true; + foreach ($seg as $vv) { if ($vv !== null) { $allNull = false; break; } } + if ($allNull) { + // Ensure default: hasMany => [], hasOne/belongsTo => null (only set if not already set) + if (!isset($parent->toArray(false)[$name])) { + if (($info['type'] ?? '') === 'hasMany') { $parent->setRelation($name, []); } + else { $parent->setRelation($name, null); } + } + continue; + } + /** @var AbstractDao $relDao */ + $relDao = $info['dao']; + // Cast and hydrate child DTO + $child = $relDao->hydrate($relDao->castRowFromStorage($seg)); + if (($info['type'] ?? '') === 'hasMany') { + $current = $parent->toArray(false)[$name] ?? []; + if (!is_array($current)) { $current = []; } + // Append; no dedup to keep simple + $current[] = $child; + $parent->setRelation($name, $current); + } else { + $parent->setRelation($name, $child); + } + } + } + return $out; + } + // ===== belongsToMany helpers (pivot operations) ===== /** @@ -861,15 +1095,23 @@ abstract class AbstractDao implements DaoInterface public function with(array $relations): static { // Accept ['rel', 'rel.child'] or ['rel' => callable, 'rel.child' => callable] + // Also accepts config arrays like ['rel' => ['strategy' => 'join']] and + // ['rel' => ['strategy' => 'join', 'constraint' => callable]] $names = []; $tree = []; foreach ($relations as $key => $value) { if (is_int($key)) { // plain name $path = (string)$value; $this->insertRelationPath($tree, $path); - } else { // constraint + } else { // constraint or config $path = (string)$key; - if (is_callable($value)) { + if (is_array($value)) { + $strategy = isset($value['strategy']) ? strtolower((string)$value['strategy']) : null; + if ($strategy) { $this->withStrategies[$path] = $strategy; } + if (isset($value['constraint']) && is_callable($value['constraint'])) { + $this->withConstraints[$path] = $value['constraint']; + } + } elseif (is_callable($value)) { $this->withConstraints[$path] = $value; } $this->insertRelationPath($tree, $path); @@ -1047,6 +1289,11 @@ abstract class AbstractDao implements DaoInterface private function castFromStorage(string $type, mixed $value): mixed { if ($value === null) return null; + // Support custom caster classes via class-string in schema 'cast' + $caster = $this->resolveCaster($type); + if ($caster) { + return $caster->fromStorage($value); + } switch ($type) { case 'int': return (int)$value; case 'float': return (float)$value; @@ -1120,6 +1367,11 @@ abstract class AbstractDao implements DaoInterface private function castForStorage(string $type, mixed $value): mixed { if ($value === null) return null; + // Support custom caster classes via class-string in schema 'cast' + $caster = $this->resolveCaster($type); + if ($caster) { + return $caster->toStorage($value); + } switch ($type) { case 'int': return (int)$value; case 'float': return (float)$value; @@ -1139,6 +1391,31 @@ abstract class AbstractDao implements DaoInterface } } + /** Cache for resolved caster instances. @var array */ + private array $casterCache = []; + + /** Resolve a caster from a type/class string. */ + private function resolveCaster(string $type): ?CasterInterface + { + // Not a class-string? return null to use built-ins + if (!class_exists($type)) { + return null; + } + if (isset($this->casterCache[$type])) { + return $this->casterCache[$type]; + } + try { + $obj = new $type(); + } catch (\Throwable) { + return null; + } + if ($obj instanceof CasterInterface) { + $this->casterCache[$type] = $obj; + return $obj; + } + return null; + } + private function nowString(): string { return gmdate('Y-m-d H:i:s'); diff --git a/src/Model/AbstractDto.php b/src/Model/AbstractDto.php index 00089a0..b1f7122 100644 --- a/src/Model/AbstractDto.php +++ b/src/Model/AbstractDto.php @@ -12,7 +12,15 @@ abstract class AbstractDto implements DtoInterface /** @param array $attributes */ public function __construct(array $attributes = []) { - $this->attributes = $attributes; + // Apply mutators if defined + foreach ($attributes as $key => $value) { + $method = $this->mutatorMethod($key); + if (method_exists($this, $method)) { + // set{Name}Attribute($value): mixed + $value = $this->{$method}($value); + } + $this->attributes[$key] = $value; + } } /** @param array $data */ @@ -23,7 +31,13 @@ abstract class AbstractDto implements DtoInterface public function __get(string $name): mixed { - return $this->attributes[$name] ?? null; + $value = $this->attributes[$name] ?? null; + $method = $this->accessorMethod($name); + if (method_exists($this, $method)) { + // get{Name}Attribute($value): mixed + return $this->{$method}($value); + } + return $value; } public function __isset(string $name): bool @@ -44,11 +58,26 @@ abstract class AbstractDto implements DtoInterface public function toArray(bool $deep = true): array { if (!$deep) { - return $this->attributes; + // Apply accessors at top level for scalar attributes + $out = []; + foreach ($this->attributes as $key => $value) { + $method = $this->accessorMethod($key); + if (method_exists($this, $method)) { + $out[$key] = $this->{$method}($value); + } else { + $out[$key] = $value; + } + } + return $out; } $result = []; foreach ($this->attributes as $key => $value) { + // Apply accessor before deep conversion for scalars/arrays + $method = $this->accessorMethod($key); + if (method_exists($this, $method)) { + $value = $this->{$method}($value); + } if ($value instanceof DtoInterface) { $result[$key] = $value->toArray(true); } elseif (is_array($value)) { @@ -66,4 +95,21 @@ abstract class AbstractDto implements DtoInterface return $result; } + + private function accessorMethod(string $key): string + { + return 'get' . $this->studly($key) . 'Attribute'; + } + + private function mutatorMethod(string $key): string + { + return 'set' . $this->studly($key) . 'Attribute'; + } + + private function studly(string $value): string + { + $value = str_replace(['-', '_'], ' ', $value); + $value = ucwords($value); + return str_replace(' ', '', $value); + } } diff --git a/src/Model/Casting/CasterInterface.php b/src/Model/Casting/CasterInterface.php new file mode 100644 index 0000000..c4d9e1c --- /dev/null +++ b/src/Model/Casting/CasterInterface.php @@ -0,0 +1,12 @@ + 'sqlite', 'path' => ':memory:']); + } + + public function testCustomCasterAndDtoAccessorsMutators(): void + { + $conn = $this->conn(); + // simple schema + $conn->execute('CREATE TABLE widgets ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT, + price_cents INTEGER, + meta TEXT + )'); + + // Custom caster for money cents <-> Money object (array for simplicity) + $moneyCasterClass = new class implements CasterInterface { + public function fromStorage(mixed $value): mixed { return ['cents' => (int)$value]; } + public function toStorage(mixed $value): mixed { + if (is_array($value) && isset($value['cents'])) { return (int)$value['cents']; } + return (int)$value; + } + }; + $moneyCasterFqcn = get_class($moneyCasterClass); + + // DTO with accessor/mutator for name (capitalize on get, trim on set) + $Dto = new class([]) extends AbstractDto { + protected function getNameAttribute($value): mixed { return is_string($value) ? strtoupper($value) : $value; } + protected function setNameAttribute($value): mixed { return is_string($value) ? trim($value) : $value; } + }; + $dtoClass = get_class($Dto); + + $Dao = new class($conn, $dtoClass, $moneyCasterFqcn) extends AbstractDao { + private string $dto; private string $caster; + public function __construct($c, string $dto, string $caster) { parent::__construct($c); $this->dto = $dto; $this->caster = $caster; } + public function getTable(): string { return 'widgets'; } + protected function dtoClass(): string { return $this->dto; } + protected function schema(): array + { + return [ + 'primaryKey' => 'id', + 'columns' => [ + 'id' => ['cast' => 'int'], + 'name' => ['cast' => 'string'], + 'price_cents' => ['cast' => $this->caster], // custom caster + 'meta' => ['cast' => 'json'], + ], + ]; + } + }; + + $dao = new $Dao($conn, $dtoClass, $moneyCasterFqcn); + + // Insert with mutator (name will be trimmed) and caster (price array -> storage int) + $created = $dao->insert([ + 'name' => ' gizmo ', + 'price_cents' => ['cents' => 1234], + 'meta' => ['color' => 'red'] + ]); + $arr = $created->toArray(false); + $this->assertSame('GIZMO', $arr['name']); // accessor uppercases + $this->assertIsArray($arr['price_cents']); + $this->assertSame(1234, $arr['price_cents']['cents']); // fromStorage via caster + $this->assertSame('red', $arr['meta']['color']); + + $id = $arr['id']; + + // Update with caster value + $updated = $dao->update($id, ['price_cents' => ['cents' => 1999]]); + $this->assertSame(1999, $updated->toArray(false)['price_cents']['cents']); + + // Verify raw storage is int (select directly) + $raw = $conn->query('SELECT price_cents, meta, name FROM widgets WHERE id = :id', ['id' => $id])[0] ?? []; + $this->assertSame(1999, (int)$raw['price_cents']); + $this->assertIsString($raw['meta']); + $this->assertSame('gizmo', strtolower((string)$raw['name'])); + } +} diff --git a/tests/JoinEagerMysqlTest.php b/tests/JoinEagerMysqlTest.php new file mode 100644 index 0000000..51d55c7 --- /dev/null +++ b/tests/JoinEagerMysqlTest.php @@ -0,0 +1,125 @@ +markTestSkipped('MYSQL_HOST not set; skipping MySQL join eager test'); + } + return [ + 'driver' => 'mysql', + 'host' => $host, + 'port' => (int)(getenv('MYSQL_PORT') ?: 3306), + 'database' => getenv('MYSQL_DB') ?: 'pairity', + 'username' => getenv('MYSQL_USER') ?: 'root', + 'password' => getenv('MYSQL_PASS') ?: 'root', + 'charset' => 'utf8mb4', + ]; + } + + public function testJoinEagerHasManyAndBelongsTo(): void + { + $cfg = $this->mysqlConfig(); + $conn = ConnectionManager::make($cfg); + $schema = SchemaManager::forConnection($conn); + + // Unique table names per run + $suf = substr(sha1((string)microtime(true)), 0, 6); + $usersT = 'je_users_' . $suf; + $postsT = 'je_posts_' . $suf; + + // Create tables + $schema->create($usersT, function (Blueprint $t) { $t->increments('id'); $t->string('name', 190); }); + $schema->create($postsT, function (Blueprint $t) { $t->increments('id'); $t->integer('user_id'); $t->string('title', 190); $t->datetime('deleted_at')->nullable(); }); + + // DTOs + $UserDto = new class([]) extends AbstractDto {}; + $PostDto = new class([]) extends AbstractDto {}; + $uClass = get_class($UserDto); $pClass = get_class($PostDto); + + // DAOs + $PostDao = new class($conn, $postsT, $pClass) extends AbstractDao { + private string $table; private string $dto; + public function __construct($c, string $table, string $dto) { parent::__construct($c); $this->table=$table; $this->dto=$dto; } + public function getTable(): string { return $this->table; } + protected function dtoClass(): string { return $this->dto; } + protected function schema(): array { return [ + 'primaryKey' => 'id', + 'columns' => [ 'id'=>['cast'=>'int'], 'user_id'=>['cast'=>'int'], 'title'=>['cast'=>'string'], 'deleted_at'=>['cast'=>'datetime'] ], + 'softDeletes' => ['enabled' => true, 'deletedAt' => 'deleted_at'], + ]; } + }; + + $UserDao = new class($conn, $usersT, $uClass, get_class($PostDao)) extends AbstractDao { + private string $table; private string $dto; private string $postDaoClass; + public function __construct($c, string $table, string $dto, string $postDaoClass) { parent::__construct($c); $this->table=$table; $this->dto=$dto; $this->postDaoClass=$postDaoClass; } + public function getTable(): string { return $this->table; } + protected function dtoClass(): string { return $this->dto; } + protected function relations(): array { return [ 'posts' => [ 'type'=>'hasMany', 'dao'=>$this->postDaoClass, 'foreignKey'=>'user_id', 'localKey'=>'id' ] ]; } + protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'name'=>['cast'=>'string']]]; } + }; + + $postDao = new $PostDao($conn, $postsT, $pClass); + $userDao = new $UserDao($conn, $usersT, $uClass, get_class($postDao)); + + // Seed + $u1 = $userDao->insert(['name' => 'Alice']); + $u2 = $userDao->insert(['name' => 'Bob']); + $uid1 = (int)$u1->toArray(false)['id']; + $uid2 = (int)$u2->toArray(false)['id']; + $postDao->insert(['user_id' => $uid1, 'title' => 'P1']); + $postDao->insert(['user_id' => $uid1, 'title' => 'P2']); + // soft-deleted child for Bob + $postDao->insert(['user_id' => $uid2, 'title' => 'Hidden', 'deleted_at' => gmdate('Y-m-d H:i:s')]); + + // Baseline batched eager + $baseline = $userDao->fields('id','name','posts.title')->with(['posts'])->findAllBy([]); + $this->assertCount(2, $baseline); + $postsAlice = $baseline[0]->toArray(false)['posts'] ?? []; + $this->assertIsArray($postsAlice); + $this->assertCount(2, $postsAlice); + + // Join-based eager (global) + $joined = $userDao->fields('id','name','posts.title')->useJoinEager()->with(['posts'])->findAllBy([]); + $this->assertCount(2, $joined); + foreach ($joined as $u) { + $posts = $u->toArray(false)['posts'] ?? []; + foreach ($posts as $p) { + $this->assertNotSame('Hidden', $p->toArray(false)['title'] ?? null); + } + } + + // belongsTo join: Posts -> User + $UserDao2 = get_class($userDao); + $PostDao2 = new class($conn, $postsT, $pClass, $UserDao2, $usersT, $uClass) extends AbstractDao { + private string $pTable; private string $dto; private string $userDaoClass; private string $uTable; private string $uDto; + public function __construct($c,string $pTable,string $dto,string $userDaoClass,string $uTable,string $uDto){ parent::__construct($c); $this->pTable=$pTable; $this->dto=$dto; $this->userDaoClass=$userDaoClass; $this->uTable=$uTable; $this->uDto=$uDto; } + public function getTable(): string { return $this->pTable; } + protected function dtoClass(): string { return $this->dto; } + protected function relations(): array { return [ 'user' => [ 'type'=>'belongsTo', 'dao'=>get_class(new class($this->getConnection(), $this->uTable, $this->uDto) extends AbstractDao { private string $t; private string $d; public function __construct($c,string $t,string $d){ parent::__construct($c); $this->t=$t; $this->d=$d; } public function getTable(): string { return $this->t; } protected function dtoClass(): string { return $this->d; } protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'name'=>['cast'=>'string']]]; } }), 'foreignKey'=>'user_id', 'otherKey'=>'id' ] ]; } + protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'user_id'=>['cast'=>'int'],'title'=>['cast'=>'string']]]; } + }; + $postDaoJ = new $PostDao2($conn, $postsT, $pClass, $UserDao2, $usersT, $uClass); + $rows = $postDaoJ->fields('id','title','user.name')->useJoinEager()->with(['user'])->findAllBy([]); + $this->assertNotEmpty($rows); + $arr = $rows[0]->toArray(false); + $this->assertArrayHasKey('user', $arr); + + // Cleanup + $schema->drop($usersT); + $schema->drop($postsT); + } +} diff --git a/tests/JoinEagerSqliteTest.php b/tests/JoinEagerSqliteTest.php new file mode 100644 index 0000000..d358d94 --- /dev/null +++ b/tests/JoinEagerSqliteTest.php @@ -0,0 +1,127 @@ + 'sqlite', 'path' => ':memory:']); + } + + public function testHasManyJoinEagerWithProjectionAndSoftDeleteScope(): void + { + $conn = $this->conn(); + // schema + $conn->execute('CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)'); + $conn->execute('CREATE TABLE posts (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, title TEXT, deleted_at TEXT NULL)'); + + // DTOs + $UserDto = new class([]) extends AbstractDto {}; + $PostDto = new class([]) extends AbstractDto {}; + $uClass = get_class($UserDto); $pClass = get_class($PostDto); + + // DAOs + $PostDao = new class($conn, $pClass) extends AbstractDao { + private string $dto; public function __construct($c, string $dto) { parent::__construct($c); $this->dto = $dto; } + public function getTable(): string { return 'posts'; } + protected function dtoClass(): string { return $this->dto; } + protected function schema(): array { return [ + 'primaryKey' => 'id', + 'columns' => [ 'id'=>['cast'=>'int'], 'user_id'=>['cast'=>'int'], 'title'=>['cast'=>'string'], 'deleted_at'=>['cast'=>'datetime'] ], + 'softDeletes' => ['enabled' => true, 'deletedAt' => 'deleted_at'], + ]; } + }; + + $UserDao = new class($conn, $uClass, get_class($PostDao)) extends AbstractDao { + private string $dto; private string $postDaoClass; public function __construct($c,string $dto,string $p){ parent::__construct($c); $this->dto=$dto; $this->postDaoClass=$p; } + public function getTable(): string { return 'users'; } + protected function dtoClass(): string { return $this->dto; } + protected function relations(): array { return [ + 'posts' => [ 'type' => 'hasMany', 'dao' => $this->postDaoClass, 'foreignKey' => 'user_id', 'localKey' => 'id' ], + ]; } + protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'name'=>['cast'=>'string']]]; } + }; + + $postDao = new $PostDao($conn, $pClass); + $userDao = new $UserDao($conn, $uClass, get_class($postDao)); + + // seed + $u1 = $userDao->insert(['name' => 'Alice']); + $u2 = $userDao->insert(['name' => 'Bob']); + $uid1 = (int)$u1->toArray(false)['id']; + $uid2 = (int)$u2->toArray(false)['id']; + $postDao->insert(['user_id' => $uid1, 'title' => 'P1']); + $postDao->insert(['user_id' => $uid1, 'title' => 'P2']); + $postDao->insert(['user_id' => $uid2, 'title' => 'Hidden', 'deleted_at' => gmdate('Y-m-d H:i:s')]); // soft-deleted + + // Batched (subquery) for baseline + $baseline = $userDao->fields('id','name','posts.title')->with(['posts'])->findAllBy([]); + $this->assertCount(2, $baseline); + $alice = $baseline[0]->toArray(false); + $this->assertIsArray($alice['posts'] ?? null); + $this->assertCount(2, $alice['posts']); + + // Join-based eager (opt-in). Requires relation field projection. + $joined = $userDao->fields('id','name','posts.title')->useJoinEager()->with(['posts'])->findAllBy([]); + $this->assertCount(2, $joined); + $aliceJ = $joined[0]->toArray(false); + $this->assertIsArray($aliceJ['posts'] ?? null); + $this->assertCount(2, $aliceJ['posts']); + + // Ensure soft-deleted child was filtered out via ON condition + foreach ($joined as $u) { + $posts = $u->toArray(false)['posts'] ?? []; + foreach ($posts as $p) { + $this->assertNotSame('Hidden', $p->toArray(false)['title'] ?? null); + } + } + } + + public function testBelongsToJoinEagerSingleLevel(): void + { + $conn = $this->conn(); + $conn->execute('CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)'); + $conn->execute('CREATE TABLE posts (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, title TEXT)'); + + $UserDto = new class([]) extends AbstractDto {}; + $PostDto = new class([]) extends AbstractDto {}; + $uClass = get_class($UserDto); $pClass = get_class($PostDto); + + $UserDao = new class($conn, $uClass) extends AbstractDao { + private string $dto; public function __construct($c,string $dto){ parent::__construct($c); $this->dto=$dto; } + public function getTable(): string { return 'users'; } + protected function dtoClass(): string { return $this->dto; } + protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'name'=>['cast'=>'string']]]; } + }; + $PostDao = new class($conn, $pClass, get_class($UserDao)) extends AbstractDao { + private string $dto; private string $userDaoClass; public function __construct($c,string $dto,string $u){ parent::__construct($c); $this->dto=$dto; $this->userDaoClass=$u; } + public function getTable(): string { return 'posts'; } + protected function dtoClass(): string { return $this->dto; } + protected function relations(): array { return [ + 'user' => [ 'type' => 'belongsTo', 'dao' => $this->userDaoClass, 'foreignKey' => 'user_id', 'otherKey' => 'id' ], + ]; } + protected function schema(): array { return ['primaryKey'=>'id','columns'=>['id'=>['cast'=>'int'],'user_id'=>['cast'=>'int'],'title'=>['cast'=>'string']]]; } + }; + + $userDao = new $UserDao($conn, $uClass); + $postDao = new $PostDao($conn, $pClass, get_class($userDao)); + + $u = $userDao->insert(['name' => 'Alice']); + $uid = (int)$u->toArray(false)['id']; + $p = $postDao->insert(['user_id' => $uid, 'title' => 'Hello']); + + $rows = $postDao->fields('id','title','user.name')->useJoinEager()->with(['user'])->findAllBy([]); + $this->assertNotEmpty($rows); + $arr = $rows[0]->toArray(false); + $this->assertSame('Hello', $arr['title']); + $this->assertSame('Alice', $arr['user']->toArray(false)['name'] ?? null); + } +}