<?php
/**
 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/9/2
 * Time: 下午3:58
 */

namespace common\helpers;

class Form {

    /**
     * 格式化错误
     * @param $formErrors
     * @return array
     */
    public static function formatErrors($formErrors){
        $errorResult = [];
        if(is_array($formErrors)){
            foreach ($formErrors as $error) {
                if(is_array($error)){
                    foreach($error as $oneError){
                        $errorResult[] = $oneError;
                    }
                } else {
                    $errorResult[] = $error;
                }
            }
        } else {
            $errorResult[] = $formErrors;
        }
        return $errorResult;
    }

    /**
     * 格式化错误
     * @param $formErrors
     * @return string
     */
    public static function formatErrorsForApi($formErrors){
        $errors = self::formatErrors($formErrors);
        $errorResult = '';
        foreach($errors as $key=>$oneError){
            $errorResult .= ($key+1).'.'.$oneError.' ';
        }
        return $errorResult;
    }
}