-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZohoCrmModuleIterator.php
76 lines (64 loc) · 2.21 KB
/
ZohoCrmModuleIterator.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
<?php
class ZohoCrmModuleIterator implements Iterator {
protected $zohoCrmModule;
protected $options;
protected $data=array();
protected $position;
protected $range=null;
protected $method;
protected $batchSize = 200;
public function __construct(ZohoCrmModule $zohoCrmModule, $options=array(), $method="getRecords") {
$this->zohoCrmModule = $zohoCrmModule;
$this->options = $options;
$this->method = $method;
if (array_key_exists('BatchSize', $this->options)) {
$this->batchSize = $this->options['BatchSize'];
}
}
protected function isInRange($position) {
return ($this->range && $position >= $this->range['fromIndex'] && $position <= $this->range['toIndex']);
}
protected function getRecord($record) {
$result = array();
foreach ($record->FL as $field) {
$result[$field->val] = $field->content;
}
$this->data[$this->range['fromIndex']+$record->no-1] = $result;
}
protected function getRange($position) {
if ($this->isInRange($position)) return;
$this->data = array();
$this->range = array();
$this->range['fromIndex'] = (floor(($position-1)/$this->batchSize)*$this->batchSize)+1;
$this->range['toIndex'] = $this->range['fromIndex']+$this->batchSize-1;
$options = $this->range+$this->options;
$response = $this->zohoCrmModule->call($this->method, $options)->response;
if (isset($response->result)) {
if (count($response->result->{$this->zohoCrmModule->name}->row) === 1) {
$this->getRecord($response->result->{$this->zohoCrmModule->name}->row);
} else {
foreach ($response->result->{$this->zohoCrmModule->name}->row as $record) {
$this->getRecord($record);
}
}
}
}
public function rewind() {
$this->position = 1;
}
function current() {
return $this->data[$this->position];
}
function key() {
return $this->position;
}
function next() {
++$this->position;
}
function valid() {
if (!$this->isInRange($this->position)) {
$this->getRange($this->position);
}
return array_key_exists($this->position, $this->data);
}
}