-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,940 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
Resources/data/zones/*.json | ||
!Resources/data/zones/de.json | ||
!Resources/data/zones/en.json | ||
!Resources/data/zones/fr.json | ||
!Resources/data/zones/meta.json | ||
!Resources/data/zones/nl.json | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
173 changes: 173 additions & 0 deletions
173
src/Symfony/Component/Intl/Data/Generator/ZoneDataGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Intl\Data\Generator; | ||
|
||
use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler; | ||
use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface; | ||
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle; | ||
use Symfony\Component\Intl\Data\Util\LocaleScanner; | ||
|
||
/** | ||
* The rule for compiling the zone bundle. | ||
* | ||
* @author Roland Franssen <franssen.roland@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class ZoneDataGenerator extends AbstractDataGenerator | ||
{ | ||
private const UNKNOWN_ZONE = 'Etc:Unknown'; | ||
|
||
/** | ||
* Collects all available zone codes. | ||
* | ||
* @var string[] | ||
*/ | ||
private $zoneCodes = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function scanLocales(LocaleScanner $scanner, $sourceDir) | ||
{ | ||
return $scanner->scanLocales($sourceDir.'/zone'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir) | ||
{ | ||
$compiler->compile($sourceDir.'/zone', $tempDir); | ||
$compiler->compile($sourceDir.'/misc/timezoneTypes.txt', $tempDir); | ||
$compiler->compile($sourceDir.'/misc/metaZones.txt', $tempDir); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function preGenerate() | ||
{ | ||
$this->zoneCodes = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale) | ||
{ | ||
$localeBundle = $reader->read($tempDir, $displayLocale); | ||
|
||
if (isset($localeBundle['zoneStrings']) && null !== $localeBundle['zoneStrings']) { | ||
$data = array( | ||
'Version' => $localeBundle['Version'], | ||
'Names' => self::generateZones( | ||
$reader->read($tempDir, 'timezoneTypes'), | ||
$reader->read($tempDir, 'metaZones'), | ||
$localeBundle | ||
), | ||
); | ||
|
||
$this->zoneCodes = array_merge($this->zoneCodes, array_keys($data['Names'])); | ||
|
||
return $data; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir) | ||
{ | ||
$rootBundle = $reader->read($tempDir, 'root'); | ||
|
||
return array( | ||
'Version' => $rootBundle['Version'], | ||
'Names' => self::generateZones( | ||
$reader->read($tempDir, 'timezoneTypes'), | ||
$reader->read($tempDir, 'metaZones'), | ||
$rootBundle | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir) | ||
{ | ||
$rootBundle = $reader->read($tempDir, 'root'); | ||
|
||
$this->zoneCodes = array_unique($this->zoneCodes); | ||
|
||
sort($this->zoneCodes); | ||
|
||
$data = array( | ||
'Version' => $rootBundle['Version'], | ||
'Zones' => $this->zoneCodes, | ||
); | ||
|
||
return $data; | ||
} | ||
|
||
private static function generateZones(ArrayAccessibleResourceBundle $typeBundle, ArrayAccessibleResourceBundle $metaBundle, ArrayAccessibleResourceBundle $localeBundle): array | ||
{ | ||
$available = array(); | ||
foreach ($typeBundle['typeMap']['timezone'] as $zone => $_) { | ||
if (self::UNKNOWN_ZONE === $zone) { | ||
continue; | ||
} | ||
|
||
$available[self::normalizeZone($zone)] = true; | ||
} | ||
|
||
$metazones = array(); | ||
foreach ($metaBundle['metazoneInfo'] as $zone => $info) { | ||
$zone = self::normalizeZone($zone); | ||
foreach ($info as $metazone) { | ||
$metazones[$zone] = $metazone->get(0); | ||
} | ||
} | ||
|
||
$zones = array(); | ||
|
||
foreach ($localeBundle['zoneStrings'] as $zone => $data) { | ||
if (!isset($available[$zone = self::normalizeZone($zone)])) { | ||
continue; | ||
} | ||
|
||
$name = null; | ||
if (isset($metazones[$zone]) && isset($localeBundle['zoneStrings']['meta:'.$metazones[$zone]])) { | ||
$metadata = $localeBundle['zoneStrings']['meta:'.$metazones[$zone]]; | ||
$name = $metadata->get('ls') ?? $metadata->get('lg') ?? $metadata->get('ld'); | ||
} | ||
|
||
$name = $data->get('ls') ?? $data->get('lg') ?? $data->get('ld') ?? $name; | ||
if (null !== $city = $data->get('ec')) { | ||
if (null === $name) { | ||
$name = str_replace('{0}', $city, $localeBundle['zoneStrings']['regionFormat']); | ||
} else { | ||
$name .= ' ('.$city.')'; | ||
} | ||
} | ||
|
||
$zones[$zone] = $name ?? $metazones[$zone] ?? $zone; | ||
} | ||
|
||
return $zones; | ||
} | ||
|
||
private static function normalizeZone(string $zone): string | ||
{ | ||
return str_replace(':', '/', $zone); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.