Search by

delolmo / valinor-console

delolmo

Map symfony/console arguments and options to typed PHP objects with cuyz/valinor.

Package info

github.com/delolmo/valinor-console

Language:Shell

pkg:composer/delolmo/valinor-console

Statistics

Installs: 1 009

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

2.6.2 2026-10-29 08:16 UTC

This package is auto-updated.

Last update: 2026-11-12 10:44:43 UTC


README

Packagist Version License: MIT

Use the InputInterface of symfony/console as a source for the cuyz/valinor mapper.

Requirements

  • PHP ~8.4.27 || ^8.5.12
  • cuyz/valinor ^2.6
  • symfony/console ^8.0

Installation

composer require delolmo/valinor-console

Example

use App\DTO\CustomObject;
use CuyZ\Valinor\Mapper\Source\Source;
use CuyZ\Valinor\MapperBuilder;
use DelOlmo\Valinor\Mapper\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
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // Create the source from the input
        $source = Source::iterable(new InputSource($input));

        // Create the mapper, using the desired configuration
        $mapper = new MapperBuilder()
            ->allowSuperfluousKeys()
            ->allowScalarValueCasting()
            ->mapper();

        // Map the source to whatever object makes sense
        $mapped = $mapper->map(CustomObject::class, $source);

        // Apply whatever business logic makes sense from here
        // ...

        return Command::SUCCESS;
    }
}

Final notes

  • Major versions of delolmo/valinor-console follow the major versions of cuyz/valinor.
  • Symfony adds several options to every command's input (help, quiet, verbose and others). Unless the target object declares them, the mapper needs allowSuperfluousKeys(), or mapping fails.
  • An argument and an option may share a name. The source then holds the argument's value.
  • Console values arrive as strings (or arrays of strings), so the mapper usually needs allowScalarValueCasting(). See the Valinor documentation on type strictness.