/** * @param ProgressAdapter|null $progressAdapter */ public function updateAddressFormats(ProgressAdapter $progressAdapter = null) { $localeDataUri = $this->options->getLocaleDataUri(); $dataPath = $this->options->getDataPath(); $locales = json_decode($this->httpClient->setUri($localeDataUri)->send()->getContent()); foreach (scandir($dataPath) as $file) { if (fnmatch('*.json', $file)) { unlink($dataPath . '/' . $file); } } $countryCodes = isset($locales->countries) ? explode('~', $locales->countries) : []; $countryCodes[] = 'ZZ'; if ($progressAdapter !== null) { $progressBar = new ProgressBar($progressAdapter, 0, count($countryCodes)); } foreach ($countryCodes as $countryCode) { file_put_contents($dataPath . '/' . $countryCode . '.json', $this->httpClient->setUri($localeDataUri . '/' . $countryCode)->send()->getContent()); if (isset($progressBar)) { $progressBar->next(); } } if (isset($progressBar)) { $progressBar->finish(); } // We clearly don't want the "ZZ" in the array! array_pop($countryCodes); $writer = new PhpArrayWriter(); $writer->setUseBracketArraySyntax(true); $writer->toFile($this->options->getCountryCodesPath(), $countryCodes, false); }
/** * @param string $countryCode * @param string $key * @return string|null */ protected function getJsonValue($countryCode, $key) { if (!in_array($countryCode, $this->getCountryCodes()) && $countryCode !== 'ZZ') { return null; } $filename = $this->options->getDataPath() . '/' . $countryCode . '.json'; if (!file_exists($filename)) { return null; } $data = json_decode(file_get_contents($filename), true); if (!isset($data[$key])) { return null; } return $data[$key]; }
/** * @covers ::setCountryCodesPath * @covers ::getCountryCodesPath */ public function testGetSetCountryCodesPath() { $options = new Options(); $options->setCountryCodesPath('foo'); $this->assertSame('foo', $options->getCountryCodesPath()); }