prinsfrank / object-resolver
From arrays/requests/json to typed and validated objects with ease
Fund package maintenance!
PrinsFrank
Requires
- php: ~8.3
- prinsfrank/enums: ^1.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.62
- phpstan/phpstan: ^1.11
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/phpunit: ^11.3
README
Object Resolver
Resolve objects from data from requests, json etc
Setup
Note Make sure you are running PHP 8.3 or higher to use this package
To start right away, run the following command in your composer project;
composer require prinsfrank/object-resolver
Or for development only;
composer require prinsfrank/object-resolver --dev
Use cases
Handling incoming requests
Given a simple login controller, we have the following request object:
<?php declare(strict_types=1); readonly class LogInRequest { public function __construct( #[SensitiveParameter] private string $email, #[SensitiveParameter] private string $password, ) { } }
With a controller that looks like this:
<?php declare(strict_types=1); readonly class LogInController { public function __invoke(LogInRequest $logInRequest){ // Handle authentication } }
We somehow need to automatically wire the incoming request based on the request data. That's where this package comes in!
If there is a container available, we can then add a dynamic abstract concrete binding:
final class RequestDataProvider implements ServiceProviderInterface { public function provides(string $identifier): bool { return is_a($identifier, RequestData::class, true); } public function register(string $identifier, DefinitionSet $resolvedSet): void { $resolvedSet->add( new Concrete( $identifier, static function (ObjectResolver $objectResolver, ServerRequestInterface $serverRequest) use ($identifier) { $requestData = match ($serverRequest->getMethod()) { 'GET' => $serverRequest->getQueryParams(), 'POST', 'PATCH', 'PUT' => $serverRequest->getParsedBody(), default => [], }; return $objectResolver->resolveFromParams($identifier, $requestData); }, ) ); } }
Casing conversion
Because code conventions between different tech stacks might differ, it's possible to automatically convert between different casings.
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
:
final class ObjectResolverServiceProvider implements ServiceProviderInterface { #[Override] public function provides(string $identifier): bool { return $identifier === ObjectResolver::class; } /** @throws InvalidArgumentException */ #[Override] public function register(string $identifier, DefinitionSet $resolvedSet): void { $resolvedSet->add( new Concrete( $identifier, fn () => new ObjectResolver(Casing::camel, Casing::snake) ) ); } }
Json from APIs etc
TODO: write documentation