forked from bronto/bronto-api-php-client
/
Row.php
732 lines (644 loc) · 18.3 KB
/
Row.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/**
* @author Chris Jones <chris.jones@bronto.com>
* @copyright 2011-2015 Bronto Software, Inc.
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
abstract class Bronto_Api_Row implements ArrayAccess, IteratorAggregate
{
/**
* The data for each column in the row (column_name => value).
* The keys must match the physical names of columns in the
* table for which this row is defined.
*
* @var array
*/
protected $_data = array();
/**
* This is set to a copy of $_data when the data is fetched from
* the API, specified as a new tuple in the constructor, or
* when dirty data is posted to the database with save().
*
* @var array
*/
protected $_cleanData = array();
/**
* Tracks columns where data has been updated. Allows more specific insert and
* update operations.
*
* @var array
*/
protected $_modifiedFields = array();
/**
* Tracks columns that are dates.
*
* @var array
*/
protected $_dateFields = array();
/**
* A row is marked read only if it contains columns that are not physically
* represented within the API schema. This can also be passed as a
* run-time config options as a means of protecting row data.
*
* @var boolean
*/
protected $_readOnly = false;
/**
* Primary row key
*
* @var string
*/
protected $_primary = 'id';
/**
* API Object
*
* @var Bronto_Api_Object
*/
protected $_apiObject;
/**
* @var bool
*/
protected $_isNew = false;
/**
* @var bool
*/
protected $_isError = false;
/**
* @var int
*/
protected $_errorCode;
/**
* @var string
*/
protected $_errorString;
/**
* @var bool
*/
protected $_isLoaded = true;
/**
* Constructor
*
* @param array $config
*/
public function __construct(array $config = array())
{
if (isset($config['apiObject']) && $config['apiObject'] instanceof Bronto_Api_Object) {
$this->_apiObject = $config['apiObject'];
}
if (isset($config['data'])) {
if (!is_array($config['data'])) {
throw new Bronto_Api_Row_Exception('Data must be an array');
}
$this->setData($config['data']);
}
if (isset($config['stored']) && $config['stored'] === true) {
$this->_cleanData = $this->_data;
} else {
$this->_isLoaded = false;
$this->_cleanData = array();
foreach ($this->_data as $key => $value) {
$this->_modifiedFields[$key] = true;
}
}
if (isset($config['readOnly']) && $config['readOnly'] === true) {
$this->_readOnly = true;
}
$this->init();
}
/**
* @param array $data
* @return Bronto_Api_Row
*/
public function setData(array $data = array())
{
if (isset($data['isNew'])) {
$this->_isNew = (bool) $data['isNew'];
$this->_isLoaded = true;
unset($data['isNew']);
}
if (isset($data['isError'])) {
$this->_isError = (bool) $data['isError'];
if ($this->_isError) {
$this->_readOnly = true;
$this->_isLoaded = false;
}
unset($data['isError']);
}
if (isset($data['errorCode'])) {
$this->_errorCode = (int) $data['errorCode'];
unset($data['errorCode']);
}
if (isset($data['errorString'])) {
$this->_errorString = (string) $data['errorString'];
unset($data['errorString']);
}
$this->_data = array_merge($this->_data, $data);
$this->_refresh(false);
$this->init();
return $this;
}
/**
* Initialize object
*
* Called from {@link __construct()} as final step of object instantiation.
*
* @return void
*/
public function init()
{
}
/**
* Proxy to __isset
* Required by the ArrayAccess implementation
*
* @param string $offset
* @return boolean
*/
public function offsetExists($offset)
{
return $this->__isset($offset);
}
/**
* Proxy to __get
* Required by the ArrayAccess implementation
*
* @param string $offset
* @return string
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}
/**
* Proxy to __set
* Required by the ArrayAccess implementation
*
* @param string $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
/**
* Proxy to __unset
* Required by the ArrayAccess implementation
*
* @param string $offset
*/
public function offsetUnset($offset)
{
return $this->__unset($offset);
}
/**
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator((array) $this->_data);
}
/**
* Returns the column/value data as an array.
*
* @return array
*/
public function toArray()
{
return (array) $this->_data;
}
/**
* @return array
*/
public function __toArray()
{
return $this->toArray();
}
/**
* @return Bronto_Api_Row
*/
public function persist()
{
if ($this->_readOnly === true) {
throw new Bronto_Api_Row_Exception(sprintf("Cannot persist a %s record.", $this->getApiObject()->getName()));
}
$type = false;
if ($this->getApiObject()->hasMethodType('addOrUpdate')) {
$type = 'addOrUpdate';
} else {
if (empty($this->_cleanData)) {
$type = 'add';
} else {
$type = 'update';
}
}
return $this->_persist($type);
}
/**
* @return Bronto_Api_Row
*/
public function persistDelete()
{
if (!$this->getApiObject()->hasMethodType('delete')) {
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass('Cannot delete a row of type: ' . $this->getApiObject()->getName());
}
return $this->_persist('delete');
}
/**
* Persist an object for write caching
*
* @param string $type
* @param mixed $defaultIndex
* @return Bronto_Api_Row
*/
public function _persist($type, $defaultIndex = false)
{
$data = array_intersect_key($this->_data, $this->_modifiedFields);
$tempPrimaryKey = $this->_primary;
if (!empty($this->{$tempPrimaryKey})) {
$defaultIndex = $this->{$tempPrimaryKey};
if ($type === 'delete') {
$data = array($this->_primary => $this->{$tempPrimaryKey});
} else {
$data = array_merge(array($this->_primary => $this->{$tempPrimaryKey}), $data);
}
}
$this->getApiObject()->addToWriteCache($type, $data, $defaultIndex);
return $this;
}
/**
* @return Bronto_Api_Row
*/
public function read()
{
$data = array();
if ($this->id) {
$data = array('id' => $this->id);
} else {
throw new Bronto_Api_Row_Exception('Trying to read Row without unique identifier for lookup');
}
$this->_read($data);
return $this;
}
/**
* @param bool $upsert
* @param bool $refresh
* @return Bronto_Api_Row
*/
public function save($upsert = false, $refresh = false)
{
$this->_save($upsert, $refresh);
return $this;
}
/**
* @return Bronto_Api_Row
*/
public function delete()
{
if (!$this->getApiObject()->hasMethodType('delete')) {
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass('Cannot delete a row of type: ' . $this->getApiObject()->getName());
}
$data = array();
if (!$this->id) {
$this->_refresh();
}
if ($this->id) {
$data = array('id' => $this->id);
} else {
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass('Trying to delete Row without unique identifier for lookup');
}
$this->_delete($data);
return $this;
}
/**
* Refreshes properties from the API.
* @param bool $pull
*/
protected function _refresh($pull = true)
{
if ($pull) {
$this->read();
}
$this->_cleanData = $this->_data;
$this->_modifiedFields = array();
}
/**
* @param array $filter
*/
protected function _read(array $filter = array())
{
if (empty($filter)) {
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass('Trying to read Row without unique identifier for lookup');
}
/* @var $rowset Bronto_Api_Rowset */
$rowset = $this->getApiObject()->readAll($filter);
if ($rowset->hasErrors()) {
// Reset class
$error = $rowset->getError();
$this->_readOnly = true;
$this->_isLoaded = false;
$this->_isError = true;
$this->_errorCode = $error['code'];
$this->_errorString = $error['message'];
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass($error['message'], $error['code']);
}
if ($rowset->count() > 0) {
// Reset all fields
$this->_isLoaded = true;
$this->_readOnly = false;
$this->_isError = false;
$this->_isNew = false;
$data = $rowset->offsetGetData(0);
$this->setData($data);
}
}
/**
* Saves the properties to the API.
*
* This performs an intelligent add/update, and can reload the
* properties with fresh data from the API on success.
*
* @param bool $upsert
* @param bool $refresh
*/
protected function _save($upsert = true, $refresh = false)
{
/**
* If the _cleanData array is empty,
* this is an ADD of a new row.
* Otherwise it is an UPDATE.
*/
if (empty($this->_cleanData)) {
if ($upsert) {
if ($this->getApiObject()->hasMethodType('addOrUpdate')) {
$this->_add(true);
} else {
$this->_add(false);
}
} else {
$this->_add(false);
}
} else {
$this->_update();
}
$refreshOnSave = $this->getApi()->getOption('refresh_on_save');
if ($refreshOnSave || $refresh) {
$this->_refresh();
}
}
/**
* @param bool $upsert
*/
protected function _add($upsert = false)
{
if ($this->_readOnly === true) {
throw new Bronto_Api_Row_Exception(sprintf("Cannot create %s record.", $this->getApiObject()->getName()));
}
$data = array_intersect_key($this->_data, $this->_modifiedFields);
if ($upsert) {
$tempPrimaryKey = $this->_primary;
if (!empty($this->{$tempPrimaryKey})) {
$data = array_merge(array($this->_primary => $this->{$tempPrimaryKey}), $data);
}
$rowset = $this->getApiObject()->addOrUpdate(array($data));
} else {
$rowset = $this->getApiObject()->add(array($data));
}
if ($rowset->hasErrors()) {
// Reset class
$error = $rowset->getError();
$this->_readOnly = true;
$this->_isLoaded = false;
$this->_isError = true;
$this->_errorCode = $error['code'];
$this->_errorString = $error['message'];
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass($error['message'], $error['code']);
}
if ($rowset->count() > 0) {
// Reset all fields
$this->_isLoaded = true;
$this->_readOnly = false;
$this->_isError = false;
$this->_isNew = false;
$data = $rowset->offsetGetData(0);
$this->setData($data);
}
}
protected function _update()
{
if ($this->_readOnly === true) {
throw new Bronto_Api_Row_Exception(sprintf("Cannot update %s record.", $this->getApiObject()->getName()));
}
$data = array_intersect_key($this->_data, $this->_modifiedFields);
if (count($data) > 0) {
$tempPrimaryKey = $this->_primary;
if (!empty($this->{$tempPrimaryKey})) {
$data = array_merge(array($this->_primary => $this->{$tempPrimaryKey}), $data);
}
$rowset = $this->getApiObject()->update(array($data));
if ($rowset->hasErrors()) {
// Reset class
$error = $rowset->getError();
$this->_readOnly = true;
$this->_isLoaded = false;
$this->_isError = true;
$this->_errorCode = $error['code'];
$this->_errorString = $error['message'];
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass($error['message'], $error['code']);
}
if ($rowset->count() > 0) {
// Reset all fields
$this->_isLoaded = true;
$this->_readOnly = false;
$this->_isError = false;
$this->_isNew = false;
$data = $rowset->offsetGetData(0);
$this->setData($data);
}
}
}
/**
* @param array $data
*/
protected function _delete(array $data)
{
if ($this->_readOnly === true) {
throw new Bronto_Api_Row_Exception(sprintf("Cannot delete this read-only %s record.", $this->getApiObject()->getName()));
}
$rowset = $this->getApiObject()->delete(array($data));
if ($rowset->hasErrors()) {
// Reset class
$error = $rowset->getError();
$this->_readOnly = true;
$this->_isLoaded = false;
$this->_isError = true;
$this->_errorCode = $error['code'];
$this->_errorString = $error['message'];
$exceptionClass = $this->getApiObject()->getExceptionClass();
throw new $exceptionClass($error['message'], $error['code']);
}
if ($rowset->count() > 0) {
// Reset all fields to indicate that the row is not there
$this->_data = array();
$this->_cleanData = array();
$this->_modifiedFields = array();
$this->_isLoaded = false;
$this->_readOnly = true;
$this->_isError = false;
$this->_isNew = false;
$data = $rowset->offsetGetData(0);
$this->setData($data);
}
}
/**
* Retrieve row field value
*
* @param string $columnName The user-specified column name.
* @return string The corresponding column value.
*/
public function __get($columnName)
{
if (!array_key_exists($columnName, $this->_data)) {
return null;
}
return $this->_data[$columnName];
}
/**
* Set row field value
*
* @param string $columnName The column key.
* @param mixed $value The value for the property.
*/
public function __set($columnName, $value)
{
if ($this->_readOnly === false) {
$this->_data[$columnName] = $value;
$this->_modifiedFields[$columnName] = true;
}
}
/**
* Unset row field value
*
* @param string $columnName The column key.
*/
public function __unset($columnName)
{
if ($this->_readOnly === false) {
unset($this->_data[$columnName]);
}
}
/**
* Test existence of row field
*
* @param string $columnName The column key.
* @return bool
*/
public function __isset($columnName)
{
return array_key_exists($columnName, $this->_data);
}
/**
* @param Bronto_Api_Object $apiObject
* @return Bronto_Api_Row
*/
public function setApiObject(Bronto_Api_Object $apiObject)
{
$this->_apiObject = $apiObject;
return $this;
}
/**
* @return Bronto_Api_Object
*/
public function getApiObject()
{
return $this->_apiObject;
}
/**
* @return Bronto_Api
*/
public function getApi()
{
return $this->_apiObject->getApi();
}
/**
* Test the read-only status of the row.
*
* @return boolean
*/
public function isReadOnly()
{
return $this->_readOnly;
}
/**
* Set the read-only status of the row.
*
* @param boolean $flag
* @return boolean
*/
public function setReadOnly($flag)
{
$this->_readOnly = (bool) $flag;
}
/**
* @return array
*/
public function getData()
{
return $this->_data;
}
/**
* @return array
*/
public function getDateFields()
{
return $this->_dateFields;
}
/**
* @param string $key
* @return bool
*/
public function isDateField($key)
{
return (bool) isset($this->_dateFields[$key]);
}
/**
* @return bool
*/
public function isNew()
{
return (bool) $this->_isNew;
}
/**
* @return bool
*/
public function hasError()
{
return (bool) $this->_isError;
}
/**
* @return int|bool
*/
public function getErrorCode()
{
if ($this->hasError()) {
return $this->_errorCode;
}
return false;
}
/**
* @return int|bool
*/
public function getErrorMessage()
{
if ($this->hasError()) {
return $this->_errorString;
}
return false;
}
}