ソースを参照

CS and cleanup

euromark 11 年 前
コミット
ae032b0acc

+ 32 - 251
Lib/GeocodeLib.php

@@ -223,11 +223,13 @@ class GeocodeLib {
 	}
 
 	/**
+	 * Return the geocoder result or empty array on failure
+	 *
 	 * @return array result
 	 */
 	public function getResult() {
 		if ($this->result === null) {
-			return false;
+			return array();
 		}
 
 		if (is_string($this->result)) {
@@ -235,7 +237,7 @@ class GeocodeLib {
 		}
 
 		if (!is_array($this->result)) {
-			return false;
+			return array();
 		}
 
 		return $this->result;
@@ -276,7 +278,6 @@ class GeocodeLib {
 			}
 
 			$status = $result['status'];
-			//debug(compact('result', 'requestUrl', 'success'));
 
 			if ($status == self::CODE_SUCCESS) {
 
@@ -436,14 +437,14 @@ class GeocodeLib {
 			} else {
 
 				// something went wrong
-				$error_message = (isset($result['error_message']) ? $result['error_message'] : '');
-				if (empty($error_message)) {
-					$error_message = (isset($this->statusCodes[$status]) ? $this->statusCodes[$status] : '');
+				$errorMessage = (isset($result['error_message']) ? $result['error_message'] : '');
+				if (empty($errorMessage)) {
+					$errorMessage = (isset($this->statusCodes[$status]) ? $this->statusCodes[$status] : '');
 				}
-				if (empty($error_message)) {
-					$error_message = 'unknown';
+				if (empty($errorMessage)) {
+					$errorMessage = 'unknown';
 				}
-				$this->setError('Error ' . $status . ' (' . $error_message . ')');
+				$this->setError('Error ' . $status . ' (' . $errorMessage . ')');
 
 				if ($this->options['log']) {
 					CakeLog::write('geocode', __('Could not geocode \'%s\'', $address));
@@ -504,6 +505,12 @@ class GeocodeLib {
 		return $accuracy < $this->options['min_accuracy'];
 	}
 
+	/**
+	 * GeocodeLib::_transform()
+	 *
+	 * @param string $record
+	 * @return array
+	 */
 	protected function _transform($record) {
 		if ($this->options['output'] === 'json') {
 			return $this->_transformJson($record);
@@ -511,6 +518,12 @@ class GeocodeLib {
 		return $this->_transformXml($record);
 	}
 
+	/**
+	 * GeocodeLib::_transformJson()
+	 *
+	 * @param string $record
+	 * @return array
+	 */
 	protected function _transformJson($record) {
 		if (!is_array($record)) {
 			$record = json_decode($record, true);
@@ -518,7 +531,12 @@ class GeocodeLib {
 		return $this->_transformData($record);
 	}
 
+	/**
+	 * @return array
+	 * @deprecated
+	 */
 	protected function _transformXml($record) {
+		trigger_error('deprecated, use json instead', E_USER_DEPRECATED);
 		if (!is_array($record)) {
 			$xml = Xml::build($record);
 			$record = Xml::toArray($xml);
@@ -573,7 +591,7 @@ class GeocodeLib {
 	 * @param mixed $record any level of input, whole raw array or records or single record
 	 * @return array $record organized & normalized
 	 */
-	public function _transformData($record) {
+	protected function _transformData($record) {
 		if (!is_array($record)) {
 			return $record;
 		}
@@ -741,7 +759,7 @@ class GeocodeLib {
 	 * @param array pointX
 	 * @param array pointY
 	 * @param float $unit (M=miles, K=kilometers, N=nautical miles, I=inches, F=feet)
-	 * @return integer distance: in km
+	 * @return int Distance in km
 	 */
 	public function distance(array $pointX, array $pointY, $unit = null) {
 		if (empty($unit) || !array_key_exists(($unit = strtoupper($unit)), $this->units)) {
@@ -808,15 +826,15 @@ class GeocodeLib {
 	 * Useful if you store other users' locations and want to grant some
 	 * privacy protection. This way the coordinates will be slightly modified.
 	 *
-	 * @param float coord
-	 * @param integer level (0 = nothing to 5 = extrem)
+	 * @param float coord Coordinates
+	 * @param integer level The Level of blurness (0 = nothing to 5 = extrem)
 	 * - 1:
 	 * - 2:
 	 * - 3:
 	 * - 4:
 	 * - 5:
+	 * @return float Coordinates
 	 * @throws CakeException
-	 * @return float coord
 	 */
 	public static function blur($coord, $level = 0) {
 		if (!$level) {
@@ -876,240 +894,3 @@ http://code.google.com/intl/de-DE/apis/maps/documentation/geocoding/
 - whats the difference to "http://maps.google.com/maps/api/geocode/output?parameters"
 
 */
-
-/*
-
-Example: NEW:
-
-Array
-(
-	[status] => OK
-	[Result] => Array
-		(
-			[type] => postal_code
-			[formatted_address] => 74523, Deutschland
-			[AddressComponent] => Array
-				(
-					[0] => Array
-						(
-							[long_name] => 74523
-							[short_name] => 74523
-							[type] => postal_code
-						)
-
-					[1] => Array
-						(
-							[long_name] => Schwaebisch Hall
-							[short_name] => SHA
-							[Type] => Array
-								(
-									[0] => administrative_area_level_2
-									[1] => political
-								)
-
-						)
-
-					[2] => Array
-						(
-							[long_name] => Baden-Wuerttemberg
-							[short_name] => BW
-							[Type] => Array
-								(
-									[0] => administrative_area_level_1
-									[1] => political
-								)
-
-						)
-
-					[3] => Array
-						(
-							[long_name] => Deutschland
-							[short_name] => DE
-							[Type] => Array
-								(
-									[0] => country
-									[1] => political
-								)
-
-						)
-
-				)
-
-			[Geometry] => Array
-				(
-					[Location] => Array
-						(
-							[lat] => 49.1257616
-							[lng] => 9.7544127
-						)
-
-					[location_type] => APPROXIMATE
-					[Viewport] => Array
-						(
-							[Southwest] => Array
-								(
-									[lat] => 49.0451477
-									[lng] => 9.6132550
-								)
-
-							[Northeast] => Array
-								(
-									[lat] => 49.1670260
-									[lng] => 9.8756350
-								)
-
-						)
-
-					[Bounds] => Array
-						(
-							[Southwest] => Array
-								(
-									[lat] => 49.0451477
-									[lng] => 9.6132550
-								)
-
-							[Northeast] => Array
-								(
-									[lat] => 49.1670260
-									[lng] => 9.8756350
-								)
-
-						)
-
-				)
-
-		)
-
-)
-
-Example OLD:
-
-Array
-(
-	[name] => 74523 Deutschland
-	[Status] => Array
-		(
-			[code] => 200
-			[request] => geocode
-		)
-
-	[Result] => Array
-		(
-			[id] => p1
-			[address] => 74523, Deutschland
-			[AddressDetails] => Array
-				(
-					[Accuracy] => 5
-					[xmlns] => urn:oasis:names:tc:ciq:xsdschema:xAL:2.0
-					[Country] => Array
-						(
-							[CountryNameCode] => DE
-							[CountryName] => Deutschland
-							[AdministrativeArea] => Array
-								(
-									[AdministrativeAreaName] => Baden-Wuerttemberg
-									[SubAdministrativeArea] => Array
-										(
-											[SubAdministrativeAreaName] => Schwaebisch Hall
-											[PostalCode] => Array
-												(
-													[PostalCodeNumber] => 74523
-												)
-
-										)
-
-								)
-
-						)
-
-				)
-
-			[ExtendedData] => Array
-				(
-					[LatLonBox] => Array
-						(
-							[north] => 49.1670260
-							[south] => 49.0451477
-							[east] => 9.8756350
-							[west] => 9.6132550
-						)
-
-				)
-
-			[Point] => Array
-				(
-					[coordinates] => 9.7544127,49.1257616,0
-				)
-
-		)
-
-) {
-	"status": "OK",
-	"results": [ {
-	"types": [ "street_address" ],
-	"formatted_address": "Krebenweg 20, 74523 Schwäbisch Hall, Deutschland",
-	"address_components": [ {
-		"long_name": "20",
-		"short_name": "20",
-		"types": [ "street_number" ]
-	}, {
-		"long_name": "Krebenweg",
-		"short_name": "Krebenweg",
-		"types": [ "route" ]
-	}, {
-		"long_name": "Bibersfeld",
-		"short_name": "Bibersfeld",
-		"types": [ "sublocality", "political" ]
-	}, {
-		"long_name": "Schwäbisch Hall",
-		"short_name": "Schwäbisch Hall",
-		"types": [ "locality", "political" ]
-	}, {
-		"long_name": "Schwäbisch Hall",
-		"short_name": "SHA",
-		"types": [ "administrative_area_level_2", "political" ]
-	}, {
-		"long_name": "Baden-Württemberg",
-		"short_name": "BW",
-		"types": [ "administrative_area_level_1", "political" ]
-	}, {
-		"long_name": "Deutschland",
-		"short_name": "DE",
-		"types": [ "country", "political" ]
-	}, {
-		"long_name": "74523",
-		"short_name": "74523",
-		"types": [ "postal_code" ]
-	} ],
-	"geometry": {
-		"location": {
-		"lat": 49.0817369,
-		"lng": 9.6908451
-		},
-		"location_type": "RANGE_INTERPOLATED", //ROOFTOP //APPROXIMATE
-		"viewport": {
-		"southwest": {
-			"lat": 49.0785954,
-			"lng": 9.6876999
-		},
-		"northeast": {
-			"lat": 49.0848907,
-			"lng": 9.6939951
-		}
-		},
-		"bounds": {
-		"southwest": {
-			"lat": 49.0817369,
-			"lng": 9.6908451
-		},
-		"northeast": {
-			"lat": 49.0817492,
-			"lng": 9.6908499
-		}
-		}
-	},
-	"partial_match": true
-	} ]
-}
-
-*/

+ 3 - 5
Model/Behavior/GeocoderBehavior.php

@@ -94,7 +94,6 @@ class GeocoderBehavior extends ModelBehavior {
 		if ($this->settings[$Model->alias]['real']) {
 			foreach ($addressfields as $field) {
 				if (!$Model->hasField($field)) {
-					//debug("Field Missing: {$field}");
 					return $return;
 				}
 			}
@@ -112,7 +111,6 @@ class GeocoderBehavior extends ModelBehavior {
 		// See if we should geocode //TODO: reverse and return here
 		$fieldsExist = (!$this->settings[$Model->alias]['real'] || ($Model->hasField($this->settings[$Model->alias]['lat']) && $Model->hasField($this->settings[$Model->alias]['lng'])));
 		if (!$fieldsExist) {
-			//debug(compact('fieldsExist'));
 			return false;
 		}
 
@@ -123,7 +121,7 @@ class GeocoderBehavior extends ModelBehavior {
 			return false;
 		}
 
-		// yup - we are geocoding
+		// Yup - we are geocoding
 		if (!empty($Model->whitelist) && (!in_array($this->settings[$Model->alias]['lat'], $Model->whitelist) || !in_array($this->settings[$Model->alias]['lng'], $Model->whitelist))) {
 			/** HACK to prevent 0 inserts if not wanted! just use whitelist now to narrow fields down - 2009-03-18 ms */
 			//$Model->whitelist[] = $this->settings[$Model->alias]['lat'];
@@ -142,7 +140,7 @@ class GeocoderBehavior extends ModelBehavior {
 
 		// if both are 0, thats not valid, otherwise continue
 		if (empty($geocode['lat']) && empty($geocode['lng'])) {
-			/** HACK to prevent 0 inserts of incorrect runs - 2009-04-07 ms */
+			// HACK to prevent 0 inserts of incorrect runs
 			if (isset($Model->data[$Model->alias][$this->settings[$Model->alias]['lat']])) {
 				unset($Model->data[$Model->alias][$this->settings[$Model->alias]['lat']]);
 			}
@@ -158,7 +156,7 @@ class GeocoderBehavior extends ModelBehavior {
 			return true;
 		}
 
-		// valid lat/lng found
+		// Valid lat/lng found
 		$Model->data[$Model->alias][$this->settings[$Model->alias]['lat']] = $geocode['lat'];
 		$Model->data[$Model->alias][$this->settings[$Model->alias]['lng']] = $geocode['lng'];
 

+ 58 - 1
Test/Case/Model/Behavior/GeocoderBehaviorTest.php

@@ -7,7 +7,7 @@ App::uses('AppController', 'Controller');
 class GeocoderBehaviorTest extends CakeTestCase {
 
 	public $fixtures = array(
-		'core.comment', 'plugin.tools.address'
+		'core.comment', 'plugin.tools.address', 'core.cake_session'
 	);
 
 	public function setUp() {
@@ -18,6 +18,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false));
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testDistance()
+	 *
+	 * @return void
+	 */
 	public function testDistance() {
 		$res = $this->Comment->distance(12, 14);
 		$expected = '6371.04 * ACOS(COS(PI()/2 - RADIANS(90 - Comment.lat)) * COS(PI()/2 - RADIANS(90 - 12)) * COS(RADIANS(Comment.lng) - RADIANS(14)) + SIN(PI()/2 - RADIANS(90 - Comment.lat)) * SIN(PI()/2 - RADIANS(90 - 12)))';
@@ -36,12 +41,22 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertEquals($expected, $res);
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testDistanceField()
+	 *
+	 * @return void
+	 */
 	public function testDistanceField() {
 		$res = $this->Comment->distanceField(12, 14);
 		$expected = '6371.04 * ACOS(COS(PI()/2 - RADIANS(90 - Comment.lat)) * COS(PI()/2 - RADIANS(90 - 12)) * COS(RADIANS(Comment.lng) - RADIANS(14)) + SIN(PI()/2 - RADIANS(90 - Comment.lat)) * SIN(PI()/2 - RADIANS(90 - 12))) AS Comment.distance';
 		$this->assertEquals($expected, $res);
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testSetDistanceAsVirtualField()
+	 *
+	 * @return void
+	 */
 	public function testSetDistanceAsVirtualField() {
 		$this->Address = ClassRegistry::init('Address');
 		$this->Address->Behaviors->load('Tools.Geocoder');
@@ -53,6 +68,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue($res[0]['Address']['distance'] > 640 && $res[0]['Address']['distance'] < 650);
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testSetDistanceAsVirtualFieldInMiles()
+	 *
+	 * @return void
+	 */
 	public function testSetDistanceAsVirtualFieldInMiles() {
 		$this->Address = ClassRegistry::init('Address');
 		$this->Address->Behaviors->load('Tools.Geocoder', array('unit' => GeocodeLib::UNIT_MILES));
@@ -64,6 +84,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue($res[0]['Address']['distance'] > 390 && $res[0]['Address']['distance'] < 410);
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testPagination()
+	 *
+	 * @return void
+	 */
 	public function testPagination() {
 		$this->Controller = new TestController(new CakeRequest(null, false), null);
 		$this->Controller->constructClasses();
@@ -78,6 +103,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testValidate()
+	 *
+	 * @return void
+	 */
 	public function testValidate() {
 		$is = $this->Comment->validateLatitude(44);
 		$this->assertTrue($is);
@@ -108,6 +138,8 @@ class GeocoderBehaviorTest extends CakeTestCase {
 
 	/**
 	 * Geocoding tests using the google webservice
+	 *
+	 * @return void
 	 */
 	public function testBasic() {
 		//echo '<h3>'.__FUNCTION__.'</h3>';
@@ -143,6 +175,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testMinAccLow()
+	 *
+	 * @return void
+	 */
 	public function testMinAccLow() {
 		//echo '<h3>'.__FUNCTION__.'</h3>';
 
@@ -160,6 +197,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testMinAccHigh()
+	 *
+	 * @return void
+	 */
 	public function testMinAccHigh() {
 		//echo '<h3>'.__FUNCTION__.'</h3>';
 
@@ -177,6 +219,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testMinInc()
+	 *
+	 * @return void
+	 */
 	public function testMinInc() {
 		//echo '<h3>'.__FUNCTION__.'</h3>';
 
@@ -201,6 +248,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testMinIncAllowed()
+	 *
+	 * @return void
+	 */
 	public function testMinIncAllowed() {
 		//echo '<h3>'.__FUNCTION__.'</h3>';
 
@@ -224,6 +276,11 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
 	}
 
+	/**
+	 * GeocoderBehaviorTest::testExpect()
+	 *
+	 * @return void
+	 */
 	public function testExpect() {
 		$this->Comment->Behaviors->unload('Geocoder');
 		$this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'expect' => array('postal_code')));