https://www.packtpub.com/packtlib/bo...sec08/Services
I can not understand how is implemented line:
Expand|Select|Wrap|Line Numbers
- $boundaries = $this->get('user_locator')->getUserGeoBoundaries();
C:\Bitnami\wampstack-5.5.30-0\sym_prog\ExtSym2\src\Khepin\BookBundle\Controlle r\DefaultController.php
Expand|Select|Wrap|Line Numbers
- <?php
- namespace Khepin\BookBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- use Khepin\BookBundle\Form\JoinEventType;
- use Khepin\BookBundle\Event\MeetupEvent;
- use Khepin\BookBundle\Event\MeetupEvents;
- use Geocoder\HttpAdapter\CurlHttpAdapter;
- use Geocoder\Geocoder;
- use Geocoder\Provider\FreeGeoIpProvider;
- class DefaultController extends Controller
- {
- /**
- * @Route("/")
- * @Template()
- */
- public function indexAction()
- {
- $boundaries = $this->get('user_locator')->getUserGeoBoundaries();
- // Create our database query
- $em = $this->getDoctrine()->getManager();
- $qb = $em->createQueryBuilder();
- $qb->select('e')
- ->from('KhepinBookBundle:Event', 'e')
- ->where('e.latitude < :lat_max')
- ->andWhere('e.latitude > :lat_min')
- ->andWhere('e.longitude < :long_max')
- ->andWhere('e.longitude > :long_min')
- ->setParameters($boundaries);
- // Retrieve interesting events
- $events = $qb->getQuery()->execute();
- return compact('events');
- }
- ..
- }
C:\Bitnami\wampstack-5.5.30-0\sym_prog\ExtSym2\vendor\willdurand\geocoder\src\ Geocoder\Geocoder.php
Expand|Select|Wrap|Line Numbers
- <?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;
- use Geocoder\Result\ResultFactoryInterface;
- use Geocoder\Result\DefaultResultFactory;
- /**
- * @author William Durand <william.durand1@gmail.com>
- */
- class Geocoder implements GeocoderInterface
- {
- /**
- * Version
- */
- const VERSION = '2.3.2';
- /**
- * @var integer
- */
- const MAX_RESULTS = 5;
- /**
- * @var ProviderInterface[]
- */
- private $providers = array();
- /**
- * @var ProviderInterface
- */
- private $provider;
- /**
- * @var ResultFactoryInterface
- */
- private $resultFactory;
- /**
- * @var integer
- */
- private $maxResults;
- /**
- * @param ProviderInterface $provider
- * @param ResultFactoryInterface $resultFactory
- * @param integer $maxResults
- */
- public function __construct(ProviderInterface $provider = null, ResultFactoryInterface $resultFactory = null, $maxResults = self::MAX_RESULTS)
- {
- $this->provider = $provider;
- $this->setResultFactory($resultFactory);
- $this->limit($maxResults);
- }
- /**
- * @param ResultFactoryInterface $resultFactory
- */
- public function setResultFactory(ResultFactoryInterface $resultFactory = null)
- {
- $this->resultFactory = $resultFactory ?: new DefaultResultFactory();
- }
- /**
- * @param integer $maxResults
- *
- * @return GeocoderInterface
- */
- public function limit($maxResults)
- {
- $this->maxResults = $maxResults;
- return $this;
- }
- /**
- * @return integer $maxResults
- */
- public function getMaxResults()
- {
- return $this->maxResults;
- }
- /**
- * {@inheritDoc}
- */
- public function geocode($value)
- {
- if (empty($value)) {
- // let's save a request
- return $this->returnResult(array());
- }
- $provider = $this->getProvider()->setMaxResults($this->getMaxResults());
- $data = $provider->getGeocodedData(trim($value));
- $result = $this->returnResult($data);
- return $result;
- }
- /**
- * {@inheritDoc}
- */
- public function reverse($latitude, $longitude)
- {
- if (empty($latitude) || empty($longitude)) {
- // let's save a request
- return $this->returnResult(array());
- }
- $provider = $this->getProvider()->setMaxResults($this->getMaxResults());
- $data = $provider->getReversedData(array($latitude, $longitude));
- $result = $this->returnResult($data);
- return $result;
- }
- /**
- * Registers a provider.
- *
- * @param ProviderInterface $provider
- *
- * @return GeocoderInterface
- */
- public function registerProvider(ProviderInterface $provider)
- {
- if (null !== $provider) {
- $this->providers[$provider->getName()] = $provider;
- }
- return $this;
- }
- /**
- * Registers a set of providers.
- *
- * @param ProviderInterface[] $providers
- *
- * @return GeocoderInterface
- */
- 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 GeocoderInterface
- */
- public function using($name)
- {
- if (isset($this->providers[$name])) {
- $this->provider = $this->providers[$name];
- }
- return $this;
- }
- /**
- * Returns registered providers indexed by name.
- *
- * @return ProviderInterface[]
- */
- public function getProviders()
- {
- return $this->providers;
- }
- /**
- * Returns the provider to use.
- *
- * @return 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;
- }
- /**
- * @param array $data An array of data.
- *
- * @return ResultInterface
- */
- protected function returnResult(array $data = array())
- {
- return $this->resultFactory->createFromArray($data);
- }
- }
- C:\Bitnami\wampstack-5.5.30-0\sym_prog\ExtSym2\vendor\willdurand\geocoder\src\Geocoder\Provider\FreeGeoIpProvider.php
- <?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\Provider;
- use Geocoder\Exception\NoResultException;
- use Geocoder\Exception\UnsupportedException;
- /**
- * @author William Durand <william.durand1@gmail.com>
- */
- class FreeGeoIpProvider extends AbstractProvider implements ProviderInterface
- {
- /**
- * @var string
- */
- const ENDPOINT_URL = 'http://freegeoip.net/json/%s';
- /**
- * {@inheritDoc}
- */
- public function getGeocodedData($address)
- {
- if (!filter_var($address, FILTER_VALIDATE_IP)) {
- throw new UnsupportedException('The FreeGeoIpProvider does not support Street addresses.');
- }
- if (in_array($address, array('127.0.0.1', '::1'))) {
- return array($this->getLocalhostDefaults());
- }
- $query = sprintf(self::ENDPOINT_URL, $address);
- return $this->executeQuery($query);
- }
- /**
- * {@inheritDoc}
- */
- public function getReversedData(array $coordinates)
- {
- throw new UnsupportedException('The FreeGeoIpProvider is not able to do reverse geocoding.');
- }
- /**
- * {@inheritDoc}
- */
- public function getName()
- {
- return 'free_geo_ip';
- }
- /**
- * @param string $query
- *
- * @return array
- */
- protected function executeQuery($query)
- {
- $content = $this->getAdapter()->getContent($query);
- if (null === $content) {
- throw new NoResultException(sprintf('Could not execute query %s', $query));
- }
- $data = (array) json_decode($content);
- if (empty($data)) {
- throw new NoResultException(sprintf('Could not execute query %s', $query));
- }
- //it appears that for US states the region code is not returning the FIPS standard
- if ('US' === $data['country_code'] && isset($data['region_code']) && !is_numeric($data['region_code'])) {
- $newRegionCode = $this->stateToRegionCode($data['region_code']);
- $data['region_code'] = is_numeric($newRegionCode) ? $newRegionCode : null;
- }
- return array(array_merge($this->getDefaults(), array(
- 'latitude' => isset($data['latitude']) ? $data['latitude'] : null,
- 'longitude' => isset($data['longitude']) ? $data['longitude'] : null,
- 'city' => isset($data['city']) ? $data['city'] : null,
- 'zipcode' => isset($data['zipcode']) ? $data['zipcode'] : null,
- 'region' => isset($data['region_name']) ? $data['region_name'] : null,
- 'regionCode' => isset($data['region_code']) ? $data['region_code'] : null,
- 'country' => isset($data['country_name']) ? $data['country_name'] : null,
- 'countryCode' => isset($data['country_code']) ? $data['country_code'] : null,
- )));
- }
- /**
- * Converts the state code to FIPS standard
- *
- * @param string $state
- *
- * @return string|integer The FIPS code or the state code if not found
- */
- private function stateToRegionCode($state)
- {
- $codes = $this->getRegionCodes();
- return array_key_exists($state, $codes) ? $codes[$state] : $state;
- }
- /**
- * Returns an array of state codes => FIPS codes
- * @see http://www.epa.gov/enviro/html/codes/state.html
- *
- * @return array
- */
- private function getRegionCodes()
- {
- return array(
- 'AK' => 2, //ALASKA
- 'AL' => 1, //ALABAMA
- 'AR' => 5, //ARKANSAS
- 'AS' => 60, //AMERICAN SAMOA
- 'AZ' => 4, //ARIZONA
- 'CA' => 6, //CALIFORNIA
- 'CO' => 8, //COLORADO
- 'CT' => 9, //CONNECTICUT
- 'DC' => 11, //DISTRICT OF COLUMBIA
- 'DE' => 10, //DELAWARE
- 'FL' => 12, //FLORIDA
- 'GA' => 13, //GEORGIA
- 'GU' => 66, //GUAM
- 'HI' => 15, //HAWAII
- 'IA' => 19, //IOWA
- 'ID' => 16, //IDAHO
- 'IL' => 17, //ILLINOIS
- 'IN' => 18, //INDIANA
- 'KS' => 20, //KANSAS
- 'KY' => 21, //KENTUCKY
- 'LA' => 22, //LOUISIANA
- 'MA' => 25, //MASSACHUSETTS
- 'MD' => 24, //MARYLAND
- 'ME' => 23, //MAINE
- 'MI' => 26, //MICHIGAN
- 'MN' => 27, //MINNESOTA
- 'MO' => 29, //MISSOURI
- 'MS' => 28, //MISSISSIPPI
- 'MT' => 30, //MONTANA
- 'NC' => 37, //NORTH CAROLINA
- 'ND' => 38, //NORTH DAKOTA
- 'NE' => 31, //NEBRASKA
- 'NH' => 33, //NEW HAMPSHIRE
- 'NJ' => 34, //NEW JERSEY
- 'NM' => 35, //NEW MEXICO
- 'NV' => 32, //NEVADA
- 'NY' => 36, //NEW YORK
- 'OH' => 39, //OHIO
- 'OK' => 40, //OKLAHOMA
- 'OR' => 41, //OREGON
- 'PA' => 42, //PENNSYLVANIA
- 'PR' => 72, //PUERTO RICO
- 'RI' => 44, //RHODE ISLAND
- 'SC' => 45, //SOUTH CAROLINA
- 'SD' => 46, //SOUTH DAKOTA
- 'TN' => 47, //TENNESSEE
- 'TX' => 48, //TEXAS
- 'UT' => 49, //UTAH
- 'VA' => 51, //VIRGINIA
- 'VI' => 78, //VIRGIN ISLANDS
- 'VT' => 50, //VERMONT
- 'WA' => 53, //WASHINGTON
- 'WI' => 55, //WISCONSIN
- 'WV' => 54, //WEST VIRGINIA
- );
- }
- }