-
Notifications
You must be signed in to change notification settings - Fork 1
Add Mapper facade and Dto::from() shortcut for heterogeneous sources #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de769d2
Add Mapper facade and Dto::from() shortcut for heterogeneous sources
dereuromark 12ca106
Drop template annotation on ObjectFactory::to() to avoid PHPStan narr…
dereuromark ec16565
Use self instead of static in anonymous class for final-construct CS …
dereuromark 2c2b016
Merge remote-tracking branch 'origin/master' into feature/mapper-facade
dereuromark b7f0d92
Reject list-shaped payloads and clarify only() source-key semantics
dereuromark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpCollective\Dto; | ||
|
|
||
| use InvalidArgumentException; | ||
| use JsonSerializable; | ||
| use PhpCollective\Dto\Dto\Dto; | ||
| use PhpCollective\Dto\Dto\FromArrayToArrayInterface; | ||
| use PhpCollective\Dto\Utility\Json; | ||
| use Throwable; | ||
|
|
||
| /** | ||
| * Unified entry point for hydrating DTOs from heterogeneous sources. | ||
| * | ||
| * Instead of picking the right constructor for each input shape | ||
| * (`createFromArray`, `fromUnserialized`, `new + fromArray`, ...), callers use | ||
| * a single `Mapper::map($source)->to(Target::class)` call and the facade | ||
| * figures out how to extract an array payload from the source. | ||
| * | ||
| * Supported sources out of the box: | ||
| * - `array` | ||
| * - `PhpCollective\Dto\Dto\Dto` instance (uses `touchedToArray()`) | ||
| * - `PhpCollective\Dto\Dto\FromArrayToArrayInterface` implementor | ||
| * - `JsonSerializable` implementor | ||
| * - JSON `string` | ||
| * - any plain `object` (via `get_object_vars()`) | ||
| * | ||
| * Example: | ||
| * ```php | ||
| * use PhpCollective\Dto\Mapper; | ||
| * | ||
| * $dto = Mapper::map($request->getParsedBody())->to(UserDto::class); | ||
| * $copy = Mapper::map($existingDto)->to(UserDto::class); | ||
| * $strict = Mapper::map($jsonString) | ||
| * ->ignoreMissing(false) | ||
| * ->withKeyType(Dto::TYPE_UNDERSCORED) | ||
| * ->to(UserDto::class); | ||
| * ``` | ||
| * | ||
| * For the common DTO-in, DTO-out case prefer the typed shortcut on the target | ||
| * DTO: `UserDto::from($source)`. It returns `static`, so PHPStan infers the | ||
| * concrete DTO type without template annotations. | ||
| */ | ||
| final class Mapper | ||
| { | ||
| /** | ||
| * Begin a fluent mapping from `$source`. | ||
| * | ||
| * @param mixed $source | ||
| * | ||
| * @return \PhpCollective\Dto\ObjectFactory | ||
| */ | ||
| public static function map(mixed $source): ObjectFactory | ||
| { | ||
| return new ObjectFactory($source); | ||
| } | ||
|
|
||
| /** | ||
| * Extract an array payload from an arbitrary supported source. | ||
| * | ||
| * Exposed as a static helper so `Dto::from()` and `ObjectFactory::to()` | ||
| * share the same detection logic. | ||
| * | ||
| * @param mixed $source | ||
| * | ||
| * @throws \InvalidArgumentException If the source shape is not supported. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public static function toArray(mixed $source): array | ||
| { | ||
| if (is_array($source)) { | ||
| self::assertAssociative($source, 'array'); | ||
|
|
||
| return $source; | ||
| } | ||
|
|
||
| if ($source instanceof Dto) { | ||
| return $source->touchedToArray(); | ||
| } | ||
|
|
||
| if ($source instanceof FromArrayToArrayInterface) { | ||
| return $source->toArray(); | ||
| } | ||
|
|
||
| if ($source instanceof JsonSerializable) { | ||
| $data = $source->jsonSerialize(); | ||
| if (is_array($data)) { | ||
| self::assertAssociative($data, 'JsonSerializable payload'); | ||
|
|
||
| return $data; | ||
| } | ||
| if (is_object($data)) { | ||
| return get_object_vars($data); | ||
| } | ||
|
|
||
| throw new InvalidArgumentException(sprintf( | ||
| 'JsonSerializable source returned unsupported payload of type "%s".', | ||
| get_debug_type($data), | ||
| )); | ||
| } | ||
|
|
||
| if (is_string($source)) { | ||
| try { | ||
| $decoded = (new Json())->decode($source, true); | ||
| } catch (Throwable $e) { | ||
| throw new InvalidArgumentException( | ||
| 'String source could not be decoded as JSON: ' . $e->getMessage(), | ||
| 0, | ||
| $e, | ||
| ); | ||
| } | ||
| if (!is_array($decoded)) { | ||
| throw new InvalidArgumentException( | ||
| 'String source could not be decoded as a JSON object into an array.', | ||
| ); | ||
| } | ||
| self::assertAssociative($decoded, 'JSON string'); | ||
|
|
||
|
dereuromark marked this conversation as resolved.
|
||
| return $decoded; | ||
| } | ||
|
|
||
| if (is_object($source)) { | ||
| return get_object_vars($source); | ||
| } | ||
|
|
||
| throw new InvalidArgumentException(sprintf( | ||
| 'Cannot map source of type "%s" — expected array, object, or JSON string.', | ||
| get_debug_type($source), | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Reject list arrays (e.g. `[1, 2, 3]` or JSON `"[1, 2]"`). DTO hydration | ||
| * operates on associative `array<string, mixed>` payloads and would later | ||
| * produce a confusing `TypeError` from `Dto::hasField(string)` if given an | ||
| * integer-keyed sequence. Failing here yields a clearer error at the | ||
| * actual misuse site. | ||
| * | ||
| * @param array<array-key, mixed> $data | ||
| * @param string $sourceDescription Human-readable source label for the message. | ||
| * | ||
| * @throws \InvalidArgumentException If `$data` is a list or has any non-string keys. | ||
| * | ||
| * @return void | ||
| */ | ||
| private static function assertAssociative(array $data, string $sourceDescription): void | ||
| { | ||
| if ($data === []) { | ||
| return; | ||
| } | ||
|
|
||
| if (array_is_list($data)) { | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Cannot map %s: expected an associative array keyed by field names, got a list.', | ||
| $sourceDescription, | ||
| )); | ||
| } | ||
|
|
||
| foreach ($data as $key => $_) { | ||
| if (!is_string($key)) { | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Cannot map %s: all keys must be strings, got "%s".', | ||
| $sourceDescription, | ||
| get_debug_type($key), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.