|
3 | 3 | <img src="https://github.com/PrinsFrank/object-resolver/raw/main/docs/images/banner_light.png" alt="Banner"> |
4 | 4 | </picture> |
5 | 5 |
|
6 | | -# object-resolver |
| 6 | +# Object Resolver |
7 | 7 |
|
8 | 8 | [](https://github.com/PrinsFrank/object-resolver/blob/main/LICENSE) |
9 | 9 | [](https://github.com/PrinsFrank/object-resolver/blob/main/composer.json) |
|
13 | 13 |
|
14 | 14 | **Resolve objects from data from requests, json etc** |
15 | 15 |
|
| 16 | +## Setup |
| 17 | + |
| 18 | +> **Note** |
| 19 | +> Make sure you are running PHP 8.3 or higher to use this package |
| 20 | +
|
| 21 | +To start right away, run the following command in your composer project; |
| 22 | + |
| 23 | +```composer require prinsfrank/object-resolver``` |
| 24 | + |
| 25 | +Or for development only; |
| 26 | + |
| 27 | +```composer require prinsfrank/object-resolver --dev``` |
| 28 | + |
| 29 | +# Use cases |
| 30 | + |
| 31 | +## Handling incoming requests |
| 32 | + |
| 33 | +Given a simple login controller, we have the following request object: |
| 34 | + |
| 35 | +```php |
| 36 | +<?php declare(strict_types=1); |
| 37 | + |
| 38 | +readonly class LogInRequest { |
| 39 | + public function __construct( |
| 40 | + #[SensitiveParameter] private string $email, |
| 41 | + #[SensitiveParameter] private string $password, |
| 42 | + ) { |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +With a controller that looks like this: |
| 48 | + |
| 49 | +```php |
| 50 | +<?php declare(strict_types=1); |
| 51 | + |
| 52 | +readonly class LogInController { |
| 53 | + public function __invoke(LogInRequest $logInRequest){ |
| 54 | + // Handle authentication |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +We somehow need to automatically wire the incoming request based on the request data. That's where this package comes in! |
| 60 | + |
| 61 | +If there is a container available, we can then add a dynamic abstract concrete binding: |
| 62 | + |
| 63 | +```php |
| 64 | +$resolvedSet->add( |
| 65 | + new AbstractConcrete( |
| 66 | + $identifier, |
| 67 | + fn (ObjectResolver $objectResolver, Request $request) => $objectResolver->resolveFromParams($identifier, $request->params()), |
| 68 | + ) |
| 69 | +); |
| 70 | +``` |
| 71 | + |
| 72 | +## Casing conversion |
| 73 | + |
| 74 | +Because code conventions between different tech stacks might differ, it's possible to automatically convert between different casings. |
| 75 | + |
| 76 | +Let's say there's a form in HTML that has name `user_name`, but in the backend our model has parameter `$userName`. This can be automatically converted, by supplying the parameters `$enforcePropertyNameCasing` and `$convertFromParamKeyCasing`: |
| 77 | + |
| 78 | +```php |
| 79 | +$resolvedSet->add( |
| 80 | + new Concrete( |
| 81 | + $identifier, |
| 82 | + fn () => new ObjectResolver(Casing::camel, Casing::snake) |
| 83 | + ) |
| 84 | +); |
| 85 | +``` |
| 86 | + |
| 87 | +## Json from APIs etc |
| 88 | + |
| 89 | +TODO: write documentation |
0 commit comments