PHP code example of kobylinski / beetroot

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

    

kobylinski / beetroot example snippets



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Kobylinski\Beetroot\WithValidate;

class AddUserCommand extends Command
{
  use WithValidate;

  protected $signature = "add-user {handle}";

  protected $description = "Add a new user with a unique handle";

  /**
   * Define validation rules for the command input.
   *
   * @return array
   */
  protected function rules(): array
  {
    return [
      "handle" => [
        "string", // Ensure it's a string
        "max:255", // Limit length to 255 characters
        "unique:users,handle", // Ensure it's unique in the 'users' table
      ],
    ];
  }

  /**
   * Execute the console command.
   *
   * @return int
   */
  public function handle(): int
  {
    $handle = $this->argument("handle");
    $this->info("User '{$handle}' added successfully!");

    return Command::SUCCESS;
  }
}


/**
 * Define custom validation messages.
 *
 * @return array
 */
protected function messages(): array
{
  return [
    "handle.unique" => "A user with this handle already exists.",
  ];
}


namespace App\Console\Commands;

use Illuminate\Console\Command;
use Kobylinski\Beetroot\WithSubcommands;

class AddUserCommand extends Command
{
  use WithSubcommands;

  protected $signature = 'user 
    {UserCommand
      (add 
        {handle : The handle of the user}
        {name : The name of the token}
        {abilities?* : The abilities of the token})
      (*find
        {handle? : Part of the handle of the user})
      (suspend|restore
        {handle : The handle of the user})
      (remove
        {handle : The handle of the user}
        {name : The name of the token})
      (token
        {handle : The handle of the user}
        {TokenCommand
          (add 
            {name : The name of the token}
            {abilities?* : The abilities of the token})
          (remove
            {name : The name of the token})
        })
    } 
  ';


public function handle(): int
{
  return match ($this->argument("UserCommand")) {
    "add" => $this->addUser(),
    "find" => $this->findUser(),
    "suspend", "restore" => $this->toggleUserStatus(),
    "remove" => $this->removeUser(),
    "token" => $this->handleTokenSubcommand(),
    default => Command::INVALID,
  };
}

[
  "field" => "



use Kobylinski\Beetroot\Attributes\Value;
use Kobylinski\Beetroot\Attributes\Flag;
use Kobylinski\Beetroot\Attributes\Sequence;
use Kobylinski\Beetroot\Attributes\Rule;

#[Rule("my_rule")]
class MyCustomRule
{
    use WithNamedParameters;

    /**
     * Run the validation rule.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @param  Closure(string, ?string=): void  $fail
     */
    #[Value("category"), Value("mode", dictionary: ["strict", "lenient"]), Flag("active"), Sequence("ids_to_exclude")]
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        // Example: Ensure the value matches the configured category
        if ($this->category !== $value) {
            $fail("The $attribute must belong to the {$this->category} category.");
        }

        // Additional checks based on mode
        if ($this->mode === 'strict' && strlen($value) < 5) {
            $fail("The $attribute must be at least 5 characters long in strict mode.");
        }

        // Example usage of flags and sequences
        if ($this->active && in_array($value, $this->ids_to_exclude)) {
            $fail("The $attribute cannot be one of the excluded values.");
        }
    }
}

use Illuminate\Support\Facades\Validator;

$data = [
    'field' => 'example_value',
];

$rules = [
    'field' => '

    #[Value("category"), Value("mode", dictionary: ["strict", "lenient"]), Flag("active"), Sequence("ids_to_exclude")]
    

use App\Rules\MyCustomRule;

public function boot()
{
    MyCustomRule::register();
}
bash
# add user
php artisan user add johndoe read write

# find user
php artisan user find john

# suspend user
php artisan user suspend johndoe

# add new token
php artisan user token johndoe add monitoring read
bash
    php artisan make:rule MyCustomRule