delolmo / valinor-http-message
There is no license information available for the latest version (1.7.1) of this package.
Use psr/http-message as a Valinor source
1.7.1
2023-11-17 08:40 UTC
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- cuyz/valinor: ^1.7
- psr/http-message: ~1.0 || ~2.0
Requires (Dev)
- ext-xdebug: *
- doctrine/coding-standard: ^10.0
- ergebnis/composer-normalize: ^2.29
- infection/infection: ^0.26
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.9
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.4
- phpunit/phpunit: ^10.4
- rector/rector: ^0.15.10
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.15
README
The following library allows using ServerRequestInterface
objects of the psr/http-message library as a source for the cuyz/valinor library.
Installation
composer require delolmo/valinor-http-message
Example
use App\DTO\CustomObject; use CuyZ\Valinor\Mapper\Source\Source; use CuyZ\Valinor\MapperBuilder; use DelOlmo\Valinor\Mapping\Source\ServerRequestSource; use Psr\Http\Message\ServerRequestInterface; final class CustomController { public function execute(ServerRequestInterface $request) { // Create the Source using the new InputSource $source = Source::iterable(new ServerRequestSource($input)); // Create the Mapper, using the desired configuration $mapper = new MapperBuilder()) ->allowSuperfluousKeys() ->enableFlexibleCasting() ->mapper(); // Map the source to whatever object makes sense $mapped = $mapper->map(CustomObject::class, $source); // Apply whatever business logic makes sense from here // ... } }
Final notes
- Versioning of
delolmo/valinor-http-message
will always matchcuyz/valinor
versions. Same goes for PHP versions. - Although query params and body params should not share the same name in the Request, it should be noted that, from a ServerRequestSource standpoint, query params always take precedence over body params. That is, if there is a query param and a body param sharing the same name, ServerRequestSource will only use the query param's value for mapping purposes.
- Considering that many query and body params are retrieved as strings or arrays, it is interesting to note that
enableFlexibleCasting
should also be configured in the Mapper. See Enabling flexible casting for more information.