prinsfrank/object-resolver

From arrays/requests/json to typed and validated objects with ease

v0.0.3 2024-09-07 21:39 UTC

This package is auto-updated.

Last update: 2024-09-07 21:40:33 UTC


README

Object Resolver

GitHub PHP Version Support Packagist Downloads codecov PHPStan Level

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