PHP code example of danilovl / symfony-console-input-validation

1. Go to this page and download the library: Download danilovl/symfony-console-input-validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

danilovl / symfony-console-input-validation example snippets


return function (array $context): Application {
    $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

    return new \Danilovl\SymfonyConsoleInputValidation\Console\Application($kernel);
};

 declare(strict_types=1);

namespace App\Application\Command;

use Danilovl\SymfonyConsoleInputValidation\Console\Input\InputOption as InputOptionValidation;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\{
    InputOption,
    InputInterface
};
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'app:command-test')]
class TestCommand extends Command
{
    protected function configure(): void
    {
        $validation = static function (mixed $value): void {
            if (empty($value)) {
                return;
            }

            if (!in_array($value, ['encrypt', 'decrypt'])) {
                throw new InvalidOptionException(sprintf('"%s" is not a valid type.', $value));
            }
        };

        $inputOption = new InputOptionValidation(
            name: 'type',
            mode: InputOption::VALUE_REQUIRED,
            description: 'Encryption type.',
            suggestedValues: ['encrypt', 'decrypt'],
            validation: $validation
        );

        $this->getDefinition()->addOption($inputOption);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $input->getOption('type');

        return Command::SUCCESS;
    }
}