格式规则:
``` * array('name' => '', 'type' => 'string', 'default' => '', 'min' => '', 'max' => '', 'regex' => '') * array('name' => '', 'type' => 'int', 'default' => '', 'min' => '', 'max' => '',) * array('name' => '', 'type' => 'float', 'default' => '', 'min' => '', 'max' => '',) * array('name' => '', 'type' => 'boolean', 'default' => '',) * array('name' => '', 'type' => 'date', 'default' => '',) * array('name' => '', 'type' => 'array', 'default' => '', 'format' => 'json/explode', 'separator' => '') * array('name' => '', 'type' => 'enum', 'default' => '', 'range' => array(...)) * array('name' => '', 'type' => 'file', 'default' => array(...), 'min' => '', 'max' => '', 'range' => array(...)) ``` * * @package PhalApi\Request * @license http://www.phalapi.net/license GPL 协议 * @link http://www.phalapi.net/ * @author dogstar 2014-10-04 */ class PhalApi_Request_Var { /** ------------------ 对外开放操作 ------------------ **/ /** * 统一格式化操作 * 扩展参数请参见各种类型格式化操作的参数说明 * * @param string $varName 变量名 * @param array $rule 格式规则: * array( * 'name' => '变量名', * 'type' => '类型', * 'default' => '默认值', * 'format' => '格式化字符串' * ... * ) * @param array $params 参数列表 * @return miexd 格式后的变量 */ public static function format($varName, $rule, $params) { $value = isset($rule['default']) ? $rule['default'] : NULL; $type = !empty($rule['type']) ? strtolower($rule['type']) : 'string'; $key = isset($rule['name']) ? $rule['name'] : $varName; $value = isset($params[$key]) ? $params[$key] : $value; if ($value === NULL && $type != 'file') { //排除文件类型 return $value; } return static::formatAllType($type, $value, $rule); } /** * 统一分发处理 * @param string $type 类型 * @param string $value 值 * @param array $rule 规则配置 * @return mixed */ protected static function formatAllType($type, $value, $rule) { $diKey = '_formatter' . ucfirst($type); $diDefautl = 'PhalApi_Request_Formatter_' . ucfirst($type); $formatter = DI()->get($diKey, $diDefautl); if (!($formatter instanceof Phalapi_Request_Formatter)) { throw new PhalApi_Exception_InternalServerError( T('invalid type: {type} for rule: {name}', array('type' => $type, 'name' => $rule['name'])) ); } return $formatter->parse($value, $rule); } }