| 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 |
|
*/ |
| 68 |
|
public function __construct() |
| 69 |
|
{ |
| 70 |
7 |
$args = func_get_args(); |
| 71 |
7 |
$type = $args[0]; |
| 72 |
7 |
$this->type = $type; |
| 73 |
|
switch($type) { |
| 74 |
7 |
case 'user': |
| 75 |
3 |
if (is_numeric($args[1])) { |
| 76 |
3 |
$this->value = $args[1]; |
| 77 |
3 |
} else { |
| 78 |
|
throw new Services_Openstreetmap_InvalidArgumentException( |
| 79 |
|
'User UID must be numeric' |
| 80 |
|
); |
| 81 |
|
} |
| 82 |
3 |
break; |
| 83 |
6 |
case 'bbox': |
| 84 |
2 |
$minLon = $args[1]; |
| 85 |
2 |
$minLat = $args[2]; |
| 86 |
2 |
$maxLon = $args[3]; |
| 87 |
2 |
$maxLat = $args[4]; |
| 88 |
2 |
$node = new Services_Openstreetmap_Node(); |
| 89 |
|
try { |
| 90 |
2 |
$node->setLon($minLon); |
| 91 |
2 |
$node->setLat($minLat); |
| 92 |
2 |
$node->setLon($maxLon); |
| 93 |
2 |
$node->setLat($maxLat); |
| 94 |
2 |
} catch(Services_Openstreetmap_InvalidArgumentException $ex) { |
| 95 |
|
throw new Services_Openstreetmap_InvalidArgumentException( |
| 96 |
|
$ex->getMessage() |
| 97 |
|
); |
| 98 |
|
} |
| 99 |
2 |
$this->value = "{$minLon},{$minLat},{$maxLon},{$maxLat}"; |
| 100 |
2 |
break; |
| 101 |
5 |
case 'display_name': |
| 102 |
3 |
$this->value = $args[1]; |
| 103 |
3 |
break; |
| 104 |
3 |
case 'closed': |
| 105 |
3 |
case 'open': |
| 106 |
1 |
break; |
| 107 |
2 |
case 'time': |
| 108 |
1 |
$before = null; |
| 109 |
1 |
$after = null; |
| 110 |
1 |
if (isset($args[1])) { |
| 111 |
1 |
$after = $args[1]; |
| 112 |
1 |
$time = strtotime($after); |
| 113 |
1 |
if ($time == -1 or $time === false) { |
| 114 |
|
throw new Services_Openstreetmap_InvalidArgumentException( |
| 115 |
|
'Invalid time value' |
| 116 |
|
); |
| 117 |
|
} |
| 118 |
1 |
$after = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time); |
| 119 |
1 |
} |
| 120 |
1 |
if (isset($args[2])) { |
| 121 |
1 |
$before = $args[2]; |
| 122 |
1 |
$time = strtotime($before); |
| 123 |
1 |
if ($time == -1 or $time === false) { |
| 124 |
|
throw new Services_Openstreetmap_InvalidArgumentException( |
| 125 |
|
'Invalid time value' |
| 126 |
|
); |
| 127 |
|
} |
| 128 |
1 |
$before = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time); |
| 129 |
1 |
} |
| 130 |
1 |
if (!is_null($before)) { |
| 131 |
1 |
$this->value = "{$after},{$before}"; |
| 132 |
1 |
} else { |
| 133 |
|
$this->value = $after; |
| 134 |
|
} |
| 135 |
1 |
break; |
| 136 |
1 |
default: |
| 137 |
1 |
$this->type = null; |
| 138 |
1 |
throw new Services_Openstreetmap_InvalidArgumentException( |
| 139 |
|
'Unknown constraint type' |
| 140 |
1 |
); |
| 141 |
1 |
} |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
/** |
| 145 |
|
* Create the required query string portion |
| 146 |
|
* |
| 147 |
|
* @return string |
| 148 |
|
*/ |
| 149 |
|
public function query() |
| 150 |
|
{ |
| 151 |
5 |
switch($this->type) { |
| 152 |
5 |
case 'bbox': |
| 153 |
2 |
return "bbox={$this->value}"; |
| 154 |
4 |
case 'closed': |
| 155 |
1 |
return 'closed'; |
| 156 |
4 |
case 'open': |
| 157 |
|
return 'open'; |
| 158 |
4 |
case 'display_name': |
| 159 |
4 |
case 'time': |
| 160 |
4 |
case 'user': |
| 161 |
4 |
return http_build_query(array($this->type => $this->value)); |
| 162 |
|
} |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
/** |
| 166 |
|
* Return the criterion type (closed, open, bbox, display_name, or user) |
| 167 |
|
* |
| 168 |
|
* @return string |
| 169 |
|
*/ |
| 170 |
|
public function type() |
| 171 |
|
{ |
| 172 |
6 |
return $this->type; |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
// vim:set et ts=4 sw=4: |
| 177 |
|
?> |