/** * 设置数据 * * @param OOP_ORM_Data $obj * @param $data * @param $compiled_data * @param $new_value * @param bool $has_compiled * @return bool */ public function set_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data, $new_value, $has_compiled) { if ($has_compiled) { if ($this->is_readonly()) { # 只读字段 return false; } } elseif ($this->field_name) { # 未构造完 // $data[$this->field_name] = $data; } if (!array_key_exists($this->key, $compiled_data) && $obj->pk()) { # 没有构造过数据直接set,则先构造下 $this->get_data($obj, $data, $compiled_data, $compiled_raw_data); } # 处理数据类型 if (isset($this->config['field_type'])) { OOP_ORM_DI::_check_field_type($this->config['field_type'], $new_value); } $compiled_data[$this->key] = $new_value; if (!$has_compiled) { $compiled_raw_data[$this->key] = $compiled_data[$this->key]; } return true; }
/** * 获取当前类型的数据 * * @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]; }
/** * 设置数据 * * @param OOP_ORM_Data $obj * @param $data * @param $compiled_data * @param $new_value * @param bool $has_compiled * @return bool */ public function set_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data, $new_value, $has_compiled) { if ($has_compiled) { if ($this->is_readonly()) { # 只读字段 return false; } } # 处理数据类型 if (isset($this->config['field_type'])) { OOP_ORM_DI::_check_field_type($this->config['field_type'], $new_value); } $compiled_data[$this->key] = $new_value; if (!$has_compiled) { $compiled_raw_data[$this->key] = $compiled_data[$this->key]; if ($this->field_name) { # 未构造完,给data设置值 $data[$this->field_name] = $this->get_data($obj, $data, $compiled_data, $compiled_raw_data); } } return true; }