vendor/willdurand/geocoder-bundle/Command/GeocodeCommand.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the BazingaGeocoderBundle package.
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  *
  8.  * @license    MIT License
  9.  */
  10. namespace Bazinga\GeocoderBundle\Command;
  11. use Geocoder\ProviderAggregator;
  12. use Geocoder\Query\GeocodeQuery;
  13. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19.  * @author Markus Bachmann <markus.bachmann@bachi.biz>
  20.  */
  21. class GeocodeCommand extends ContainerAwareCommand
  22. {
  23.     protected static $defaultName 'geocoder:geocode';
  24.     /**
  25.      * @var ProviderAggregator
  26.      */
  27.     private $geocoder;
  28.     /**
  29.      * @param ProviderAggregator $geocoder
  30.      */
  31.     public function __construct(ProviderAggregator $geocoder)
  32.     {
  33.         $this->geocoder $geocoder;
  34.         parent::__construct();
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     protected function configure()
  40.     {
  41.         $this
  42.             ->setName('geocoder:geocode')
  43.             ->setDescription('Geocode an address or a ip address')
  44.             ->addArgument('address'InputArgument::REQUIRED'The address')
  45.             ->addOption('provider'nullInputOption::VALUE_OPTIONAL)
  46.             ->setHelp(<<<'HELP'
  47. The <info>geocoder:geocoder</info> command will fetch the latitude
  48. and longitude from the given address.
  49. You can force a provider with the "provider" option.
  50. <info>php bin/console geocoder:geocoder "Eiffel Tower" --provider=yahoo</info>
  51. HELP
  52.             );
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     protected function execute(InputInterface $inputOutputInterface $output)
  58.     {
  59.         if ($input->getOption('provider')) {
  60.             $this->geocoder->using($input->getOption('provider'));
  61.         }
  62.         $results $this->geocoder->geocodeQuery(GeocodeQuery::create($input->getArgument('address')));
  63.         $data $results->first()->toArray();
  64.         $max 0;
  65.         foreach ($data as $key => $value) {
  66.             $length strlen($key);
  67.             if ($max $length) {
  68.                 $max $length;
  69.             }
  70.         }
  71.         $max += 2;
  72.         foreach ($data as $key => $value) {
  73.             $key $this->humanize($key);
  74.             $output->writeln(sprintf(
  75.                 '<comment>%s</comment>: %s',
  76.                 str_pad($key$max' 'STR_PAD_RIGHT),
  77.                 is_array($value) ? json_encode($value) : $value
  78.             ));
  79.         }
  80.     }
  81.     private function humanize(string $text): string
  82.     {
  83.         $text preg_replace('/([A-Z][a-z]+)|([A-Z][A-Z]+)|([^A-Za-z ]+)/'' \1'$text);
  84.         return ucfirst(strtolower($text));
  85.     }
  86. }