<?php

namespace App\Http\Support;

class ValidationRule{
    const WARNING = [
        'string' => 'must be a string',
        'email' => 'tidak valid',
        'mimes' => 'tipe file tidak sesuai',
        'unique' => 'must be unique',
        'required' => 'tidak boleh kosong',
        'unique' => 'sudah digunakan',
        'numeric' => 'harus berupa nomor',
        'max' => 'tidak boleh lebih dari',
        'min' => 'tidak boleh kurang dari',
        'before_or_equal' => 'maksimal hari ini',
        'after_or_equal' => 'minimal hari ini',
    ];

    public static function getErrorMessage($validationRules) {
        $messages = [];
        foreach ($validationRules as $key => $rules) {
            $rulesArr = explode('|', $rules);

            foreach ($rulesArr as $rule) {
                $ruleArr = explode('|', $rule);
                $ruleParams = explode(':', $ruleArr[0]);
                $ruleKey = $ruleParams[0];

                $message = str_replace('inputs.', '', $key);
                $message = str_replace(['.', '_'], ' ', $message).' '.self::WARNING[$ruleKey].($ruleKey == 'max' || $ruleKey == 'min' ? ' '.$ruleParams[1].' characters' : '');
                $messages[$key.'.'.$ruleKey] = $message;
            }
        }
        return $messages;
    }
}

?>
