PHP code example of perryvandermeer / laravel-console-validator

1. Go to this page and download the library: Download perryvandermeer/laravel-console-validator 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/ */

    

perryvandermeer / laravel-console-validator example snippets



 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use PerryvanderMeer\LaravelConsoleValidator\ValidatesArguments;
 
class ExampleCommand extends Command
{
    use ValidatesArguments;
    
    /**
     * Set the validation rules that apply to the command.
     *
     * @var array<string, array|string>
     */
    protected array $rules = [
        'foo' => ['


 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use PerryvanderMeer\LaravelConsoleValidator\ValidatesArguments;
 
class ExampleCommand extends Command
{
    use ValidatesArguments;
}

/**
 * Set the validation rules that apply to the command.
 *
 * @var array<string, array|string>
 */
protected array $rules = [
    'title' => ['

/**
 * Get the validation rules that apply to the command.
 *
 * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
 */
protected function rules() : array
{
    return [
        'title' => ['

/**
 * Set the error messages for the defined validation rules.
 *
 * @var array<string, string>
 */
protected array $messages = [
    'title' => 'Whoo general message for title argument..!',
    'content.min' => 'Hmm the content is very short..!',
];

/**
 * Get the error messages for the defined validation rules.
 *
 * @return array<string, string>
 */
protected function messages() : array
{
    return [
        'title' => 'Whoo general message for title argument..!',
        'content.min' => 'Hmm the content is very short..!',
    ];   
}

/**
 * Set custom attributes for validator errors.
 *
 * @return array<string, string>
 */
protected array $attributes = [
    'title' => 'custom title',
    'content' => 'custom content',
];

/**
 * Get custom attributes for validator errors.
 *
 * @return array<string, string>
 */
protected function attributes() : array
{
    return [
        'title' => 'custom title',
        'content' => 'custom content',
    ];
}

/**
 * Prepare the data for validation.
 */
protected function prepareForValidation() : void
{
     $this->input->setArgument(
        name: 'foo',
        value: "{$this->argument('foo')}-bar",
    );
}

$this->validated(); // (array) ['foo' => 'bar', 'bar' => 'baz']

$this->collect(); // collect(['foo' => 'bar', 'bar' => 'baz'])

$this->validated('foo'); // (string) 'bar'

$this->string('foo'); // (string) 'bar'

$this->boolean('foo'); // (bool) false

use Symfony\Component\Console\Command\Command;

$this->artisan('foo')
    ->assertFailedWithValidationError();

use Symfony\Component\Console\Command\Command;

$this->artisan('foo')
    ->assertExitCode(Command::INVALID);

$this->artisan('foo')
    ->expectsOutput('The foo field must be at least 6 characters.')