| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Core\Abstracts; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
abstract class YatraData |
| 10 |
{ |
| 11 |
|
| 12 |
protected $id = 0; |
| 13 |
|
| 14 |
protected $data = array(); |
| 15 |
|
| 16 |
protected $changes = array(); |
| 17 |
|
| 18 |
protected $object_read = false; |
| 19 |
|
| 20 |
protected $object_type = 'data'; |
| 21 |
|
| 22 |
protected $extra_data = array(); |
| 23 |
|
| 24 |
protected $default_data = array(); |
| 25 |
|
| 26 |
protected $cache_group = ''; |
| 27 |
|
| 28 |
protected $meta_data = null; |
| 29 |
|
| 30 |
public function __construct() |
| 31 |
{ |
| 32 |
$this->data = array_merge($this->data, $this->extra_data); |
| 33 |
$this->default_data = $this->data; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
public function __sleep() |
| 38 |
{ |
| 39 |
return array('id'); |
| 40 |
} |
| 41 |
|
| 42 |
public function __wakeup() |
| 43 |
{ |
| 44 |
try { |
| 45 |
$this->__construct(absint($this->id)); |
| 46 |
} catch (\Exception $e) { |
| 47 |
$this->set_id(0); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public function __clone() |
| 52 |
{ |
| 53 |
if (!empty($this->meta_data)) { |
| 54 |
foreach ($this->meta_data as $array_key => $meta) { |
| 55 |
$this->meta_data[$array_key] = clone $meta; |
| 56 |
if (!empty($meta->id)) { |
| 57 |
$this->meta_data[$array_key]->id = null; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
public function get_id() |
| 65 |
{ |
| 66 |
return $this->id; |
| 67 |
} |
| 68 |
|
| 69 |
public function __toString() |
| 70 |
{ |
| 71 |
return wp_json_encode($this->get_data()); |
| 72 |
} |
| 73 |
|
| 74 |
public function get_data() |
| 75 |
{ |
| 76 |
return array_merge(array('id' => $this->get_id()), $this->data, array('meta_data' => [])); |
| 77 |
} |
| 78 |
|
| 79 |
public function get_data_keys() |
| 80 |
{ |
| 81 |
return array_keys($this->data); |
| 82 |
} |
| 83 |
|
| 84 |
public function get_extra_data_keys() |
| 85 |
{ |
| 86 |
return array_keys($this->extra_data); |
| 87 |
} |
| 88 |
|
| 89 |
public function set_id($id) |
| 90 |
{ |
| 91 |
$this->id = absint($id); |
| 92 |
} |
| 93 |
|
| 94 |
public function set_props($props, $context = 'set') |
| 95 |
{ |
| 96 |
$errors = false; |
| 97 |
|
| 98 |
foreach ($props as $prop => $value) { |
| 99 |
try { |
| 100 |
/** |
| 101 |
* Checks if the prop being set is allowed, and the value is not null. |
| 102 |
*/ |
| 103 |
if (is_null($value) || in_array($prop, array('prop', 'date_prop', 'meta_data'), true)) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
$setter = "set_$prop"; |
| 107 |
|
| 108 |
if (is_callable(array($this, $setter))) { |
| 109 |
$this->{$setter}($value); |
| 110 |
} |
| 111 |
} catch (\Exception $e) { |
| 112 |
if (!$errors) { |
| 113 |
$errors = new \WP_Error(); |
| 114 |
} |
| 115 |
$errors->add($e->getCode(), $e->getMessage()); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return $errors && count($errors->get_error_codes()) ? $errors : true; |
| 120 |
} |
| 121 |
|
| 122 |
protected function set_prop($prop, $value) |
| 123 |
{ |
| 124 |
if (array_key_exists($prop, $this->data)) { |
| 125 |
if (true === $this->object_read) { |
| 126 |
if ($value !== $this->data[$prop] || array_key_exists($prop, $this->changes)) { |
| 127 |
$this->changes[$prop] = $value; |
| 128 |
} |
| 129 |
} else { |
| 130 |
$this->data[$prop] = $value; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public function get_changes() |
| 136 |
{ |
| 137 |
return $this->changes; |
| 138 |
} |
| 139 |
|
| 140 |
protected function get_hook_prefix() |
| 141 |
{ |
| 142 |
return 'yatra_' . $this->object_type . '_get_'; |
| 143 |
} |
| 144 |
|
| 145 |
protected function get_prop($prop, $context = 'view') |
| 146 |
{ |
| 147 |
$value = null; |
| 148 |
|
| 149 |
if (array_key_exists($prop, $this->data)) { |
| 150 |
$value = array_key_exists($prop, $this->changes) ? $this->changes[$prop] : $this->data[$prop]; |
| 151 |
|
| 152 |
if ('view' === $context) { |
| 153 |
$value = apply_filters($this->get_hook_prefix() . $prop, $value, $this); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $value; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
} |
| 162 |
|