-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonsumSubscriptionsOfCustomer.php
73 lines (59 loc) · 1.75 KB
/
MonsumSubscriptionsOfCustomer.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
<?php
/*
* Monsum API Wrapper
* Licensed under the MIT License (MIT)
*/
class MonsumSubscriptionsOfCustomer implements Iterator
{
protected $cst_id;
protected $api_obj;
protected $api_limit;
protected $api_offset;
protected $data;
public function __construct($api_obj, $cst_id, $api_limit=100) {
$this->cst_id = $cst_id;
$this->api_obj = $api_obj;
$this->api_limit = $api_limit;
}
public function rewind() {
$this->api_offset = 0;
$this->data = null;
$this->load();
}
public function current() {
$obj = new MonsumSubscription($api_obj);
$obj->loadSubscriptionFromData($this->data[0]);
return $obj;
}
public function key() {
return $this->data[0]->SUBSCRIPTION_ID;
}
public function next() {
array_shift($this->data);
if(count($this->data) == 0)
$this->load();
}
public function valid() {
return count($this->data) > 0;
}
protected function load() {
if($this->api_offset == -1)
return;
$query = array("SERVICE" => "subscription.get",
"FILTER" => array("CUSTOMER_ID" => $this->cst_id),
"LIMIT" => $this->api_limit,
"OFFSET" => $this->api_offset);
if($this->api_obj->api_call($query, true)) {
$this->data = $this->api_obj->get_data()->SUBSCRIPTIONS;
if(count($this->data) == $this->api_limit)
$this->api_offset = $this->api_offset + count($this->data);
else
$this->api_offset = -1;
}
else {
throw new Exception("Cannot load from API. Rewinding.");
rewind();
}
}
}
?>