Mercurial > hg > dml-open-vis
comparison src/DML/MainVisBundle/Command/Views/Geography/ExtractCountryCodesCommand.php @ 0:493bcb69166c
added public content
author | Daniel Wolff |
---|---|
date | Tue, 09 Feb 2016 20:54:02 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:493bcb69166c |
---|---|
1 <?php | |
2 namespace DML\MainVisBundle\Command\Views\Geography; | |
3 | |
4 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
5 use Symfony\Component\Console\Input\InputArgument; | |
6 use Symfony\Component\Console\Input\InputInterface; | |
7 use Symfony\Component\Console\Input\InputOption; | |
8 use Symfony\Component\Console\Output\OutputInterface; | |
9 | |
10 class ExtractCountryCodesCommand extends ContainerAwareCommand | |
11 { | |
12 protected $sourceRelativePath = "$/views/geography/parsedPlaces.json"; | |
13 protected $countriesRelativePath = "$/views/geography/countries.json"; | |
14 protected $result1RelativePath = "$/views/geography/placeCountryCodes.json"; | |
15 protected $result2RelativePath = "$/views/geography/placeCountryNumericCodes.json"; | |
16 | |
17 protected function configure() | |
18 { | |
19 $this | |
20 ->setName('dml:views:geography:extract-country-codes') | |
21 ->setDescription('Takes country codes from parsedPlaces.json and writes them to placeCountryCodes.json') | |
22 ; | |
23 } | |
24 | |
25 protected function execute(InputInterface $input, OutputInterface $output) | |
26 { | |
27 $output->writeln(sprintf("Reading from <comment>%s</comment>", $this->sourceRelativePath)); | |
28 | |
29 $sourcePath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->sourceRelativePath; | |
30 $parsedPlaces = json_decode(file_get_contents($sourcePath), true); | |
31 | |
32 | |
33 $output->writeln(sprintf("Reading from <comment>%s</comment>", $this->countriesRelativePath)); | |
34 | |
35 $countriesPath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->countriesRelativePath; | |
36 $countries = json_decode(file_get_contents($countriesPath), true); | |
37 | |
38 | |
39 $output->write(sprintf("Processing...")); | |
40 | |
41 $countryNumericCodesByCountryCodes = array(); | |
42 foreach ($countries as $country) { | |
43 $countryNumericCodesByCountryCodes[$country[0]] = $country[1]; | |
44 } | |
45 | |
46 $countryCodes = array(); | |
47 $countryNumericCodes = array(); | |
48 | |
49 foreach ($parsedPlaces as $palceName => $parsedPlace) { | |
50 if ($parsedPlace && array_key_exists('country_code', $parsedPlace)) { | |
51 $countryCode = strtoupper($parsedPlace['country_code']); | |
52 $countryCodes[$palceName] = $countryCode; | |
53 $countryNumericCodes[$palceName] = $countryNumericCodesByCountryCodes[$countryCode]; | |
54 } | |
55 } | |
56 | |
57 $output->writeln(sprintf(" <comment>%s</comment> -> <comment>%s</comment>", sizeof($parsedPlaces), sizeof($countryCodes))); | |
58 | |
59 | |
60 $output->writeln(sprintf("Writing to <comment>%s</comment>", $this->result1RelativePath)); | |
61 | |
62 $encodedResult = json_encode($countryCodes); | |
63 $encodedResult = str_replace(array('","'), array("\",\n\""), $encodedResult); | |
64 | |
65 $resultPath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->result1RelativePath; | |
66 file_put_contents($resultPath, $encodedResult); | |
67 | |
68 | |
69 $output->writeln(sprintf("Writing to <comment>%s</comment>", $this->result2RelativePath)); | |
70 | |
71 $encodedResult = json_encode($countryNumericCodes); | |
72 $encodedResult = str_replace(array(',"'), array(",\n\""), $encodedResult); | |
73 | |
74 $resultPath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->result2RelativePath; | |
75 file_put_contents($resultPath, $encodedResult); | |
76 | |
77 $output->writeln(sprintf("Done.")); | |
78 } | |
79 } |