CompiledData.php
3 years ago
Cookie.php
4 years ago
Option.php
9 years ago
Permissions.php
1 year ago
Preferences.php
4 years ago
RecentItems.php
2 years ago
Serializable.php
3 years ago
Session.php
4 years ago
Settings.php
1 year ago
Transient.php
7 years ago
Upload.php
2 years ago
Serializable.php
237 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Generic array-like object that may be serialized as an array and committed into WordPress data stores. |
| 4 | */ |
| 5 | abstract class Loco_data_Serializable extends ArrayObject { |
| 6 | |
| 7 | /** |
| 8 | * Object/schema version (not plugin version) can be used for validation and migrations |
| 9 | * @var string|int|float |
| 10 | */ |
| 11 | private $v = 0; |
| 12 | |
| 13 | /** |
| 14 | * Time object was last persisted |
| 15 | * @var int |
| 16 | */ |
| 17 | private $t = 0; |
| 18 | |
| 19 | /** |
| 20 | * @var bool |
| 21 | */ |
| 22 | private $dirty; |
| 23 | |
| 24 | /** |
| 25 | * Whether persisting on object destruction |
| 26 | * @var bool |
| 27 | */ |
| 28 | private $lazy = false; |
| 29 | |
| 30 | /** |
| 31 | * Commit serialized data to WordPress storage |
| 32 | * @return mixed |
| 33 | */ |
| 34 | abstract public function persist(); |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
| 40 | public function __construct( array $data = [] ){ |
| 41 | $this->setFlags( ArrayObject::ARRAY_AS_PROPS ); |
| 42 | parent::__construct( $data ); |
| 43 | $this->dirty = (bool) $data; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * @internal |
| 49 | */ |
| 50 | final public function __destruct(){ |
| 51 | if( $this->lazy ){ |
| 52 | $this->persistIfDirty(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Check if object's properties have change since last clean |
| 59 | * @return bool |
| 60 | */ |
| 61 | public function isDirty(){ |
| 62 | return $this->dirty; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Make not dirty |
| 68 | * @return self |
| 69 | */ |
| 70 | protected function clean(){ |
| 71 | $this->dirty = false; |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Force dirtiness for next check |
| 78 | * @return static |
| 79 | */ |
| 80 | protected function touch(){ |
| 81 | $this->dirty = true; |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Enable lazy persistence on object destruction, if dirty |
| 88 | * @return static |
| 89 | */ |
| 90 | public function persistLazily(){ |
| 91 | $this->lazy = true; |
| 92 | return $this; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Call persist method only if has changed since last clean |
| 98 | * @return static |
| 99 | */ |
| 100 | public function persistIfDirty(){ |
| 101 | if( $this->isDirty() ){ |
| 102 | $this->persist(); |
| 103 | } |
| 104 | return $this; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * {@inheritdoc} |
| 110 | * override so we can set dirty flag |
| 111 | */ |
| 112 | #[ReturnTypeWillChange] |
| 113 | public function offsetSet( $key, $value ){ |
| 114 | if( ! isset($this[$key]) || $value !== $this[$key] ){ |
| 115 | parent::offsetSet( $key, $value ); |
| 116 | $this->dirty = true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * {@inheritdoc} |
| 123 | * override so we can set dirty flag |
| 124 | */ |
| 125 | #[ReturnTypeWillChange] |
| 126 | public function offsetUnset( $key ){ |
| 127 | if( isset($this[$key]) ){ |
| 128 | parent::offsetUnset($key); |
| 129 | $this->dirty = true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * @param string|int|float $version |
| 136 | * @return self |
| 137 | */ |
| 138 | public function setVersion( $version ){ |
| 139 | if( $version !== $this->v ){ |
| 140 | $this->v = $version; |
| 141 | $this->dirty = true; |
| 142 | } |
| 143 | return $this; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * @return string|int|float |
| 149 | */ |
| 150 | public function getVersion(){ |
| 151 | return $this->v; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * @return int |
| 157 | */ |
| 158 | public function getTimestamp(){ |
| 159 | return $this->t; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Get serializable data for storage |
| 165 | * @return array |
| 166 | */ |
| 167 | protected function getSerializable(){ |
| 168 | return [ |
| 169 | 'c' => get_class($this), |
| 170 | 'v' => $this->getVersion(), |
| 171 | 'd' => $this->getArrayCopy(), |
| 172 | 't' => time(), |
| 173 | ]; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Restore object state from array as returned from getSerializable |
| 179 | * @param array $data |
| 180 | * @return self |
| 181 | */ |
| 182 | protected function setUnserialized( $data ){ |
| 183 | |
| 184 | if( ! is_array($data) || ! isset($data['d']) ) { |
| 185 | throw new InvalidArgumentException('Unexpected data'); |
| 186 | } |
| 187 | |
| 188 | if( get_class($this) !== $data['c'] ){ |
| 189 | throw new InvalidArgumentException('Unexpected class name'); |
| 190 | } |
| 191 | |
| 192 | // ok to populate ArrayObject |
| 193 | $this->exchangeArray( $data['d'] ); |
| 194 | |
| 195 | // setting version as it was in database |
| 196 | $this->setVersion( $data['v'] ); |
| 197 | |
| 198 | // timestamp may not be present in old objects |
| 199 | $this->t = isset($data['t']) ? $data['t'] : 0; |
| 200 | |
| 201 | // object is being restored, probably from disk so start with clean state |
| 202 | $this->dirty = false; |
| 203 | |
| 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * @param string $prop |
| 210 | * @param mixed $value |
| 211 | * @param array $defaults |
| 212 | * @return mixed |
| 213 | */ |
| 214 | protected static function cast( $prop, $value, array $defaults ){ |
| 215 | if( ! array_key_exists($prop,$defaults) ){ |
| 216 | throw new InvalidArgumentException('Invalid option, '.$prop ); |
| 217 | } |
| 218 | $default = $defaults[$prop]; |
| 219 | // cast to same type as default |
| 220 | if( is_bool($default) ){ |
| 221 | $value = (bool) $value; |
| 222 | } |
| 223 | else if( is_int($default) ){ |
| 224 | $value = (int) $value; |
| 225 | } |
| 226 | else if( is_array($default) ){ |
| 227 | if( ! is_array($value) ){ |
| 228 | $value = preg_split( '/[\\s,]+/', trim($value), -1, PREG_SPLIT_NO_EMPTY ); |
| 229 | } |
| 230 | } |
| 231 | else { |
| 232 | $value = (string) $value; |
| 233 | } |
| 234 | return $value; |
| 235 | } |
| 236 | |
| 237 | } |