Daniel@0: setName('dml:views:geography:extract-country-codes')
Daniel@0: ->setDescription('Takes country codes from parsedPlaces.json and writes them to placeCountryCodes.json')
Daniel@0: ;
Daniel@0: }
Daniel@0:
Daniel@0: protected function execute(InputInterface $input, OutputInterface $output)
Daniel@0: {
Daniel@0: $output->writeln(sprintf("Reading from %s", $this->sourceRelativePath));
Daniel@0:
Daniel@0: $sourcePath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->sourceRelativePath;
Daniel@0: $parsedPlaces = json_decode(file_get_contents($sourcePath), true);
Daniel@0:
Daniel@0:
Daniel@0: $output->writeln(sprintf("Reading from %s", $this->countriesRelativePath));
Daniel@0:
Daniel@0: $countriesPath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->countriesRelativePath;
Daniel@0: $countries = json_decode(file_get_contents($countriesPath), true);
Daniel@0:
Daniel@0:
Daniel@0: $output->write(sprintf("Processing..."));
Daniel@0:
Daniel@0: $countryNumericCodesByCountryCodes = array();
Daniel@0: foreach ($countries as $country) {
Daniel@0: $countryNumericCodesByCountryCodes[$country[0]] = $country[1];
Daniel@0: }
Daniel@0:
Daniel@0: $countryCodes = array();
Daniel@0: $countryNumericCodes = array();
Daniel@0:
Daniel@0: foreach ($parsedPlaces as $palceName => $parsedPlace) {
Daniel@0: if ($parsedPlace && array_key_exists('country_code', $parsedPlace)) {
Daniel@0: $countryCode = strtoupper($parsedPlace['country_code']);
Daniel@0: $countryCodes[$palceName] = $countryCode;
Daniel@0: $countryNumericCodes[$palceName] = $countryNumericCodesByCountryCodes[$countryCode];
Daniel@0: }
Daniel@0: }
Daniel@0:
Daniel@0: $output->writeln(sprintf(" %s -> %s", sizeof($parsedPlaces), sizeof($countryCodes)));
Daniel@0:
Daniel@0:
Daniel@0: $output->writeln(sprintf("Writing to %s", $this->result1RelativePath));
Daniel@0:
Daniel@0: $encodedResult = json_encode($countryCodes);
Daniel@0: $encodedResult = str_replace(array('","'), array("\",\n\""), $encodedResult);
Daniel@0:
Daniel@0: $resultPath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->result1RelativePath;
Daniel@0: file_put_contents($resultPath, $encodedResult);
Daniel@0:
Daniel@0:
Daniel@0: $output->writeln(sprintf("Writing to %s", $this->result2RelativePath));
Daniel@0:
Daniel@0: $encodedResult = json_encode($countryNumericCodes);
Daniel@0: $encodedResult = str_replace(array(',"'), array(",\n\""), $encodedResult);
Daniel@0:
Daniel@0: $resultPath = $this->getContainer()->getParameter("kernel.root_dir") . '/../' . $this->result2RelativePath;
Daniel@0: file_put_contents($resultPath, $encodedResult);
Daniel@0:
Daniel@0: $output->writeln(sprintf("Done."));
Daniel@0: }
Daniel@0: }