-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathARValueMapper.php
More file actions
289 lines (252 loc) · 5.42 KB
/
ARValueMapper.php
File metadata and controls
289 lines (252 loc) · 5.42 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
include_once(dirname(__file__) . '/schema/ARField.php');
include_once(dirname(__file__) . '/ARSerializableDateTime.php');
/**
* Binds a value to a schema field
* (it is just an ordinary row field value container)
*
* @package activerecord
* @author Integry Systems
*/
class ARValueMapper implements Serializable
{
/**
* Schema field instance
*
* @var ARSchemaField
*/
private $field = null;
/**
* Value assigned to a schema field
*
* @var mixed
*/
private $value = null;
/**
* Initial ID value (reset after saving)
*
* Necessary when a record primary key values change and record needs to be updated (referencing the old ID values)
*
* @var mixed
*/
private $initialID = null;
/**
* Initial value (reset after saving)
*
* Necessary to know when value changes change (other) objects state
*
* For example, changing products stock count from 0 to 5 would increase the count of the available
* products by 1, but changing the stock count from 5 to 10 wouldn't affect affect this count, so it's
* sometimes not enough just to be able to check that the value has been changed.
*
* @var mixed
*/
private $initialValue = null;
/**
* A mark to indicate if a record was modified
*
* @var bool
*/
private $isModified = false;
private $isNull = false;
public function __construct(ARField $field, $value = null)
{
$this->field = $field;
$this->value = $value;
if (($this->field->getDataType() instanceof ARDateTime) && !($value instanceof ARSerializableDateTime))
{
$this->value = new ARSerializableDateTime($this->value);
}
$this->initialValue = $value;
}
/**
* Gets a related schema field
*
* @return ARField
*/
public function getField()
{
return $this->field;
}
/**
* Sets a related schema field.
*
* This method should never be called directly
*
* @return ARField
*/
public function setField(ARField $field)
{
$this->field = $field;
}
/**
* Assigns a value to a field
*
* @param mixed $value
*/
public function set($value = null, $markAsModified = true)
{
if (is_null($value))
{
$this->setNull();
return false;
}
// === also checks variable type, so we use == to compare values
if (!is_null($this->initialValue) && (!is_object($this->value) && !is_object($value) && ($value == $this->value)) )
{
return false;
}
if ($this->field instanceof ARForeignKey)
{
if (!($value instanceof ActiveRecord))
{
throw new ARException("Invalid value parameter: must be an instance of ActiveRecord");
}
else if (!is_a($value, $this->field->getForeignClassName()))
{
throw new ARException("Invalid value parameter: must be an instance of " . $this->field->getForeignClassName());
}
else if ($this->value && ($this->value === $value))
{
return false;
}
if ($this->value && !$this->initialID)
{
$this->initialID = $value->getID();
}
}
else if ($this->field)
{
$value = $this->field->getDataType()->getValidatedValue($value);
}
$this->isNull = false;
$this->value = $value;
if ($markAsModified)
{
$this->isModified = true;
}
}
public function setNull($markAsModified = true)
{
$this->value = null;
$this->isNull = true;
if ($markAsModified)
{
$this->isModified = true;
}
}
public function isNull()
{
return $this->isNull;
}
/**
* Gets a field value
*
* @return mixed
*/
public function get()
{
return $this->value;
}
/**
* Gets initial field ID value
*
* @return mixed
*/
public function getInitialID()
{
if (!$this->initialID)
{
return $this->value->getID();
}
else
{
return $this->initialID;
}
}
/**
* Gets initial field value
*
* @return mixed
*/
public function getInitialValue()
{
return $this->initialValue;
}
/**
* Returns true if field is being modified
*
* @return bool
*/
public function isModified()
{
return $this->isModified;
}
/**
* Marks field as modified
*/
public function setAsModified()
{
$this->isModified = true;
}
/**
* Marks the field as not modified (after saving, etc.)
*/
public function resetModifiedStatus($isModified = false)
{
$this->isModified = $isModified;
if (!$isModified && $this->field instanceof ARForeignKey && !is_null($this->value))
{
$this->initialID = $this->value->getID();
}
if (!$isModified)
{
$this->initialValue = $this->value;
}
}
/**
* Checks if this instance has an assigned value
*
* @return bool
*/
public function hasValue()
{
if ($this->value != null)
{
return true;
}
else
{
return false;
}
}
public function serialize()
{
return serialize($this->value);
}
public function unserialize($serialized)
{
$this->set(unserialize($serialized), false);
}
public function destructValue()
{
$this->initialValue = null;
if ($this->value instanceof ActiveRecord && !$this->value->isDestructing())
{
$this->value->__destruct();
}
$this->value = null;
}
public function __clone()
{
$this->isModified = true;
$this->initialID = null;
$this->initialValue = null;
if ($this->field instanceof ARPrimaryKeyField)
{
$this->setNull(true);
$this->value = null;
}
}
}
?>