<?php

namespace App\Http\Support;

class ValidationRule{
    const WARNING = [
        'required' => 'inputan tidak boleh kosong',
        'numeric' => 'inputan harus berupa angka',
        'string' => 'inputan harus berupa teks',
        'max' => 'must be maximum',
        'min' => 'must be minimum',
        'email' => 'is invalid',
        'mimes' => 'file type does not match',
        'unique' => 'must be unique'
    ];

    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;
    }
}

?>
