/** * 基于配置清理表单提交的数据 * <pre> * 一.清理规则: * $rules = array( * 'user_loginname' => 'trim', * 'user_interest' => array($foo, 'explode') * ); * 参数: * $attributes = array( * 'user_loginname' => ' abcdefghi ', * 'user_interest' => ' 1, 2' * ); * 结果: * $result = array( * 'user_loginname' => 'abcdefghi', * 'user_interest' => array(1, 2) * ); * * 二.清理规则: * $rules = array( * 'user_password' => 'md5', * 'user_interest' => array($foo, 'implode') * ); * 参数: * $attributes = array( * 'user_password' => ' 1234 ', * 'user_interest' => array(1, 2) * ); * 结果: * $result = array( * 'user_loginname' => '81dc9bdb52d04dc20036dbd8313ed055', * 'user_interest' => '1,2' * ); * </pre> * @param array $rules * @param array $attributes * @return array */ public function clean(array $rules, array $attributes) { return Clean::rules($rules, $attributes); }
/** * 过滤数组(只保留指定的字段)、清理数据并且清除空数据(空字符,负数) * @param array $attributes * @param array $rules * @return void */ protected function filterCleanEmpty(array &$attributes = array(), array $rules = array()) { $this->filterAttributes($attributes, array_keys($rules)); $attributes = Clean::rules($rules, $attributes); foreach ($rules as $columnName => $funcName) { if (!isset($attributes[$columnName])) { continue; } if ($funcName === 'trim' && $attributes[$columnName] === '') { unset($attributes[$columnName]); continue; } if ($funcName === 'intval' && $attributes[$columnName] < 0) { unset($attributes[$columnName]); continue; } } }