<?php
namespace backend\helpers;

use yii\base\DynamicModel;

class ValidatorHelper
{
    const REGEX_EMAIL = "/^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$/";
    /**
     * @param array $data
     * @param array $rules
     * @param null $errors
     * @return bool|array
     * @see DynamicModel
     * @see Validator
     */
    public static function validateData(array $data, $rules = [], &$errors = null)
    {
        $model = DynamicModel::validateData($data, $rules);
        if ($model->hasErrors()) {
            $errors = $model->getErrors();
            return false;
        } else {
            return $model->getAttributes();
        }
    }

    /**
     * 校验邮箱
     * @param $email
     * @return bool
     */
    public static function isEmail($email)
    {
        $bool = false;
        if (preg_match(self::REGEX_EMAIL, $email)) {
            $bool = true;
        }
        return $bool;
    }
}
