Beispiel #1
0
 /**
  * 获取当前类型的数据
  *
  * @param OOP_ORM_Data $obj
  * @param $data
  * @param $compiled_data
  * @return mixed
  */
 public function &get_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data)
 {
     $field_name = $this->field_name;
     if (null === $field_name && array_key_exists($this->key, $data)) {
         $field_name = $this->key;
     }
     if ($field_name && isset($data[$field_name])) {
         $tmp_data = $data[$field_name];
         # 处理格式化数据
         if (isset($this->config['format'])) {
             OOP_ORM_DI::_do_de_format_data($this->config['format'], $tmp_data);
         } elseif (isset($this->config['is_temp_instance']) && $this->config['is_temp_instance']) {
             # 虚拟对象,尝试解析数据
             if (is_string($tmp_data)) {
                 if (in_array(substr($tmp_data, 0, 1), array('{', '['))) {
                     # 尝试用 json 解析
                     $tmp_data2 = @json_decode($tmp_data, true);
                     if (null !== $tmp_data2) {
                         $tmp_data = $tmp_data2;
                         $this->config['format'] = 'json';
                         // json处理
                     }
                 } elseif (in_array(substr($tmp_data, 0, 2), array('a:', 'o:', 's:', 'i:', 'f:'))) {
                     # 尝试用反序列化解析
                     try {
                         $tmp_data2 = unserialize($tmp_data);
                         $tmp_data = $tmp_data2;
                         $this->config['format'] = 'serialize';
                         // 序列化处理
                     } catch (Exception $e) {
                         # 解析失败则忽略操作
                     }
                 }
             }
         }
         # 处理数据类型
         if (isset($this->config['field_type'])) {
             OOP_ORM_DI::_check_field_type($this->config['field_type'], $tmp_data);
         }
         $compiled_data[$this->key] = $tmp_data;
         $compiled_raw_data[$this->key] = $tmp_data;
         // 保存一个数据备份,用于检查是否修改
     } else {
         $compiled_data[$this->key] = null;
     }
     return $compiled_data[$this->key];
 }
Beispiel #2
0
 /**
  * 获取当前类型的数据
  *
  * @param OOP_ORM_Data $obj
  * @param $data
  * @param $compiled_data
  * @return mixed
  */
 public function &get_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data)
 {
     # 处理URL变量
     $this->url = $this->config['resource'];
     if (preg_match_all('#{{([a-z0-9_]+)}}#i', $this->url, $m)) {
         foreach ($m[1] as $v) {
             $this->url = str_replace('{{' . $v . '}}', $obj->{$v}, $this->url);
         }
     }
     # 获取缓存
     if (isset($this->config['cache'])) {
         /**
          * @var $cache Cache
          */
         list($cache, $key) = $this->get_cache_instance_and_key($obj);
         $tmp_data = $cache->get($key);
     } else {
         $tmp_data = null;
     }
     # 获取内容
     if (null === $tmp_data || false === $tmp_data) {
         $tmp_data = HttpClient::factory()->get($this->url)->data();
         if (false === $tmp_data) {
             return false;
         }
         # 处理数据类型
         if (isset($this->config['field_type'])) {
             OOP_ORM_DI::_check_field_type($this->config['field_type'], $tmp_data);
         }
         # 设置缓存
         if (isset($this->config['cache'])) {
             $cache->set($key, $tmp_data, isset($this->config['cache']['expired']) ? $this->config['cache']['expired'] : 3600, isset($this->config['cache']['expire_type']) ? $this->config['cache']['expire_type'] : null);
         }
     }
     # 处理格式化数据
     if (isset($this->config['format'])) {
         OOP_ORM_DI::_do_de_format_data($this->config['format'], $tmp_data);
     }
     $compiled_data[$this->key] = $tmp_data;
     $compiled_raw_data[$this->key] = $tmp_data;
     return $compiled_data[$this->key];
 }
Beispiel #3
0
 protected function get_metadata_by_group_data($config, $metadata)
 {
     if ($config['depth'] > 0) {
         $tmp_data = array();
         foreach ($metadata as $item) {
             if ($item['field_name'] !== $config['field_name']) {
                 continue;
             }
             if (strpos($item['meta_index'], '.') !== false) {
                 // 构造一个多维数组
                 $tmp =& $tmp_data;
                 foreach (explode('.', $item['meta_index']) as $key) {
                     $tmp =& self::_create_metadata_tree($key, $tmp);
                 }
                 $tmp = $item['meta_value'];
                 if (isset($config['format']) && $config['format']) {
                     OOP_ORM_DI::_do_de_format_data($config['format'], $tmp);
                 }
                 unset($tmp);
             } else {
                 if (isset($config['format']) && $config['format']) {
                     OOP_ORM_DI::_do_de_format_data($config['format'], $item['meta_value']);
                 }
                 $tmp_data[$item['meta_index']] = $item['meta_value'];
             }
         }
         return $tmp_data;
     } else {
         foreach ($metadata as $item) {
             if ($item['field_name'] === $config['field_name']) {
                 if (isset($config['format']) && $config['format']) {
                     OOP_ORM_DI::_do_de_format_data($config['format'], $item['meta_value']);
                 }
                 return $item['meta_value'];
             }
         }
         return null;
     }
 }