-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPodioFetchAll.php
149 lines (139 loc) · 5.98 KB
/
PodioFetchAll.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
<?php
require_once 'RateLimitChecker.php';
/**
* Description of PodioFetchAll
*
* @author SCHRED
*/
class PodioFetchAll {
/**
* Wrapper to fetch all elements besides some from podio imposed maxresult.
*
* Examples:
*
* $result = PodioFetchAll::iterateApiCall('PodioFile::get_for_app', "YOUR_APP_ID", array('attached_to' => 'item'));
* $result = PodioFetchAll::iterateApiCall('PodioItem::filter', "YOUR_APP_ID", array(), "items");
*
* @param type $function e.g. 'PodioFile::get_for_app'
* @param type $id first parameter of function
* @param type $params
* @param int $limit no of elements fetched on each call
* @param String $resulttype if set, the result of the call is suspected to be in $result[$resulttype]
* @return type array of all fetched elements
*/
static function iterateApiCall($function, $id, $params = array(), $limit = 100, $resulttype = null) {
$completed = false;
$iteration = 0;
$result = array();
while (!$completed) {
#$tmp_result = $function($id, array_merge($params, array("limit" => $limit, 'offset' => $limit * $iteration)));
$tmp_result = call_user_func($function, $id, array_merge($params, array('limit' => $limit, 'offset' => $limit * $iteration)));
RateLimitChecker::preventTimeOut();
#echo "done iteration $iteration\n";
$iteration++;
if ($tmp_result instanceof PodioCollection) {
$result = array_merge($result, $tmp_result->_get_items());
if ($tmp_result instanceof PodioItemCollection) {
echo "filtered: $tmp_result->filtered, total: $tmp_result->total (limit: $limit)\n";
if (sizeof($tmp_result->_get_items()) < $limit) {
$completed = true;
}
} else {
echo "WARNING unexpected collection: " . get_class($tmp_result);
}
} else if (isset($resulttype)) {
if (is_array($tmp_result) && isset($tmp_result[$resulttype]) && is_array($tmp_result[$resulttype])) {
$result = array_merge($result, $tmp_result[$resulttype]);
if (sizeof($tmp_result[$resulttype]) < $limit) {
$completed = true;
}
} else {
$completed = true;
}
} else {
if (is_array($tmp_result)) {
$result = array_merge($result, $tmp_result);
if (sizeof($tmp_result) < $limit) {
$completed = true;
}
} else {
$completed = true;
}
}
unset($tmp_result);
}
return $result;
}
public static function podioElements(array $elements) {
return array('__attributes' => $elements, '__properties' => $elements);
}
/**
* Removes all elements/properties of $object, that are not defined in $elements.
* This can be used to save memory when handling large lists of items.
*
* Be aware that podio objects make use of the __get(..) and __set(..) funktions,
* hence a direct removal of the attributes is not possible - on might use PodioFetchAll::podioElemnts(..)
*
* Example usage:
*
* PodioFetchAll::flattenObjectsArray($appFiles, array('__attributes' =>
* array('file_id' => null, 'name' => null, 'link' => null, 'hosted_by' => null,
* 'context' => array('id' => NULL, 'type' => null, 'title' => null)),
* '__properties' => array('file_id' => null, 'name' => null, 'link' => null, 'hosted_by' => null,
* 'context' => NULL)));
*
* analogous:
*
* PodioFetchAll::flattenObjectsArray($appFiles,
* PodioFetchAll::podioElemnts(array('file_id' => null, 'name' => null, 'link' => null, 'hosted_by' => null,
* 'context' => array('id' => NULL, 'type' => null, 'title' => null)));
*
*
* @param type $object can be class or array
* @param array $elements
*/
public static function flattenObject(&$object, array $elements) {
//unset all propterties/elements of $object:
if (is_array($object)) {
foreach (array_keys($object) as $key) {
if (array_key_exists($key, $elements)) {
#echo "found array-key: $key -> $elements[$key]\n";
if (is_array($elements[$key])) {
#echo "flattening key. for elements $elements[$key]\n";
self::flattenObject($object[$key], $elements[$key]);
} #else: do nothing
} else {
#echo "unsetting $key\n";
unset($object[$key]);
}
}
} else {
foreach (array_keys(get_object_vars($object)) as $key) {
if (array_key_exists($key, $elements)) {
#echo "found object-key: $key -> $elements[$key]\n";
#var_dump($elements[$key]);
if (is_array($elements[$key])) {
#echo "flattening key. for elements $elements[$key]\n";
self::flattenObject($object->$key, $elements[$key]);
} #else: do nothing
} else {
#echo "unsetting $key\n";
unset($object->$key);
}
}
}
}
/**
* @see PodioFetchAll::flattenObject(..)
* @param array $objects
* @param array $elements
* @return array
*/
public static function flattenObjectsArray(array &$objects, array $elements) {
$start = time();
foreach ($objects as $object) {
self::flattenObject($object, $elements);
}
echo "flattening took " . (time() - $start) . " seconds.\n";
}
}