/** * Binds geocoding functionality to the model specified. * * @param string $class A fully-namespaced class reference to a model class that `Geocoder` * should bind to. Assigns configuration for the model, and attaches custom * behavior to perform geocode lookups and location-based searches. * @param array $config An array of configuration settings defining how `Geocoder` should * behave with this model. The available settings are as follows: * - `'service'` _string_: The name of the API service to use when geocoding * addresses. You can configure custom lookup services, but by default, the * available options are `'google'` or `'yahoo'`. Attempting to use an undefined * service will result in an exception. * */ public static function bind($class, array $config = array()) { $defaults = array('service' => 'google', 'autoIndex' => true, 'fields' => array('latitude', 'longitude'), 'format' => '{:address} {:city}, {:state} {:zip}'); $config += $defaults; $geocoder = static::$_classes['geocoder']; if (!$geocoder::services($config['service'])) { $message = "The lookup service `{$config['service']}` does not exist."; throw new UnexpectedValueException($message); } if ($index = $config['autoIndex']) { static::index($class, $config['fields'], is_array($index) ? $index : array()); } $finder = function ($self, $params, $chain) use($class) { $params['options'] = Locatable::invokeMethod('_formatParameters', array($class, $params['type'], $params['options'])); return $chain->next($self, $params, $chain); }; $class::finder('near', $finder); $class::finder('within', $finder); $class::applyFilter('find', $finder); return static::$_configurations[$class] = $config; }
public function testShortHandFormats() { $model = 'li3_geo\\tests\\mocks\\MockLocatableModel'; $coords = array(84.13, 11.38); $coords2 = array_map(function ($point) { return $point + 5; }, $coords); Locatable::bind($model, array('autoIndex' => false, 'fields' => array('location.latitude', 'location.longitude'))); $result = $model::find('near', $coords); $conditions = array('location' => array('$near' => $coords)); $this->assertEqual(compact('conditions'), $result['options']); $result = $model::find('within', array($coords, $coords2)); $conditions = array('location' => array('$within' => array('$box' => array($coords2, $coords)))); $this->assertEqual(compact('conditions'), $result['options']); }