1
|
|
|
<?php |
2
|
|
|
|
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
|
|
|
|
11
|
|
|
namespace Bazinga\GeocoderBundle\Command; |
12
|
|
|
|
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
|
|
|
/** |
20
|
|
|
* @author Markus Bachmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class GeocodeCommand extends ContainerAwareCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
|
|
->setName('geocoder:geocode') |
31
|
|
|
->setDescription('Geocode an address or a ip address') |
32
|
|
|
->addArgument('address', InputArgument::REQUIRED, 'The address') |
33
|
|
|
->addOption('provider', null, InputOption::VALUE_OPTIONAL) |
34
|
|
|
->setHelp(<<<'HELP' |
35
|
|
|
The <info>geocoder:geocoder</info> command will fetch the latitude |
36
|
|
|
and longitude from the given address. |
37
|
|
|
|
38
|
|
|
You can force a provider with the "provider" option. |
39
|
|
|
|
40
|
|
|
<info>php app/console geocoder:geocoder "Eiffel Tower" --provider=yahoo</info> |
41
|
|
|
HELP |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
/** @var $geocoder \Geocoder\Geocoder */ |
51
|
|
|
$geocoder = $this->getContainer()->get('bazinga_geocoder.geocoder'); |
52
|
|
|
|
53
|
|
|
if ($input->getOption('provider')) { |
54
|
|
|
$geocoder->using($input->getOption('provider')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$results = $geocoder->geocode($input->getArgument('address')); |
58
|
|
|
$data = $results->first()->toArray(); |
59
|
|
|
|
60
|
|
|
$max = 0; |
61
|
|
|
|
62
|
|
|
foreach ($data as $key => $value) { |
63
|
|
|
$length = strlen($key); |
64
|
|
|
if ($max < $length) { |
65
|
|
|
$max = $length; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$max += 2; |
70
|
|
|
|
71
|
|
|
foreach ($data as $key => $value) { |
72
|
|
|
$key = $this->humanize($key); |
73
|
|
|
|
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
|
|
|
|
82
|
|
|
private function humanize($text) |
83
|
|
|
{ |
84
|
|
|
$text = preg_replace('/([A-Z][a-z]+)|([A-Z][A-Z]+)|([^A-Za-z ]+)/', ' \1', $text); |
85
|
|
|
|
86
|
|
|
return ucfirst(strtolower($text)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|