PHP code example of henzeb / ruler-laravel

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

    

henzeb / ruler-laravel example snippets


 [
   'attribute'=>'

use Henzeb\Ruler\Concerns\Ruler;
use Illuminate\Support\ServiceProvider;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

class YourProvider extends ServiceProvider
{
    use Ruler;
    
    private array $rules = [
        'your_rule' => YourRule::class,
        YourOtherRule::class
    ]; 
}

use Henzeb\Ruler\Concerns\Ruler;
use Illuminate\Support\ServiceProvider;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

class YourProvider extends ServiceProvider
{
    use Ruler;
    
    private array $rules = [
        'your_rule' => YourRule::class,
        YourOtherRule::class
    ]; 
    
    public function boot() {
        $this->bootRuler();
        // your code
    }
}

use Henzeb\Ruler\Concerns\Ruler;
use Illuminate\Support\ServiceProvider;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

class YourProvider extends ServiceProvider
{
    use Ruler;
    
    public function boot() {
        if(/** some condition */) {
            $this->rule(YourRule::class, 'your_rule');
        }
        
        if(/** some condition */) {
            $this->rule(YourOtherRule::class);
        }
        // your code
    }
}

use Illuminate\Contracts\Validation\Rule;

class YourRule implements Rule {

    public function passes($attribute, $value)
    {
        // your validation code
    }
    
    public function message() 
    {
        return 'Message when fails';
    }
}

public function __construct(private string $param1, private string $param2 = null){}

use Illuminate\Contracts\Validation\ImplicitRule;

class YourImplicitRule implements ImplicitRule {
    // the code
}

use Illuminate\Contracts\Validation\DataAwareRule;

class DependentRule implements DataAwareRule {
    private array $data;
    
    public function setData(array $data) {
        $this->data = $data;
        return $this;
    }
    
    // your code 
}

use Henzeb\Ruler\Contracts\ReplacerAwareRule;
use Illuminate\Contracts\Validation\Rule;

class YourRule implements Rule
{
    return string $message = 'Something went wrong';
    
    public __construct($param_1, $paramTwo) {}
    
    public function passes($attribute, $value)
    {
        $this->message = 'Your error message';
        return false;
    }
    
    public function message()
    {
        return return $this->message;
    }
}

use Henzeb\Ruler\Contracts\ReplacerAwareRule;

class YourRule implements ReplacerAwareRule
{
    public __construct($param_1, $paramTwo) {}
    
    public function message()
    {
        return ':attribute :param_1 :paramTwo';
    }

    public function replacers(): array
    {
        return [
            'param_1',
            'paramTwo'
        ];
    }
}

class YourRule implements ReplacerAwareRule
{
    public function message()
    {
        return ':attribute :param_1 :paramTwo';
    }

    public function replacers(): array
    {
        return [
            'param_1'=> function(string $value, string $attribute, array $parameters, array $data) {
                return 'any string'.$parameters['paramTwo']
            },
            'paramTwo'
        ];
    }
}