Permalink
Browse files

more merges from CloCkWeRX's clone: logging, DI, better access to und…

…erlying xml
  • Loading branch information...
1 parent 5b5a9d9 commit f4bd5cc1fa2508d8dd17d4ae3f51e686bd79ed7f @kenguest kenguest committed Mar 24, 2012
Showing with 93 additions and 18 deletions.
  1. +29 −11 Services/OpenStreetMap.php
  2. +43 −6 Services/OpenStreetMap/Transport.php
  3. +21 −1 tests/OSMTest.php
View
@@ -136,9 +136,10 @@ public function get($minLon, $minLat, $maxLon, $maxLat)
$url = $config->getValue('server')
. 'api/'
. $config->getValue('api_version')
- . "/map?bbox=$minLat,$minLon,$maxLat,$maxLon";
+ . "/map?bbox=$minLat,$minLon,$maxLat,$maxLon";
$response = $this->getTransport()->getResponse($url);
$this->xml = $response->getBody();
+ return $this->xml;
}
/**
@@ -155,24 +156,36 @@ public function get($minLon, $minLat, $maxLon, $maxLat)
*/
public function getCoordsOfPlace($place)
{
- $url = 'http://nominatim.openstreetmap.org/search?q='
- . urlencode($place) . '&limit=1&format=xml';
- $response = $this->getTransport()->getResponse($url);
- $xml = simplexml_load_string($response->getBody());
- $obj = $xml->xpath('//place');
- if (empty($obj)) {
+ $places = $this->getPlace($place);
+ if (empty($places)) {
throw new Services_OpenStreetMap_Exception(
'Could not get coords for ' . $place
);
}
- $attrs = $xml->named[0];
- $attrs = $obj[0]->attributes();
+ $attrs = $places[0]->attributes();
$lat = (string) $attrs['lat'];
$lon = (string) $attrs['lon'];
return compact('lat', 'lon');
}
/**
+ * Return a structured result set for $place
+ *
+ * @param string $place Location to search for details of
+ *
+ * @return void
+ */
+ public function getPlace($place)
+ {
+ $url = 'http://nominatim.openstreetmap.org/search?q='
+ . urlencode($place) . '&limit=1&format=xml';
+ $response = $this->getTransport()->getResponse($url);
+ $xml = simplexml_load_string($response->getBody());
+ $places = $xml->xpath('//place');
+ return $places;
+ }
+
+ /**
* Given the results of a call to func_get_args return an array of unique
* valid IDs specified in those results (either 1 per argument or each
* argument containing an array of IDs).
@@ -447,15 +460,20 @@ public function getConfig()
/**
* Get current Transport object.
*
- * If one is not defined, create it.
- *
* @return Services_OpenStreetMap_Transport
*/
public function getTransport()
{
return $this->transport;
}
+ /**
+ * set Transport object.
+ *
+ * @param Services_OpenStreetMap_Transport $transport transport object
+ *
+ * @return Services_OpenStreetMap
+ */
public function setTransport($transport)
{
$this->transport = $transport;
@@ -13,6 +13,10 @@
* @link http://pear.php.net/package/Services_OpenStreetMap
*/
+
+require_once 'Log.php';
+require_once 'Log/null.php';
+
/**
* Services_OpenStreetMap_Transport
*
@@ -83,11 +87,26 @@ class Services_OpenStreetMap_Transport
*/
protected $request = null;
+ /**
+ * @var Services_OpenStreetMap_Config $config
+ */
protected $config = null;
- public function __construct() {
+ /**
+ * @var log $log
+ */
+ protected $log = null;
+
+ /**
+ * __construct
+ *
+ * @return Services_OpenStreetMap_Transport
+ */
+ public function __construct()
+ {
$this->setConfig(new Services_OpenStreetMap_Config());
$this->setRequest(new HTTP_Request2());
+ $this->setLog(new Log_null(null));
}
/**
@@ -106,7 +125,7 @@ public function __construct() {
* happened while conversing with
* the server.
*/
- function getResponse(
+ public function getResponse(
$url,
$method = HTTP_Request2::METHOD_GET,
$user = null,
@@ -119,7 +138,7 @@ function getResponse(
$eMsg = null;
if ($this->getConfig()->getValue('verbose')) {
- echo $url, "\n";
+ $this->log->debug($url);
}
$request = $this->getRequest();
$request->setUrl($url);
@@ -154,8 +173,8 @@ function getResponse(
$code = $response->getStatus();
if ($this->getConfig()->getValue('verbose')) {
- var_dump($response->getHeader());
- var_dump($response->getBody());
+ $this->log->debug($response->getHeader());
+ $this->log->debug($response->getBody());
}
if (Services_OpenStreetMap_Transport::OK == $code) {
@@ -171,6 +190,7 @@ function getResponse(
}
} catch (HTTP_Request2_Exception $e) {
+ $this->log->warning($response->getHeader());
throw new Services_OpenStreetMap_Exception(
$e->getMessage(),
$code,
@@ -187,7 +207,7 @@ function getResponse(
*
* @return HTTP_Request2
*/
- function getRequest()
+ public function getRequest()
{
return $this->request;
}
@@ -276,6 +296,9 @@ public function getObjects($type, array $ids)
try {
$response = $this->getResponse($url);
} catch (Services_OpenStreetMap_Exception $ex) {
+ if (isset($response)) {
+ $this->log->warning($response->getHeader());
+ }
switch ($ex->getCode()) {
case Services_OpenStreetMap_Transport::NOT_FOUND:
case Services_OpenStreetMap_Transport::UNAUTHORISED:
@@ -345,6 +368,7 @@ public function searchObjects($type, array $criteria)
try {
$response = $this->getResponse($url);
} catch (Services_OpenStreetMap_Exception $ex) {
+ $this->log->warning($response->getHeader());
switch ($ex->getCode()) {
case Services_OpenStreetMap_Transport::NOT_FOUND:
case Services_OpenStreetMap_Transport::UNAUTHORISED:
@@ -364,6 +388,19 @@ public function searchObjects($type, array $criteria)
}
return $obj;
}
+
+ /**
+ * set Log object
+ *
+ * @param Log $log Log object
+ *
+ * @return Services_OpenStreetMap_Transport
+ */
+ public function setLog($log)
+ {
+ $this->log = $log;
+ return $this;
+ }
}
// vim:set et ts=4 sw=4:
?>
View
@@ -66,7 +66,6 @@ public function testLoadXml()
$this->assertEquals($osm->getXml(), null);
$osm->loadXml(__DIR__ . '/files/osm.osm');
$this->assertNotEquals($osm->getXml(), null);
-
}
/**
@@ -302,6 +301,27 @@ public function testGetArea()
);
}
+ public function testGetReturnValue()
+ {
+ $mock = new HTTP_Request2_Adapter_Mock();
+ $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
+ $mock->addResponse(fopen(__DIR__ . '/responses/area.xml', 'rb'));
+
+ $config = array(
+ 'adapter' => $mock,
+ 'server' => 'http://api06.dev.openstreetmap.org/'
+ );
+ $osm = new Services_OpenStreetMap($config);
+ $results = $osm->search(array('amenity' => 'pharmacy'));
+ $this->AssertTrue(empty($results));
+ $xml = $osm->get(
+ 52.84824191354071, -8.247245026639696,
+ 52.89957825532213, -8.174161478654796
+ );
+ $xml1 = $osm->getXml();
+ $this->assertEquals($xml, $xml1);
+ }
+
/**
* Test searching for a value where it is part of a semicolon delimited
* string.

0 comments on commit f4bd5cc

Please sign in to comment.