Permalink
Browse files

Initial commit

  • Loading branch information...
willdurand committed Nov 7, 2011
0 parents commit a5e2ebe5a13eab1da663681e0901072b8b121d49
@@ -0,0 +1,3 @@
Geocoder
========
@@ -0,0 +1,14 @@
<?php
if (!class_exists('\Symfony\Component\ClassLoader\UniversalClassLoader')) {
require_once __DIR__ . '/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
}
$loader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespaces(array(
'Geocoder' => __DIR__.'/src',
'Geocoder\Tests' => __DIR__.'/tests',
'Symfony\Component' => __DIR__.'/vendor',
'Buzz' => __DIR__.'/../vendor/Buzz/lib',
));
$loader->register();
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Geocoder Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/Geocoder/</directory>
</whitelist>
</filter>
</phpunit>
@@ -0,0 +1,222 @@
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder;
use Geocoder\Provider\ProviderInterface;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class Geocoder implements GeocoderInterface
{
/**
* @var double
*/
protected $latitude;
/**
* @var double
*/
protected $longitude;
/**
* @var string
*/
protected $city;
/**
* @var string
*/
protected $zipcode;
/**
* @var string
*/
protected $region;
/**
* @var string
*/
protected $country;
/**
* @var array
*/
private $providers = array();
/**
* @var \Geocoder\Provider\ProviderInterface
*/
private $provider;
/**
* @param \Geocoder\Provider\ProviderInterface $provider
*/
public function __construct(ProviderInterface $provider = null)
{
$this->provider = $provider;
}
/**
* {@inheritDoc}
*/
public function geocode($value)
{
$data = $this->getProvider()->getData($value);
$this->extractData($data);
}
/**
* {@inheritDoc}
*/
public function getCoordinates()
{
return array($this->getLatitude(), $this->getLongitude());
}
/**
* {@inheritDoc}
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* {@inheritDoc}
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* {@inheritDoc}
*/
public function getCity()
{
return $this->city;
}
/**
* {@inheritDoc}
*/
public function getZipcode()
{
return $this->zipcode;
}
/**
* {@inheritDoc}
*/
public function getRegion()
{
return $this->region;
}
/**
* {@inheritDoc}
*/
public function getCountry()
{
return $this->country;
}
/**
* Registers a provider.
*
* @param \Geocoder\Provider\ProviderInterface $provider
* @return \Geocoder\AbstractGeocoder
*/
public function registerProvider(ProviderInterface $provider)
{
if (null !== $provider) {
$this->providers[$provider->getName()] = $provider;
}
return $this;
}
/**
* Registers a set of providers..
*
* @param array $providers
* @return \Geocoder\AbstractGeocoder
*/
public function registerProviders(array $providers = array())
{
foreach ($providers as $provider) {
$this->registerProvider($provider);
}
return $this;
}
/**
* Sets the provider to use.
*
* @param string $name A provider's name
* @return \Geocoder\AbstractGeocoder
*/
public function using($name)
{
if (isset($this->providers[$name])) {
$this->provider = $this->providers[$name];
}
return $this;
}
/**
* Returns the provider to use.
*
* @return \Geocoder\Provider\ProviderInterface
*/
protected function getProvider()
{
if (null === $this->provider) {
if (0 === count($this->providers)) {
throw new \RuntimeException('No provider registered.');
} else {
$this->provider = $this->providers[key($this->providers)];
}
}
return $this->provider;
}
/**
* Extracts data from an array.
*
* @param array $data An array.
*/
protected function extractData(array $data = array())
{
if (isset($data['latitude'])) {
$this->latitude = $data['latitude'];
}
if (isset($data['longitude'])) {
$this->longitude = $data['longitude'];
}
if (isset($data['city'])) {
$this->city = $data['city'];
}
if (isset($data['zipcode'])) {
$this->zipcode = $data['zipcode'];
}
if (isset($data['region'])) {
$this->region = $data['region'];
}
if (isset($data['country'])) {
$this->country = $data['country'];
}
}
}
@@ -0,0 +1,73 @@
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder;
/**
* @author William Durand <william.durand1@gmail.com>
*/
interface GeocoderInterface
{
/**
* Geocode a given value.
*
* @param string $value A value to geocode.
*/
function geocode($value);
/**
* Returns an array of coordinates (latitude, longitude).
*
* @return array
*/
function getCoordinates();
/**
* Returns the latitude value.
*
* @return double
*/
function getLatitude();
/**
* Returns the longitude value.
*
* @return double
*/
function getLongitude();
/**
* Returns the city value.
*
* @return string
*/
function getCity();
/**
* Returns the zipcode value.
*
* @return string
*/
function getZipcode();
/**
* Returns the region value.
*
* @return string
*/
function getRegion();
/**
* Returns the country value.
*
* @return string
*/
function getCountry();
}
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\HttpAdapter;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class BuzzHttpAdapter implements HttpAdapterInterface
{
/**
* {@inheritDoc}
*/
public function getContent($url)
{
$browser = new \Buzz\Browser();
$response = $browser->get($url);
return $response->getContent();
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'buzz';
}
}
Oops, something went wrong.

0 comments on commit a5e2ebe

Please sign in to comment.