takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Laravel の artisan コマンドの引数バリデーションを行う

artisan コマンドの引数バリデーションを行うパッケージ cerbero/command-validator についてのメモ

検証環境

Windows10 Home Edition (version 21H1)

$ php --version
PHP 7.4.9 (cli) (built: Aug  4 2020 11:52:41) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies
    with Xdebug v2.8.1, Copyright (c) 2002-2019, by Derick Rethans

$ php artisan --version
Laravel Framework 8.73.0

cerbero/command-validator パッケージについて

リクエストに適用するバリデーションルールと同じ記述方法で artisan コマンドの引数のバリデーションが出来るパッケージ

github.com

インストール方法

$ composer require cerbero/command-validator

検証用 artisan コマンド

検証用として 2 つの数値を引数に与えるとその和を表示する artisan コマンドを作成する

$ php artisan make:command Addition

app/Console/Commands/Addition.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Addition extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'addition {num1} {num2}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Addition of two numbers';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info($this->argument('num1') + $this->argument('num2'));
        return Command::SUCCESS;
    }
}

動作確認

$ php artisan addition 1 2
3

$ php artisan addition 1 2.5
3.5

$ php artisan addition 1.25 2.17
3.42

# 引数に数値以外を与えるとエラーになるためバリデーションで防止したい
$ php artisan addition 1 abc

   ErrorException

  A non-numeric value encountered

検証用 artisan コマンド (バリデーションあり)

cerbero/command-validator を使って引数をバリデーションするように変更

app/Console/Commands/Addition.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Cerbero\CommandValidator\ValidatesInput;

class Addition extends Command
{
    use ValidatesInput;

    protected function rules()
    {
        return [
            'num1' => 'numeric',
            'num2' => 'numeric',
        ];
    }

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'addition {num1} {num2}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Addition of two numbers';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info($this->argument('num1') + $this->argument('num2'));
        return Command::SUCCESS;
    }
}

動作確認

$ php artisan addition 1 2
3

$ php artisan addition 1 2.5
3.5

$ php artisan addition 1.25 2.17
3.42

$ php artisan addition 1 abc


  The num2 must be a number.

バリデーションによって数値以外を受け付けないことを確認