subscription/includes/abstracts/abstract-yoone-data.php

220 lines
4.5 KiB
PHP

<?php
/**
* 抽象数据类
*
* 为所有数据对象提供基础功能
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* 抽象数据类
*/
abstract class Abstract_Yoone_Data {
/**
* 数据存储
*/
protected $data = array();
/**
* 元数据存储
*/
protected $meta_data = array();
/**
* 对象ID
*/
protected $id = 0;
/**
* 对象类型
*/
protected $object_type = '';
/**
* 数据变更标记
*/
protected $changes = array();
/**
* 构造函数
*/
public function __construct($id = 0) {
if ($id > 0) {
$this->set_id($id);
$this->read();
}
}
/**
* 获取ID
*/
public function get_id() {
return $this->id;
}
/**
* 设置ID
*/
public function set_id($id) {
$this->id = absint($id);
}
/**
* 获取对象类型
*/
public function get_object_type() {
return $this->object_type;
}
/**
* 获取数据
*/
public function get_data() {
return array_merge($this->data, array('id' => $this->get_id()));
}
/**
* 获取变更
*/
public function get_changes() {
return $this->changes;
}
/**
* 设置属性
*/
protected function set_prop($prop, $value) {
if (array_key_exists($prop, $this->data)) {
if ($this->data[$prop] !== $value) {
$this->changes[$prop] = $value;
$this->data[$prop] = $value;
}
}
}
/**
* 获取属性
*/
protected function get_prop($prop, $context = 'view') {
$value = null;
if (array_key_exists($prop, $this->changes)) {
$value = $this->changes[$prop];
} elseif (array_key_exists($prop, $this->data)) {
$value = $this->data[$prop];
}
if ('view' === $context) {
$value = apply_filters($this->get_hook_prefix() . $prop, $value, $this);
}
return $value;
}
/**
* 获取钩子前缀
*/
protected function get_hook_prefix() {
return 'yoone_' . $this->object_type . '_get_';
}
/**
* 获取元数据
*/
public function get_meta($key = '', $single = true, $context = 'view') {
if (!$this->get_id()) {
return $single ? '' : array();
}
return get_metadata($this->object_type, $this->get_id(), $key, $single);
}
/**
* 设置元数据
*/
public function set_meta($key, $value) {
if (!$this->get_id()) {
return false;
}
return update_metadata($this->object_type, $this->get_id(), $key, $value);
}
/**
* 删除元数据
*/
public function delete_meta($key) {
if (!$this->get_id()) {
return false;
}
return delete_metadata($this->object_type, $this->get_id(), $key);
}
/**
* 应用变更
*/
protected function apply_changes() {
$this->data = array_replace_recursive($this->data, $this->changes);
$this->changes = array();
}
/**
* 保存数据
*/
public function save() {
if (!$this->get_id()) {
$this->create();
} else {
$this->update();
}
$this->apply_changes();
return $this->get_id();
}
/**
* 删除数据
*/
public function delete($force_delete = false) {
if (!$this->get_id()) {
return false;
}
do_action('yoone_before_delete_' . $this->object_type, $this->get_id(), $this);
$result = $this->delete_from_database($force_delete);
if ($result) {
$this->set_id(0);
do_action('yoone_delete_' . $this->object_type, $this->get_id(), $this);
}
return $result;
}
/**
* 抽象方法 - 从数据库读取
*/
abstract protected function read();
/**
* 抽象方法 - 创建记录
*/
abstract protected function create();
/**
* 抽象方法 - 更新记录
*/
abstract protected function update();
/**
* 抽象方法 - 从数据库删除
*/
abstract protected function delete_from_database($force_delete = false);
}