/
User.php
325 lines (301 loc) · 7.56 KB
/
User.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
<?php
/**
* User.php
* 07-Sep-2011
*
* PHP Version 5
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link User.php
*/
/**
* Services_OpenStreetMap_User
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link User.php
*/
class Services_OpenStreetMap_User
{
/**
* User preferences
*
* @var array
*/
protected $preferences = array();
/**
* setXml
*
* @param SimpleXMLElement $xml XML describing a user.
*
* @return Services_OpenStreetMap_User
*/
public function setXml(SimpleXMLElement $xml)
{
$this->xml = $xml->saveXML();
$this->obj = $xml->xpath('//user');
return $this;
}
/**
* setPreferencesXml
*
* @param mixed $xml XML describing a user's preferences.
*
* @return void
*/
public function setPreferencesXml($xml)
{
$this->prefXml = $xml;
$this->prefObj = simplexml_load_string($xml)->xpath('//preferences');
}
/**
* Return the attributes set for this user instance.
*
* @return string getAttributes()
*/
public function getAttributes()
{
return $this->obj[0]->attributes();
}
/**
* Return the display name of the user.
*
* @return string display name of user.
*/
public function getDisplayName()
{
return (string) $this->getAttributes()->display_name;
}
/**
* Retrieve date, as a string, representing when the user's account was
* created.
*
* @return string
*/
public function getAccountCreated()
{
return (string) $this->getAttributes()->account_created;
}
/**
* Return the description set for the user.
*
* @return string
*/
public function getDescription()
{
$desc = simplexml_load_string($this->xml)->xpath('//user/description');
return (string) trim($desc[0]);
}
/**
* Retrieve the id of the user.
*
* @return integer id of the object
*/
public function getId()
{
return (float) $this->getAttributes()->id;
}
/**
* Return href to user's profile image, null if not set.
*
* @return string
*/
public function getImage()
{
$img = simplexml_load_string($this->xml)->xpath('//user/img');
if (empty($img)) {
return null;
}
return (string) $img[0]->attributes()->href;
}
/**
* Return an array of the user's preferred languages.
*
* @return array
*/
public function getLanguages()
{
$langers = array();
$cxml = simplexml_load_string($this->xml);
$languages = $cxml->xpath('//user/languages');
if (empty($languages)) {
return null;
}
foreach ($languages[0]->children() as $child) {
if ($child->getName() == 'lang') {
$langers[] = (string) $child[0];
}
}
return $langers;
}
/**
* Latitude of 'home' setting for user.
*
* @return float
*/
public function getLat()
{
$home = simplexml_load_string($this->xml)->xpath('//user/home');
if (empty($home)) {
return null;
}
return (float) $home[0]->attributes()->lat;
}
/**
* Longitude of 'home' setting for user.
*
* @return float
*/
public function getLon()
{
$cxml = simplexml_load_string($this->xml);
$home = $cxml->xpath('//user/home');
if (empty($home)) {
return null;
}
return (float) $home[0]->attributes()->lon;
}
/**
* Zoom level of 'home' setting for user.
*
* @return integer
*/
public function getZoom()
{
$cxml = simplexml_load_string($this->xml);
$home = $cxml->xpath('//user/home');
if (empty($home)) {
return null;
}
return (integer) $home[0]->attributes()->zoom;
}
/**
* The number of changesets opened by the user.
*
* @return integer
*/
public function getChangesets()
{
$cxml = simplexml_load_string($this->xml);
$changesets = $cxml->xpath('//user/changesets');
if (empty($changesets)) {
return null;
}
return (integer) $changesets[0]->attributes()->count;
}
/**
* The number of traces uploaded by the user.
*
* @return integer
*/
public function getTraces()
{
$cxml = simplexml_load_string($this->xml);
$traces = $cxml->xpath('//user/traces');
if (empty($traces)) {
return null;
}
return (integer) $traces[0]->attributes()->count;
}
/**
* The [total] number of blocks received by the user.
*
* @return integer
*/
public function getBlocksReceived()
{
$cxml = simplexml_load_string($this->xml);
$changesets = $cxml->xpath('//user/blocks/received');
if (empty($changesets)) {
return null;
}
return (integer) $changesets[0]->attributes()->count;
}
/**
* The number of active blocks received by the user.
*
* @return integer
*/
public function getActiveBlocksReceived()
{
$cxml = simplexml_load_string($this->xml);
$changesets = $cxml->xpath('//user/blocks/received');
if (empty($changesets)) {
return null;
}
return (integer) $changesets[0]->attributes()->active;
}
/**
* The [total] number of blocks issued by the user.
*
* @return integer
*/
public function getBlocksIssued()
{
$cxml = simplexml_load_string($this->xml);
$changesets = $cxml->xpath('//user/blocks/issued');
if (empty($changesets)) {
return null;
}
return (integer) $changesets[0]->attributes()->count;
}
/**
* The number of active blocks issued by the user.
*
* @return integer
*/
public function getActiveBlocksIssued()
{
$cxml = simplexml_load_string($this->xml);
$changesets = $cxml->xpath('//user/blocks/issued');
if (empty($changesets)) {
return null;
}
return (integer) $changesets[0]->attributes()->active;
}
/**
* Array of names of roles associated with the user.
*
* @return array
*/
public function getRoles()
{
$ret = array();
$cxml = simplexml_load_string($this->xml);
$roles = $cxml->xpath('//user/roles');
if (empty($roles)) {
return $ret;
}
foreach ($roles[0]->children() as $child) {
$ret[] = $child->getName();
}
return $ret;
}
/**
* return an array of the user's preferences.
*
* @return array
*/
public function getPreferences()
{
if ($this->preferences == array()) {
$preferences = array();
foreach ($this->prefObj[0]->children() as $child) {
$key = (string) $child->attributes()->k;
if ($key != '') {
$preferences[$key] = (string) $child->attributes()->v;
}
}
$this->preferences = $preferences;
}
return $this->preferences;
}
}
// vim:set et ts=4 sw=4:
?>