-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_source_classes.inc
172 lines (154 loc) · 4.13 KB
/
json_source_classes.inc
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
<?php
/**
* @file
* Base class to handle JSON User sources
*
*/
class UserListJSON extends MigrateListJSON {
/**
* The default implementation assumes the IDs are top-level array elements,
* but the array elements are the data items - we need to look inside them
* for the IDs.
* @param array $data
* @return array
*/
protected function getIDsFromJSON(array $data) {
$ids = array();
foreach ( $data as $item ) {
$ids[] = $item['people_id'];
}
return $ids;
}
}
class UserItemJSON extends MigrateItemJSON {
protected $data = array();
/**
* Two problems with the default getItem() - it reads and parses the JSON on
* each call, which we don't want to do for multiple items in the file; and,
* we need to retrieve a given item from the file according to its 'id' element.
* @param mixed $people_id
* @return null
*/
public function getItem( $people_id ) {
// We cache the parsed JSON at $this->data.
if (empty($this->data)) {
$data = $this->loadJSONUrl( $this->itemUrl );
if ($data) {
// Let's index the array by the ID for easy retrieval.
foreach ($data as $item) {
$this->data[$item->people_id] = $item;
}
}
else {
print_r($people_id);
}
}
// Return the requested item
if (isset($this->data[$people_id])) {
return $this->data[$people_id];
}
else {
return NULL;
}
}
}
/**
* Base class to handle JSON PAGE sources
*
*/
class PageListJSON extends MigrateListJSON {
/**
* The default implementation assumes the IDs are top-level array elements,
* but the array elements are the data items - we need to look inside them
* for the IDs.
*/
protected function getIDsFromJSON(array $data) {
$ids = array();
foreach ($data as $item) {
if (!isset($item['pageid'])) :
//dpm($item);
else :
$ids[] = $item['pageid'];
endif;
}
return $ids;
}
}
class PageItemJSON extends MigrateItemJSON {
protected $data = array();
/**
* Two problems with the default getItem() - it reads and parses the JSON on
* each call, which we don't want to do for multiple items in the file; and,
* we need to retrieve a given item from the file according to its 'id' element.
*/
public function getItem($pageid) {
// We cache the parsed JSON at $this->data.
if (empty($this->data)) {
$data = $this->loadJSONUrl($this->itemUrl);
if ($data) {
// Let's index the array by the ID for easy retrieval.
foreach ($data as $item) {
$this->data[$item->pageid] = $item;
}
}
else {
// Error-handling here....
}
}
// Return the requested item
if (isset($this->data[$pageid])) {
return $this->data[$pageid];
}
else {
return NULL;
}
}
}
/**
* Base class to handle JSON TERMS sources
*
*/
class TermListJSON extends MigrateListJSON {
/**
* The default implementation assumes the IDs are top-level array elements,
* but the array elements are the data items - we need to look inside them
* for the IDs.
*/
protected function getIDsFromJSON(array $data) {
$ids = array();
foreach ($data as $item) {
$ids[] = $item['termid'];
}
return $ids;
}
}
class TermItemJSON extends MigrateItemJSON {
protected $data = array();
/**
* Two problems with the default getItem() - it reads and parses the JSON on
* each call, which we don't want to do for multiple items in the file; and,
* we need to retrieve a given item from the file according to its 'id' element.
*/
public function getItem($termid) {
// We cache the parsed JSON at $this->data.
if (empty($this->data)) {
$data = $this->loadJSONUrl($this->itemUrl);
if ($data) {
// Let's index the array by the ID for easy retrieval.
foreach ($data as $item) {
$this->data[$item->termid] = $item;
}
}
else {
// Error-handling here....
}
}
// Return the requested item
if (isset($this->data[$termid])) {
return $this->data[$termid];
}
else {
return NULL;
}
}
}