$legendValues */ public static function getOptionLegendText(string $legendText, array $legendValues): string { return str_replace(array_keys($legendValues), array_values($legendValues), $legendText); } /** * @param array>> $statsCountries * @param array> $mapOptions * * @return array> */ public static function buildMapData(array $statsCountries, array $mapOptions, string $legendText): array { foreach ($mapOptions as $key => $value) { $mappedData = empty($statsCountries[$key]) ? [] : self::mapCountries($statsCountries[$key], $key); $result[] = [ 'data' => $mappedData['data'] ?? [], 'label' => $value['label'], 'legendText' => MapHelper::getOptionLegendText( $legendText, [ '%total' => $mappedData['total'] ?? 0, '%withCountry' => $mappedData['totalWithCountry'] ?? 0, ] ), 'unit' => $value['unit'], ]; } return $result ?? []; } /** * @param array> $stats * * @return array> */ public static function mapCountries(array $stats, string $countKey): array { $countries = array_flip(Countries::getNames('en')); $results = [ 'data' => [], 'total' => 0, 'totalWithCountry' => 0, ]; foreach ($stats as $s) { $countryName = $s['country']; $results['total'] += $s[$countKey]; if (isset($countries[$countryName])) { $countryCode = $countries[$countryName]; if (!empty($s[$countKey])) { $results['data'][$countryCode] = (int) $s[$countKey]; } $results['totalWithCountry'] += $s[$countKey]; } } return $results; } }