/** * @param $field * @param TextStyle $style * @param Locale $locale * @param callable $transformer * @return array|null */ public static function tryFetchStyleValues($field, $style, Locale $locale, callable $transformer) { $bundle = new ResourceBundle($locale->getLocale(), null); if ($style === null) { $tmp = []; foreach (['stand-alone', 'format'] as $id) { foreach (['wide', 'abbreviated', 'narrow'] as $style) { $values = $bundle['calendar']['gregorian'][$field][$id][$style]; if ($values === null) { continue; } foreach ($values as $key => $value) { $tmp[$value] = $transformer($key); } } } return $tmp; } $id = $style->isStandalone() ? 'stand-alone' : 'format'; $styles = [\IntlDateFormatter::FULL => 'wide', \IntlDateFormatter::MEDIUM => 'abbreviated', \IntlDateFormatter::SHORT => 'narrow']; $values = $bundle['calendar']['gregorian'][$field][$id][$styles[$style->toCalendarStyle()]]; // fallback to stand alone if not found if ($values === null && $id === 'format') { $values = $bundle['calendar']['gregorian'][$field]['stand-alone'][$styles[$style->toCalendarStyle()]]; } // ERA if ($values === null) { $values = $bundle['calendar']['gregorian'][$field][$styles[$style->toCalendarStyle()]]; } // AMPM if ($values === null) { $values = $bundle['calendar']['gregorian'][$field]; } if (!$values) { return null; } $tmp = []; foreach ($values as $key => $value) { $tmp[$value] = $transformer($key); } return $tmp; }