Spaces:
No application file
No application file
File size: 2,397 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\Helper;
use Symfony\Component\Intl\Countries;
class MapHelper
{
/**
* @param array<string, int> $legendValues
*/
public static function getOptionLegendText(string $legendText, array $legendValues): string
{
return str_replace(array_keys($legendValues), array_values($legendValues), $legendText);
}
/**
* @param array<string, array<int, array<string, int|string>>> $statsCountries
* @param array<string, array<string, string>> $mapOptions
*
* @return array<int, array<string, mixed>>
*/
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<int, array<string, int|string>> $stats
*
* @return array<string, int|array<string, int>>
*/
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;
}
}
|