Example #1
0
 /**
  * 构造方法:初始化页面编码方式
  * @param string $encoding
  */
 public function __construct($encoding = null)
 {
     if ($encoding === null) {
         $encoding = Ap::getEncoding();
     }
     $this->setEncoding($encoding);
 }
Example #2
0
 /**
  * 单例模式:获取本类的实例化对象
  * @param string $charset
  * @return \tfc\util\Encoder
  */
 public static function getInstance($charset = null)
 {
     if ($charset === null) {
         $charset = Ap::getEncoding();
     }
     $charset = strtoupper($charset);
     if (!isset(self::$_instances[$charset])) {
         self::$_instances[$charset] = new self($charset);
     }
     return self::$_instances[$charset];
 }
Example #3
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.');
 }
Example #4
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);
         }
     }
 }
Example #5
0
 /**
  * 测试打印数据,只有DEBUG或者强制的时候才输出
  * @param mixed $expression
  * @param boolean $coercion
  * @return void
  */
 function debug_print_r($expression, $coercion = false)
 {
     if (DEBUG || $coercion) {
         $response = \tfc\ap\Ap::getResponse();
         if (!$response->headersSent()) {
             $response->contentType('text/html', \tfc\ap\Ap::getEncoding());
         }
         echo '<pre>';
         print_r($expression);
         echo '</pre>';
         exit;
     }
 }