Skip to content

A small trait to make it easier to validate the input to your Laravel commands.

License

Notifications You must be signed in to change notification settings

devlop/laravel-console-input-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version License

Laravel Console Input Validation

A small trait to make it easier to validate the input to your Laravel commands.

Installation

composer require devlop/laravel-console-input-validation

Usage

use Devlop\Laravel\Console\ValidateInput;
use Symfony\Component\Console\Input\InputInterface;

class DemoCommand extends Command
{
    use ValidateInput;

    /**
     * Validate the console command input.
     *
     * @throws InvalidArgumentException
     */
    protected function validate(InputInterface $input) : void
    {
        // Example using manual validation
        if (! is_numeric($input->getOption('limit'))) {
            throw new InvalidArgumentException('--limit must be numeric');
        }

        // Example using webmozarts/assert
        Assert::numeric($input->getOption('limit')); // assert that the --limit option got a numeric value
        Assert::greaterThan($input->getOption('limit'), 0); // assert that the --limit option get a value greater than 0
    }

    public function handle() : int
    {
        // ...
    }
}