delolmo / valinor-console
There is no license information available for the latest version (1.7.1) of this package.
Use symfony/console as a Valinor source
1.7.1
2023-12-18 15:40 UTC
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- cuyz/valinor: ^1.2
- symfony/console: ^6.2 || ^7.0
Requires (Dev)
- ext-xdebug: *
- doctrine/coding-standard: ^12.0
- ergebnis/composer-normalize: ^2.39
- infection/infection: ^0.27
- 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.18
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.16
README
The following library allows using InputInterface
objects of the symfony/console component as a source for the cuyz/valinor library.
Installation
composer require delolmo/valinor-console
Example
use App\DTO\CustomObject; use CuyZ\Valinor\Mapper\Source\Source; use CuyZ\Valinor\MapperBuilder; use DelOlmo\Valinor\Mapping\Source\InputSource; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; final class CustomCommand extends Command { public function execute(InputInterface $input, OutputInterface $output): int { // Create the Source using the new InputSource $source = Source::iterable(new InputSource($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-console
will always matchcuyz/valinor
versions. Same goes for PHP versions. - When creating the Mapper object, it should always be taken into account that, by default, Symfony adds several options to the InputInterface object (i.e., help or verbosity levels). If
allowSuperfluousKeys
is not used, the mapping process will throw an exception - unless you consider these parameters in the object you are trying to map (App\DTO\CustomObject
in the above example). See Allow superflous keys for more information. - Although options and arguments cannot share the same name within the same Symfony command, it should be noted that, from an InputSource standpoint, arguments always take precedence over options. That is, if there is an argument and an option sharing the same name, InputSource will only use the argument's value for mapping purposes.
- Considering that Symfony command applications convert most of the fields to either strings or arrays, it is interesting to note that
enableFlexibleCasting
should also be configured in the Mapper. See Enabling flexible casting for more information.