annotate src/DML/MainVisBundle/Command/Views/Geography/ExtractCountryCodesCommand.php @ 1:f38015048f48 tip

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