<?php
/**
 * Created by PhpStorm.
 * User: Ming
 * Date: 2017/11/3
 * Time: 13:09
 */

namespace common\traits;

use Yii;

trait GetIdTrait {
    /**
     * 获取URL中的ID
     *
     * @param string $key
     * @return array|bool|mixed
     */
    public function getId($key = 'id'){
        $id = $this->isId($key);
        if($id === false){
            return $this->notice('缺少必要的参数['.$key.']','error');
        }
        return $id;
    }

    /**
     * 是不是ID
     *
     * @param string $key
     * @return bool
     */
    public function isId($key = 'id'){
        $id = Yii::$app->request->all($key);
        if(!$id || !strlen($id)!=32 || !preg_match("/^[a-zA-Z0-9]+$/", $id)){
            return false;
        }
        return $id;
    }

}