/var/www/html_it/wp-content/plugins/loco-translate/src/data/Serializable.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
 * Generic array-like object that may be serialized as an array and committed into WordPress data stores.
 */
abstract class Loco_data_Serializable extends ArrayObject {

    
/**
     * Object/schema version (not plugin version) can be used for validation and migrations
     * @var string|int|float
     */
    
private $v 0;

    
/**
     * Time object was last persisted
     * @var int
     */
    
private $t 0;

    
/**
     * @var bool
     */
    
private $dirty;

    
/**
     * Whether persisting on object destruction
     * @var bool
     */
    
private $lazy false;

    
/**
     * Commit serialized data to WordPress storage
     * @return mixed
     */
    
abstract public function persist();


    
/**
     * {@inheritdoc}
     */
    
public function __construct( array $data = [] ){
        
$this->setFlagsArrayObject::ARRAY_AS_PROPS );
        
parent::__construct$data );
        
$this->dirty = (bool) $data;
    }


    
/**
     * @internal 
     */
    
final public function __destruct(){
        if( 
$this->lazy ){
            
$this->persistIfDirty();
        }
    }


    
/**
     * Check if object's properties have change since last clean
     * @return bool
     */
    
public function isDirty(){
        return 
$this->dirty;
    }


    
/**
     * Make not dirty
     * @return self
     */
    
protected function clean(){
        
$this->dirty false;
        return 
$this;
    }


    
/**
     * Force dirtiness for next check
     * @return static
     */
    
protected function touch(){
        
$this->dirty true;
        return 
$this;
    }


    
/**
     * Enable lazy persistence on object destruction, if dirty
     * @return static
     */
    
public function persistLazily(){
        
$this->lazy true;
        return 
$this;
    }


    
/**
     * Call persist method only if has changed since last clean
     * @return static
     */
    
public function persistIfDirty(){
        if( 
$this->isDirty() ){
            
$this->persist();
        }
        return 
$this;
    }


    
/**
     * {@inheritdoc}
     * override so we can set dirty flag
     */
    
#[ReturnTypeWillChange]
    public function 
offsetSet$key$value ){
        if( ! isset(
$this[$key]) || $value !== $this[$key] ){
            
parent::offsetSet$key$value );
            
$this->dirty true;
        }
    }


    
/**
     * {@inheritdoc}
     * override so we can set dirty flag
     */
    
#[ReturnTypeWillChange]
    public function 
offsetUnset$key ){
        if( isset(
$this[$key]) ){
            
parent::offsetUnset($key);
            
$this->dirty true;
        }
    }


    
/**
     * @param string|int|float $version
     * @return self
     */
    
public function setVersion$version ){
        if( 
$version !== $this->){
            
$this->$version;
            
$this->dirty true;
        }
        return 
$this;
    }


    
/**
     * @return string|int|float
     */
    
public function getVersion(){
        return 
$this->v;
    }


    
/**
     * @return int
     */
    
public function getTimestamp(){
        return 
$this->t;
    }


    
/**
     * Get serializable data for storage
     * @return array
     */
    
protected function getSerializable(){
        return  [
            
'c' => get_class($this),
            
'v' => $this->getVersion(),
            
'd' => $this->getArrayCopy(),
            
't' => time(),
        ];
    }


    
/**
     * Restore object state from array as returned from getSerializable
     * @param array $data
     * @return self
     */    
    
protected function setUnserialized$data ){

        if( ! 
is_array($data) || ! isset($data['d']) ) {
            throw new 
InvalidArgumentException('Unexpected data');
        }
        
        if( 
get_class($this) !== $data['c'] ){
            throw new 
InvalidArgumentException('Unexpected class name');
        }

        
// ok to populate ArrayObject
        
$this->exchangeArray$data['d'] );

        
// setting version as it was in database
        
$this->setVersion$data['v'] );

        
// timestamp may not be present in old objects
        
$this->= isset($data['t']) ? $data['t'] : 0;

        
// object is being restored, probably from disk so start with clean state
        
$this->dirty false;
        
        return 
$this;
    }


    
/**
     * @param string $prop
     * @param mixed $value
     * @param array $defaults
     * @return mixed
     */
    
protected static function cast$prop$value, array $defaults ){
        if( ! 
array_key_exists($prop,$defaults) ){
            throw new 
InvalidArgumentException('Invalid option, '.$prop );
        }
        
$default $defaults[$prop];
        
// cast to same type as default
        
if( is_bool($default) ){
            
$value = (bool) $value;
        }
        else if( 
is_int($default) ){
            
$value = (int) $value;
        }
        else if( 
is_array($default) ){
            if( ! 
is_array($value) ){
                
$value preg_split'/[\\s,]+/'trim($value), -1PREG_SPLIT_NO_EMPTY );
            }
        }
        else {
            
$value = (string) $value;
        }
        return 
$value;
    }

}