1 |
|
<?php |
2 |
|
/** |
3 |
|
* User.php |
4 |
|
* 07-Sep-2011 |
5 |
|
* |
6 |
|
* PHP Version 5 |
7 |
|
* |
8 |
|
* @category Services |
9 |
|
* @package Services_OpenStreetMap |
10 |
|
* @author Ken Guest <kguest@php.net> |
11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
12 |
|
* @version Release: @package_version@ |
13 |
|
* @link User.php |
14 |
|
*/ |
15 |
|
|
16 |
|
/** |
17 |
|
* Services_OpenStreetMap_User |
18 |
|
* |
19 |
|
* @category Services |
20 |
|
* @package Services_OpenStreetMap |
21 |
|
* @author Ken Guest <kguest@php.net> |
22 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
23 |
|
* @link User.php |
24 |
|
*/ |
25 |
|
class Services_OpenStreetMap_User |
26 |
|
{ |
27 |
|
|
28 |
|
protected $preferences = array(); |
29 |
|
|
30 |
|
/** |
31 |
|
* setXml |
32 |
|
* |
33 |
|
* @param SimpleXMLElement $xml XML describing a user. |
34 |
|
* |
35 |
|
* @return Services_OpenStreetMap_User |
36 |
|
*/ |
37 |
|
public function setXml(SimpleXMLElement $xml) |
38 |
|
{ |
39 |
3 |
$this->xml = $xml->saveXML(); |
40 |
3 |
$this->obj = $xml->xpath('//user'); |
41 |
3 |
return $this; |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
|
* setPreferencesXml |
46 |
|
* |
47 |
|
* @param mixed $xml XML describing a user's preferences. |
48 |
|
* |
49 |
|
* @return void |
50 |
|
*/ |
51 |
|
public function setPreferencesXml($xml) |
52 |
|
{ |
53 |
3 |
$this->prefXml = $xml; |
54 |
3 |
$this->prefObj = simplexml_load_string($xml)->xpath('//preferences'); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* Return the attributes set for this user instance. |
59 |
|
* |
60 |
|
* @return string getAttributes() |
61 |
|
*/ |
62 |
|
public function getAttributes() |
63 |
|
{ |
64 |
1 |
return $this->obj[0]->attributes(); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
|
* Return the display name of the user. |
69 |
|
* |
70 |
|
* @return string display name of user. |
71 |
|
*/ |
72 |
|
public function getDisplayName() |
73 |
|
{ |
74 |
1 |
return (string) $this->getAttributes()->display_name; |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
|
* Retrieve date, as a string, representing when the user's account was |
79 |
|
* created. |
80 |
|
* |
81 |
|
* @return string |
82 |
|
*/ |
83 |
|
public function getAccountCreated() |
84 |
|
{ |
85 |
1 |
return (string) $this->getAttributes()->account_created; |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
|
* Return the description set for the user. |
90 |
|
* |
91 |
|
* @return string |
92 |
|
*/ |
93 |
|
public function getDescription() |
94 |
|
{ |
95 |
1 |
$desc = simplexml_load_string($this->xml)->xpath('//user/description'); |
96 |
1 |
return (string) trim($desc[0]); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* Retrieve the id of the user. |
101 |
|
* |
102 |
|
* @return integer id of the object |
103 |
|
*/ |
104 |
|
public function getId() |
105 |
|
{ |
106 |
1 |
return (float) $this->getAttributes()->id; |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Return href to user's profile image, null if not set. |
111 |
|
* |
112 |
|
* @return string |
113 |
|
*/ |
114 |
|
public function getImage() |
115 |
|
{ |
116 |
2 |
$img = simplexml_load_string($this->xml)->xpath('//user/img'); |
117 |
2 |
if (empty($img)) { |
118 |
1 |
return null; |
119 |
1 |
} |
120 |
1 |
return (string) $img[0]->attributes()->href; |
121 |
|
} |
122 |
|
|
123 |
|
/** |
124 |
|
* Return an array of the user's preferred languages. |
125 |
|
* |
126 |
|
* @return array |
127 |
|
*/ |
128 |
|
public function getLanguages() |
129 |
|
{ |
130 |
1 |
$langers = array(); |
131 |
1 |
$cxml = simplexml_load_string($this->xml); |
132 |
1 |
$languages = $cxml->xpath('//user/languages'); |
133 |
1 |
if (empty($languages)) { |
134 |
|
return null; |
135 |
|
} |
136 |
1 |
foreach ($languages[0]->children() as $child) { |
137 |
1 |
if ($child->getName() == 'lang') { |
138 |
1 |
$langers[] = (string) $child[0]; |
139 |
1 |
} |
140 |
1 |
} |
141 |
1 |
return $langers; |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
|
* Latitude of 'home' setting for user. |
146 |
|
* |
147 |
|
* @return float |
148 |
|
*/ |
149 |
|
public function getLat() |
150 |
|
{ |
151 |
2 |
$home = simplexml_load_string($this->xml)->xpath('//user/home'); |
152 |
2 |
if (empty($home)) { |
153 |
|
return null; |
154 |
|
} |
155 |
2 |
return (float) $home[0]->attributes()->lat; |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
|
* Longitude of 'home' setting for user. |
160 |
|
* |
161 |
|
* @return float |
162 |
|
*/ |
163 |
|
public function getLon() |
164 |
|
{ |
165 |
2 |
$cxml = simplexml_load_string($this->xml); |
166 |
2 |
$home = $cxml->xpath('//user/home'); |
167 |
2 |
if (empty($home)) { |
168 |
|
return null; |
169 |
|
} |
170 |
2 |
return (float) $home[0]->attributes()->lon; |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
|
* Zoom level of 'home' setting for user. |
175 |
|
* |
176 |
|
* @return integer |
177 |
|
*/ |
178 |
|
public function getZoom() |
179 |
|
{ |
180 |
1 |
$cxml = simplexml_load_string($this->xml); |
181 |
1 |
$home = $cxml->xpath('//user/home'); |
182 |
1 |
if (empty($home)) { |
183 |
|
return null; |
184 |
|
} |
185 |
1 |
return (integer) $home[0]->attributes()->zoom; |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
|
* The number of changesets opened by the user. |
190 |
|
* |
191 |
|
* @return integer |
192 |
|
*/ |
193 |
|
public function getChangesets() |
194 |
|
{ |
195 |
1 |
$cxml = simplexml_load_string($this->xml); |
196 |
1 |
$changesets = $cxml->xpath('//user/changesets'); |
197 |
1 |
if (empty($changesets)) { |
198 |
|
return null; |
199 |
|
} |
200 |
1 |
return (integer) $changesets[0]->attributes()->count; |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* The number of traces uploaded by the user. |
205 |
|
* |
206 |
|
* @return integer |
207 |
|
*/ |
208 |
|
public function getTraces() |
209 |
|
{ |
210 |
1 |
$cxml = simplexml_load_string($this->xml); |
211 |
1 |
$traces = $cxml->xpath('//user/traces'); |
212 |
1 |
if (empty($traces)) { |
213 |
|
return null; |
214 |
|
} |
215 |
1 |
return (integer) $traces[0]->attributes()->count; |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
|
* The [total] number of blocks received by the user. |
220 |
|
* |
221 |
|
* @return integer |
222 |
|
*/ |
223 |
|
public function getBlocksReceived() |
224 |
|
{ |
225 |
1 |
$cxml = simplexml_load_string($this->xml); |
226 |
1 |
$changesets = $cxml->xpath('//user/blocks/received'); |
227 |
1 |
if (empty($changesets)) { |
228 |
|
return null; |
229 |
|
} |
230 |
1 |
return (integer) $changesets[0]->attributes()->count; |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
|
* The number of active blocks received by the user. |
235 |
|
* |
236 |
|
* @return integer |
237 |
|
*/ |
238 |
|
public function getActiveBlocksReceived() |
239 |
|
{ |
240 |
1 |
$cxml = simplexml_load_string($this->xml); |
241 |
1 |
$changesets = $cxml->xpath('//user/blocks/received'); |
242 |
1 |
if (empty($changesets)) { |
243 |
|
return null; |
244 |
|
} |
245 |
1 |
return (integer) $changesets[0]->attributes()->active; |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
|
* The [total] number of blocks issued by the user. |
250 |
|
* |
251 |
|
* @return integer |
252 |
|
*/ |
253 |
|
public function getBlocksIssued() |
254 |
|
{ |
255 |
1 |
$cxml = simplexml_load_string($this->xml); |
256 |
1 |
$changesets = $cxml->xpath('//user/blocks/issued'); |
257 |
1 |
if (empty($changesets)) { |
258 |
|
return null; |
259 |
|
} |
260 |
1 |
return (integer) $changesets[0]->attributes()->count; |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
|
* The number of active blocks issued by the user. |
265 |
|
* |
266 |
|
* @return integer |
267 |
|
*/ |
268 |
|
public function getActiveBlocksIssued() |
269 |
|
{ |
270 |
1 |
$cxml = simplexml_load_string($this->xml); |
271 |
1 |
$changesets = $cxml->xpath('//user/blocks/issued'); |
272 |
1 |
if (empty($changesets)) { |
273 |
|
return null; |
274 |
|
} |
275 |
1 |
return (integer) $changesets[0]->attributes()->active; |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
|
* Array of names of roles associated with the user. |
280 |
|
* |
281 |
|
* @return array |
282 |
|
*/ |
283 |
|
public function getRoles() |
284 |
|
{ |
285 |
1 |
$ret = array(); |
286 |
1 |
$cxml = simplexml_load_string($this->xml); |
287 |
1 |
$roles = $cxml->xpath('//user/roles'); |
288 |
1 |
if (empty($roles)) { |
289 |
|
return $ret; |
290 |
|
} |
291 |
1 |
foreach ($roles[0]->children() as $child) { |
292 |
1 |
$ret[] = $child->getName(); |
293 |
1 |
} |
294 |
1 |
return $ret; |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
|
* return an array of the user's preferences. |
299 |
|
* |
300 |
|
* @return array |
301 |
|
*/ |
302 |
|
public function getPreferences() |
303 |
|
{ |
304 |
1 |
if ($this->preferences == array()) { |
305 |
|
|
306 |
1 |
$preferences = array(); |
307 |
1 |
foreach ($this->prefObj[0]->children() as $child) { |
308 |
1 |
$key = (string) $child->attributes()->k; |
309 |
1 |
if ($key != '') { |
310 |
1 |
$preferences[$key] = (string) $child->attributes()->v; |
311 |
1 |
} |
312 |
1 |
} |
313 |
1 |
$this->preferences = $preferences; |
314 |
1 |
} |
315 |
1 |
return $this->preferences; |
316 |
|
} |
317 |
|
|
318 |
|
} |
319 |
|
// vim:set et ts=4 sw=4: |
320 |
|
?> |