Index: branches/Maps0.8/includes/manipulations/Maps_ParamGeoService.php |
— | — | @@ -0,0 +1,239 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Parameter manipulation ensuring the value is a geocoding service, |
| 6 | + * and taking care of both alias resolving and overrides handling. |
| 7 | + * |
| 8 | + * @since 0.7.5 |
| 9 | + * |
| 10 | + * @file Maps_ParamGeoService.php |
| 11 | + * @ingroup Maps |
| 12 | + * @ingroup ParameterManipulations |
| 13 | + * |
| 14 | + * @author Jeroen De Dauw |
| 15 | + */ |
| 16 | +class MapsParamGeoService extends ItemParameterManipulation { |
| 17 | + |
| 18 | + /** |
| 19 | + * @since 0.7.5 |
| 20 | + * |
| 21 | + * @var string or false |
| 22 | + */ |
| 23 | + protected $mappingServiceParam; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructor. |
| 27 | + * |
| 28 | + * @since 0.7.5 |
| 29 | + */ |
| 30 | + public function __construct( $mappingServiceParam = false ) { |
| 31 | + parent::__construct(); |
| 32 | + |
| 33 | + $this->mappingServiceParam = $mappingServiceParam; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @see ItemParameterManipulation::doManipulation |
| 38 | + * |
| 39 | + * @since 0.7.5 |
| 40 | + */ |
| 41 | + public function doManipulation( &$value, Parameter $parameter, array &$parameters ) { |
| 42 | + global $egMapsDefaultGeoService, $egMapsUserGeoOverrides; |
| 43 | + static $validatedDefault = false; |
| 44 | + |
| 45 | + if ( !MapsGeocoders::canGeocode() ) { |
| 46 | + throw new Exception( 'There are no geocoders registered, so no geocoding can happen.' ); |
| 47 | + } |
| 48 | + |
| 49 | + // Get rid of any aliases. |
| 50 | + $value = $this->getMainIndentifier( $value ); |
| 51 | + |
| 52 | + // Override the defaulting. |
| 53 | + if ( $parameter->wasSetToDefault() && $this->mappingServiceParam !== false ) { |
| 54 | + $value = self::resolveOverrides( $value, $parameters[$this->mappingServiceParam]->getValue() ); |
| 55 | + } |
| 56 | + |
| 57 | + if ( $value == '' || !array_key_exists( $value, MapsGeocoders::$registeredGeocoders ) ) { |
| 58 | + if ( !$validatedDefault ) { |
| 59 | + if ( !array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) { |
| 60 | + $egMapsDefaultGeoService = array_shift( array_keys( MapsGeocoders::$registeredGeocoders ) ); |
| 61 | + if ( is_null( $egMapsDefaultGeoService ) ) { |
| 62 | + throw new Exception( 'Tried to geocode while there are no geocoders available at ' . __METHOD__ ); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if ( array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) { |
| 68 | + $value = $egMapsDefaultGeoService; |
| 69 | + } |
| 70 | + else { |
| 71 | + throw new Exception( 'Attempt to use the default geocoder while it does not exist.' ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Replaces the geocoder identifier in case there is an override specified by |
| 78 | + * one of the registered geocoders. |
| 79 | + * |
| 80 | + * @since 0.7.5 |
| 81 | + * |
| 82 | + * @param string $geocoderIdentifier |
| 83 | + * @param string $mappingService |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + protected static function resolveOverrides( $geocoderIdentifier, $mappingService ) { |
| 88 | + static $overrides = false; |
| 89 | + |
| 90 | + if ( $overrides === false ) { |
| 91 | + $overrides = array(); |
| 92 | + |
| 93 | + foreach ( MapsGeocoders::$registeredGeocoders as $key => $class ) { |
| 94 | + $overrides[$key] = call_user_func( array( $class, 'getOverrides' ) ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + foreach ( $overrides as $geocoder => $services ) { |
| 99 | + if ( in_array( $mappingService, $services ) ) { |
| 100 | + return $geocoder; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $geocoderIdentifier; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Gets the main geocoder identifier by resolving aliases. |
| 109 | + * |
| 110 | + * @since 0.7.5 |
| 111 | + * |
| 112 | + * @param string $geocoderIdentifier |
| 113 | + * |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + protected function getMainIndentifier( $geocoderIdentifier ) { |
| 117 | + // TODO: implement actual function |
| 118 | + return $geocoderIdentifier; |
| 119 | + } |
| 120 | + |
| 121 | +}<?php |
| 122 | + |
| 123 | +/** |
| 124 | + * Parameter manipulation ensuring the value is a geocoding service, |
| 125 | + * and taking care of both alias resolving and overrides handling. |
| 126 | + * |
| 127 | + * @since 0.7.5 |
| 128 | + * |
| 129 | + * @file Maps_ParamGeoService.php |
| 130 | + * @ingroup Maps |
| 131 | + * @ingroup ParameterManipulations |
| 132 | + * |
| 133 | + * @author Jeroen De Dauw |
| 134 | + */ |
| 135 | +class MapsParamGeoService extends ItemParameterManipulation { |
| 136 | + |
| 137 | + /** |
| 138 | + * @since 0.7.5 |
| 139 | + * |
| 140 | + * @var string or false |
| 141 | + */ |
| 142 | + protected $mappingServiceParam; |
| 143 | + |
| 144 | + /** |
| 145 | + * Constructor. |
| 146 | + * |
| 147 | + * @since 0.7.5 |
| 148 | + */ |
| 149 | + public function __construct( $mappingServiceParam = false ) { |
| 150 | + parent::__construct(); |
| 151 | + |
| 152 | + $this->mappingServiceParam = $mappingServiceParam; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @see ItemParameterManipulation::doManipulation |
| 157 | + * |
| 158 | + * @since 0.7.5 |
| 159 | + */ |
| 160 | + public function doManipulation( &$value, Parameter $parameter, array &$parameters ) { |
| 161 | + global $egMapsDefaultGeoService, $egMapsUserGeoOverrides; |
| 162 | + static $validatedDefault = false; |
| 163 | + |
| 164 | + if ( !MapsGeocoders::canGeocode() ) { |
| 165 | + throw new Exception( 'There are no geocoders registered, so no geocoding can happen.' ); |
| 166 | + } |
| 167 | + |
| 168 | + // Get rid of any aliases. |
| 169 | + $value = $this->getMainIndentifier( $value ); |
| 170 | + |
| 171 | + // Override the defaulting. |
| 172 | + if ( $parameter->wasSetToDefault() && $this->mappingServiceParam !== false ) { |
| 173 | + $value = self::resolveOverrides( $value, $parameters[$this->mappingServiceParam]->getValue() ); |
| 174 | + } |
| 175 | + |
| 176 | + if ( $value == '' || !array_key_exists( $value, MapsGeocoders::$registeredGeocoders ) ) { |
| 177 | + if ( !$validatedDefault ) { |
| 178 | + if ( !array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) { |
| 179 | + $egMapsDefaultGeoService = array_shift( array_keys( MapsGeocoders::$registeredGeocoders ) ); |
| 180 | + if ( is_null( $egMapsDefaultGeoService ) ) { |
| 181 | + throw new Exception( 'Tried to geocode while there are no geocoders available at ' . __METHOD__ ); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if ( array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) { |
| 187 | + $value = $egMapsDefaultGeoService; |
| 188 | + } |
| 189 | + else { |
| 190 | + throw new Exception( 'Attempt to use the default geocoder while it does not exist.' ); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Replaces the geocoder identifier in case there is an override specified by |
| 197 | + * one of the registered geocoders. |
| 198 | + * |
| 199 | + * @since 0.7.5 |
| 200 | + * |
| 201 | + * @param string $geocoderIdentifier |
| 202 | + * @param string $mappingService |
| 203 | + * |
| 204 | + * @return string |
| 205 | + */ |
| 206 | + protected static function resolveOverrides( $geocoderIdentifier, $mappingService ) { |
| 207 | + static $overrides = false; |
| 208 | + |
| 209 | + if ( $overrides === false ) { |
| 210 | + $overrides = array(); |
| 211 | + |
| 212 | + foreach ( MapsGeocoders::$registeredGeocoders as $key => $class ) { |
| 213 | + $overrides[$key] = call_user_func( array( $class, 'getOverrides' ) ); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + foreach ( $overrides as $geocoder => $services ) { |
| 218 | + if ( in_array( $mappingService, $services ) ) { |
| 219 | + return $geocoder; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return $geocoderIdentifier; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Gets the main geocoder identifier by resolving aliases. |
| 228 | + * |
| 229 | + * @since 0.7.5 |
| 230 | + * |
| 231 | + * @param string $geocoderIdentifier |
| 232 | + * |
| 233 | + * @return string |
| 234 | + */ |
| 235 | + protected function getMainIndentifier( $geocoderIdentifier ) { |
| 236 | + // TODO: implement actual function |
| 237 | + return $geocoderIdentifier; |
| 238 | + } |
| 239 | + |
| 240 | +} |
Property changes on: branches/Maps0.8/includes/manipulations/Maps_ParamGeoService.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 241 | + native |
Index: branches/Maps0.8/includes/Maps_Geocoder.php |
— | — | @@ -143,7 +143,7 @@ |
144 | 144 | * |
145 | 145 | * @return array |
146 | 146 | */ |
147 | | - public function getOverrides() { |
| 147 | + public static function getOverrides() { |
148 | 148 | return array(); |
149 | 149 | } |
150 | 150 | |
Index: branches/Maps0.8/includes/geocoders/Maps_GoogleGeocoder.php |
— | — | @@ -9,7 +9,8 @@ |
10 | 10 | * @ingroup Maps |
11 | 11 | * @ingroup Geocoders |
12 | 12 | * |
13 | | - * @author Jeroen De Dauw |
| 13 | + * @licence GNU GPL v3 |
| 14 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 15 | * @author Sergey Chernyshev |
15 | 16 | */ |
16 | 17 | final class MapsGoogleGeocoder extends MapsGeocoder { |
— | — | @@ -74,7 +75,7 @@ |
75 | 76 | * |
76 | 77 | * @return array |
77 | 78 | */ |
78 | | - public function getOverrides() { |
| 79 | + public static function getOverrides() { |
79 | 80 | return array( 'googlemaps2', 'googlemaps3' ); |
80 | 81 | } |
81 | 82 | |
Index: branches/Maps0.8/includes/geocoders/Maps_GeonamesOldGeocoder.php |
— | — | @@ -10,7 +10,9 @@ |
11 | 11 | * @ingroup Maps |
12 | 12 | * @ingroup Geocoders |
13 | 13 | * |
14 | | - * @author Jeroen De Dauw |
| 14 | + * @licence GNU GPL v3 |
| 15 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 16 | + * |
15 | 17 | * Thanks go to Joel Natividad for pointing me to the GeoNames services. |
16 | 18 | */ |
17 | 19 | final class MapsGeonamesOldGeocoder extends MapsGeocoder { |
Index: branches/Maps0.8/includes/geocoders/Maps_GeonamesGeocoder.php |
— | — | @@ -3,14 +3,13 @@ |
4 | 4 | /** |
5 | 5 | * Class for geocoding requests with the GeoNames webservice. |
6 | 6 | * |
7 | | - * GeoNames Web Services Documentation: http://www.geonames.org/export/geonames-search.html |
8 | | - * |
| 7 | + * @since 0.8 |
9 | 8 | * @file Maps_GeonamesGeocoder.php |
10 | 9 | * @ingroup Maps |
11 | 10 | * @ingroup Geocoders |
12 | 11 | * |
13 | | - * @author Jeroen De Dauw |
14 | | - * Thanks go to Joel Natividad for pointing me to the GeoNames services. |
| 12 | + * @licence GNU GPL v3 |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
15 | 14 | */ |
16 | 15 | final class MapsGeonamesGeocoder extends MapsGeocoder { |
17 | 16 | |
— | — | @@ -35,7 +34,7 @@ |
36 | 35 | /** |
37 | 36 | * @see MapsGeocoder::getRequestUrl |
38 | 37 | * |
39 | | - * @since 0.7 |
| 38 | + * @since 0.8 |
40 | 39 | * |
41 | 40 | * @param string $address |
42 | 41 | * |
— | — | @@ -49,7 +48,7 @@ |
50 | 49 | /** |
51 | 50 | * @see MapsGeocoder::parseResponse |
52 | 51 | * |
53 | | - * @since 0.7 |
| 52 | + * @since 0.8 |
54 | 53 | * |
55 | 54 | * @param string $address |
56 | 55 | * |
Index: branches/Maps0.8/includes/geocoders/Maps_YahooGeocoder.php |
— | — | @@ -9,7 +9,8 @@ |
10 | 10 | * @ingroup Maps |
11 | 11 | * @ingroup Geocoders |
12 | 12 | * |
13 | | - * @author Jeroen De Dauw |
| 13 | + * @licence GNU GPL v3 |
| 14 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 15 | */ |
15 | 16 | final class MapsYahooGeocoder extends MapsGeocoder { |
16 | 17 | |
— | — | @@ -69,7 +70,7 @@ |
70 | 71 | * |
71 | 72 | * @return array |
72 | 73 | */ |
73 | | - public function getOverrides() { |
| 74 | + public static function getOverrides() { |
74 | 75 | return array( 'yahoomaps' ); |
75 | 76 | } |
76 | 77 | |