|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Intl\Data\Generator; |
| 13 | + |
| 14 | +use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler; |
| 15 | +use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface; |
| 16 | +use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle; |
| 17 | +use Symfony\Component\Intl\Data\Util\LocaleScanner; |
| 18 | + |
| 19 | +/** |
| 20 | + * The rule for compiling the zone bundle. |
| 21 | + * |
| 22 | + * @author Roland Franssen <franssen.roland@gmail.com> |
| 23 | + * |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +class TimezoneDataGenerator extends AbstractDataGenerator |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Collects all available zone codes. |
| 30 | + * |
| 31 | + * @var string[] |
| 32 | + */ |
| 33 | + private $zoneCodes = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + protected function scanLocales(LocaleScanner $scanner, $sourceDir) |
| 39 | + { |
| 40 | + return $scanner->scanLocales($sourceDir.'/zone'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir) |
| 47 | + { |
| 48 | + $compiler->compile($sourceDir.'/zone', $tempDir); |
| 49 | + $compiler->compile($sourceDir.'/misc/timezoneTypes.txt', $tempDir); |
| 50 | + $compiler->compile($sourceDir.'/misc/metaZones.txt', $tempDir); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + protected function preGenerate() |
| 57 | + { |
| 58 | + $this->zoneCodes = []; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + */ |
| 64 | + protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale) |
| 65 | + { |
| 66 | + $localeBundle = $reader->read($tempDir, $displayLocale); |
| 67 | + |
| 68 | + if (isset($localeBundle['zoneStrings']) && null !== $localeBundle['zoneStrings']) { |
| 69 | + $data = [ |
| 70 | + 'Version' => $localeBundle['Version'], |
| 71 | + 'Names' => self::generateZones( |
| 72 | + $reader->read($tempDir, 'timezoneTypes'), |
| 73 | + $reader->read($tempDir, 'metaZones'), |
| 74 | + $reader->read($tempDir, 'root'), |
| 75 | + $localeBundle |
| 76 | + ), |
| 77 | + ]; |
| 78 | + |
| 79 | + $this->zoneCodes = array_merge($this->zoneCodes, array_keys($data['Names'])); |
| 80 | + |
| 81 | + return $data; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritdoc} |
| 87 | + */ |
| 88 | + protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir) |
| 89 | + { |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + */ |
| 95 | + protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir) |
| 96 | + { |
| 97 | + $rootBundle = $reader->read($tempDir, 'root'); |
| 98 | + |
| 99 | + $this->zoneCodes = array_unique($this->zoneCodes); |
| 100 | + |
| 101 | + sort($this->zoneCodes); |
| 102 | + |
| 103 | + $data = [ |
| 104 | + 'Version' => $rootBundle['Version'], |
| 105 | + 'Zones' => $this->zoneCodes, |
| 106 | + ]; |
| 107 | + |
| 108 | + return $data; |
| 109 | + } |
| 110 | + |
| 111 | + private static function generateZones(ArrayAccessibleResourceBundle $typeBundle, ArrayAccessibleResourceBundle $metaBundle, ArrayAccessibleResourceBundle $rootBundle, ArrayAccessibleResourceBundle $localeBundle): array |
| 112 | + { |
| 113 | + $available = []; |
| 114 | + foreach ($typeBundle['typeMap']['timezone'] as $zone => $_) { |
| 115 | + if ('Etc:Unknown' === $zone || preg_match('~^Etc:GMT[-+]\d+$~', $zone)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + $available[$zone] = true; |
| 120 | + } |
| 121 | + |
| 122 | + $metazones = []; |
| 123 | + foreach ($metaBundle['metazoneInfo'] as $zone => $info) { |
| 124 | + foreach ($info as $metazone) { |
| 125 | + $metazones[$zone] = $metazone->get(0); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + $zones = []; |
| 130 | + foreach (array_keys($available) as $zone) { |
| 131 | + // lg: long generic, e.g. "Central European Time" |
| 132 | + // ls: long specific (not DST), e.g. "Central European Standard Time" |
| 133 | + // ld: long DST, e.g. "Central European Summer Time" |
| 134 | + // ec: example city, e.g. "Amsterdam" |
| 135 | + $name = $localeBundle['zoneStrings'][$zone]['lg'] ?? $rootBundle['zoneStrings'][$zone]['lg'] ?? $localeBundle['zoneStrings'][$zone]['ls'] ?? $rootBundle['zoneStrings'][$zone]['ls'] ?? null; |
| 136 | + $city = $localeBundle['zoneStrings'][$zone]['ec'] ?? $rootBundle['zoneStrings'][$zone]['ec'] ?? null; |
| 137 | + |
| 138 | + if (null === $name && isset($metazones[$zone])) { |
| 139 | + $meta = 'meta:'.$metazones[$zone]; |
| 140 | + $name = $localeBundle['zoneStrings'][$meta]['lg'] ?? $rootBundle['zoneStrings'][$meta]['lg'] ?? $localeBundle['zoneStrings'][$meta]['ls'] ?? $rootBundle['zoneStrings'][$meta]['ls'] ?? null; |
| 141 | + } |
| 142 | + if (null === $city && 0 !== strrpos($zone, 'Etc:') && false !== $i = strrpos($zone, ':')) { |
| 143 | + $city = str_replace('_', ' ', substr($zone, $i + 1)); |
| 144 | + } |
| 145 | + if (null === $name) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + if (null !== $city) { |
| 149 | + $name .= ' ('.$city.')'; |
| 150 | + } |
| 151 | + |
| 152 | + $id = str_replace(':', '/', $zone); |
| 153 | + $zones[$id] = $name; |
| 154 | + } |
| 155 | + |
| 156 | + return $zones; |
| 157 | + } |
| 158 | +} |
0 commit comments