示例#1
0
 /**
  * 通过输出数据类型,输出数据
  * @param mixed $data
  * @return void
  * @throws InvalidArgumentException 如果不是可支持的输出数据类型,抛出异常
  */
 public function display($data)
 {
     // 规范输出格式
     $data = $this->getViewData($data);
     // 数据转Json类型后输出,如果项目不是UTF-8格式,需要先将输出数据转成UTF-8格式
     if ($this->_dataType === 'JSON') {
         if (Ap::getEncoding() !== 'UTF-8') {
             $data = Encoder::getInstance()->convert($data, Ap::getEncoding(), 'UTF-8');
         }
         echo json_encode($data);
         exit;
     }
     // 数据序列化后输出
     if ($this->_dataType === 'SERIAL') {
         echo serialize($data);
         exit;
     }
     throw new InvalidArgumentException('DataAction is unable to determine the output data type of the request.');
 }
示例#2
0
 /**
  * 转换字符串或字符串数组的编码
  * 优先采用mb_convert_encoding函数,如果mb_convert_encoding函数不存在,采用iconv函数
  * @param mixed $param
  * @param string $inCharset
  * @param string $outCharset
  * @return mixed
  * @throws ErrorException 如果mb_convert_encoding或者iconv函数不存在,抛出异常
  */
 public function convert($param, $inCharset = 'GBK', $outCharset = null)
 {
     if (function_exists('mb_convert_encoding')) {
         return Encoder::mb_convert_encoding($param, $inCharset, $outCharset);
     }
     if (function_exists('iconv')) {
         return Encoder::iconv($param, $inCharset, $outCharset);
     }
     throw new ErrorException(sprintf('Encoder convert encoding failed, function "mb_convert_encoding" or "iconv" not exists'));
 }
示例#3
0
 /**
  * 初始化项目编码和输入内容编码,如果输入编码和项目编码不一致,自动转换RPG中数据编码
  * @return void
  * @throws InvalidArgumentException 如果不是可支持的编码,抛出异常
  */
 protected function _initEncoding()
 {
     // 验证配置中的项目编码是否合法
     try {
         $encoding = strtoupper(trim(Cfg::getApp('encoding')));
         Ap::setEncoding($encoding);
     } catch (ErrorException $e) {
     }
     if (!in_array(Ap::getEncoding(), $this->_encodings)) {
         throw new InvalidArgumentException('BaseAction is unable to determine the charset of the config.');
     }
     // 从RGP中获取‘ie’的值(input encode),并验证是否合法
     $encoding = Ap::getRequest()->getTrim('ie');
     if ($encoding !== '') {
         $encoding = strtoupper($encoding);
         if (!in_array($encoding, $this->_encodings)) {
             throw new InvalidArgumentException('BaseAction is unable to determine the charset of the request.');
         }
         // 转换输入内容编码
         if (Ap::getEncoding() !== $encoding) {
             $encoder = Encoder::getInstance();
             $_GET = $encoder->convert($_GET, $encoding);
             $_POST = $encoder->convert($_POST, $encoding);
             // $_COOKIE = $encoder->convert($_COOKIE, $encoding);
         }
     }
 }