<?php

namespace App\Forms;

use App\Forms\CommonForm;

class UserForm extends CommonForm
{
  public function __construct()
  {
    parent::__construct();
  }
  // protected function getValidationRule(string $field, string $rule): array
  // {
  //   switch ($field) {
  //     case 'role':
  //       $field = "{$field}.*";
  //       break;
  //     default:
  //       return parent::getValidationRule($field, $rule);
  //       // break;
  //   }
  //   return array($field, $rule);
  // }
  public function getFormRule(string $action, string $field): string
  {
    switch ($field) {
      case "id":
        $rule = sprintf("required|trim|min_length[4]|max_length[20]%s", in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "passwd":
        $rule =  sprintf("%s|%s", in_array($action, ["create", "create_form"]) ? "required" : "permit_empty", "trim|string");
        break;
      case "confirmpassword":
        $rule = sprintf("%s|%s", in_array($action, ["create", "create_form"]) ? "required" : "permit_empty", "trim|string|matches[passwd]");
        break;
      case "email":
        $rule = sprintf("required|trim|valid_email%s", in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "role":
        $rule  = "required|trim|string";
        break;
      default:
        $rule = parent::getFormRule($action, $field);
        break;
    }
    return $rule;
  }
}
