1 |
|
<?php |
2 |
|
/** |
3 |
|
* Criterion.php |
4 |
|
* 25-Nov-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 Criterion.php |
14 |
|
*/ |
15 |
|
|
16 |
|
/** |
17 |
|
* Services_OpenStreetMap_Criterion |
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 Criterion.php |
24 |
|
*/ |
25 |
|
class Services_OpenStreetMap_Criterion |
26 |
|
{ |
27 |
|
/** |
28 |
|
* Criterion type. |
29 |
|
* |
30 |
|
* @var mixed |
31 |
|
*/ |
32 |
|
protected $type = null; |
33 |
|
/** |
34 |
|
* Depending on type, value is null, a specified or generated value. |
35 |
|
* |
36 |
|
* @var mixed |
37 |
|
*/ |
38 |
|
protected $value = null; |
39 |
|
|
40 |
|
/** |
41 |
|
* A Criterion is used to specify a condition on how to search through |
42 |
|
* changesets. |
43 |
|
* |
44 |
|
* Search changesets by |
45 |
|
* A user id: |
46 |
|
* Services_OpenStreetMap_Criterion('user', 12345) |
47 |
|
* |
48 |
|
* A display/user name: |
49 |
|
* Services_OpenStreetMap_Criterion('display_name', 'fredflintstone') |
50 |
|
* |
51 |
|
* A bounding box: |
52 |
|
* Services_OpenStreetMap_Criterion('bbox', -8.0590275, 52.9347449, -7.9966939, 52.9611999) |
53 |
|
* |
54 |
|
* For open changesets only: |
55 |
|
* Services_OpenStreetMap_Criterion('open') |
56 |
|
* |
57 |
|
* For closed changesets only: |
58 |
|
* Services_OpenStreetMap_Criterion('closed') |
59 |
|
* |
60 |
|
* For changesets created after a specific time: |
61 |
|
* Services_OpenStreetMap_Criterion('time', '17/11/2011') |
62 |
|
* |
63 |
|
* For changesets created during a specific timespan: |
64 |
|
* Services_OpenStreetMap_Criterion('time', '17/11/2011', '29/11/2011') |
65 |
|
* |
66 |
|
* @return Services_OpenStreetMap_Criterion |
67 |
|
* @throws Services_OpenStreetMap_InvalidArgumentException |
68 |
|
*/ |
69 |
|
public function __construct() |
70 |
|
{ |
71 |
8 |
$args = func_get_args(); |
72 |
8 |
$type = $args[0]; |
73 |
8 |
$this->type = $type; |
74 |
|
switch($type) { |
75 |
8 |
case 'user': |
76 |
4 |
if (is_numeric($args[1])) { |
77 |
3 |
$this->value = $args[1]; |
78 |
3 |
} else { |
79 |
1 |
throw new Services_OpenStreetMap_InvalidArgumentException( |
80 |
|
'User UID must be numeric' |
81 |
1 |
); |
82 |
|
} |
83 |
3 |
break; |
84 |
6 |
case 'bbox': |
85 |
2 |
$minLon = $args[1]; |
86 |
2 |
$minLat = $args[2]; |
87 |
2 |
$maxLon = $args[3]; |
88 |
2 |
$maxLat = $args[4]; |
89 |
2 |
$node = new Services_OpenStreetMap_Node(); |
90 |
|
try { |
91 |
2 |
$node->setLon($minLon); |
92 |
2 |
$node->setLat($minLat); |
93 |
2 |
$node->setLon($maxLon); |
94 |
2 |
$node->setLat($maxLat); |
95 |
2 |
} catch(Services_OpenStreetMap_InvalidArgumentException $ex) { |
96 |
|
throw new Services_OpenStreetMap_InvalidArgumentException( |
97 |
|
$ex->getMessage() |
98 |
|
); |
99 |
|
} |
100 |
2 |
$this->value = "{$minLon},{$minLat},{$maxLon},{$maxLat}"; |
101 |
2 |
break; |
102 |
5 |
case 'display_name': |
103 |
3 |
$this->value = $args[1]; |
104 |
3 |
break; |
105 |
3 |
case 'closed': |
106 |
3 |
case 'open': |
107 |
1 |
break; |
108 |
2 |
case 'time': |
109 |
1 |
$before = null; |
110 |
1 |
$after = null; |
111 |
1 |
if (isset($args[1])) { |
112 |
1 |
$after = $args[1]; |
113 |
1 |
$time = strtotime($after); |
114 |
1 |
if ($time == -1 or $time === false) { |
115 |
|
throw new Services_OpenStreetMap_InvalidArgumentException( |
116 |
|
'Invalid time value' |
117 |
|
); |
118 |
|
} |
119 |
1 |
$after = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time); |
120 |
1 |
} |
121 |
1 |
if (isset($args[2])) { |
122 |
1 |
$before = $args[2]; |
123 |
1 |
$time = strtotime($before); |
124 |
1 |
if ($time == -1 or $time === false) { |
125 |
|
throw new Services_OpenStreetMap_InvalidArgumentException( |
126 |
|
'Invalid time value' |
127 |
|
); |
128 |
|
} |
129 |
1 |
$before = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time); |
130 |
1 |
} |
131 |
1 |
if (!is_null($before)) { |
132 |
1 |
$this->value = "{$after},{$before}"; |
133 |
1 |
} else { |
134 |
|
$this->value = $after; |
135 |
|
} |
136 |
1 |
break; |
137 |
1 |
default: |
138 |
1 |
$this->type = null; |
139 |
1 |
throw new Services_OpenStreetMap_InvalidArgumentException( |
140 |
|
'Unknown constraint type' |
141 |
1 |
); |
142 |
1 |
} |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* Create the required query string portion |
147 |
|
* |
148 |
|
* @return string |
149 |
|
*/ |
150 |
|
public function query() |
151 |
|
{ |
152 |
5 |
switch($this->type) { |
153 |
5 |
case 'bbox': |
154 |
2 |
return "bbox={$this->value}"; |
155 |
4 |
case 'closed': |
156 |
1 |
return 'closed'; |
157 |
4 |
case 'open': |
158 |
|
return 'open'; |
159 |
4 |
case 'display_name': |
160 |
4 |
case 'time': |
161 |
4 |
case 'user': |
162 |
4 |
return http_build_query(array($this->type => $this->value)); |
163 |
|
} |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
|
* Return the criterion type (closed, open, bbox, display_name, or user) |
168 |
|
* |
169 |
|
* @return string |
170 |
|
*/ |
171 |
|
public function type() |
172 |
|
{ |
173 |
6 |
return $this->type; |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
// vim:set et ts=4 sw=4: |
178 |
|
?> |