From d05eee801fc462c9b9988c60fc9fe84a8e2a2dda Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 27 Jan 2026 16:36:03 +0100 Subject: [PATCH] Add country flag --- app/Models/User.php | 30 +++++++++++++++++++++ resources/views/admin/users/index.blade.php | 2 +- tests/Feature/UserCountryLookupTest.php | 27 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/Models/User.php b/app/Models/User.php index 7a979ddc0..b1c98901e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -421,6 +421,36 @@ final class User extends Authenticatable ); } + /** + * Get country flag emoji from country code. + * Converts 2-letter country code to Unicode regional indicator symbols. + */ + protected function countryFlag(): Attribute + { + return Attribute::make( + get: function (): ?string { + $countryCode = $this->getCountryFromIp()['countryCode'] ?? null; + + if (empty($countryCode) || strlen($countryCode) !== 2) { + return null; + } + + // Convert country code to regional indicator symbols (flag emoji) + // Each letter is converted to its regional indicator equivalent + // A = πŸ‡¦ (U+1F1E6), B = πŸ‡§ (U+1F1E7), etc. + $countryCode = strtoupper($countryCode); + $flag = ''; + + for ($i = 0; $i < 2; $i++) { + $char = ord($countryCode[$i]) - ord('A') + 0x1F1E6; + $flag .= mb_chr($char, 'UTF-8'); + } + + return $flag; + }, + ); + } + /** * Lookup country information from IP address using ip-api.com. * diff --git a/resources/views/admin/users/index.blade.php b/resources/views/admin/users/index.blade.php index 1abe00d81..5a4f6fa3d 100644 --- a/resources/views/admin/users/index.blade.php +++ b/resources/views/admin/users/index.blade.php @@ -266,7 +266,7 @@ {{ $user->host ?? 'N/A' }} @if(!empty($user->country_code)) - {{ $user->country_code }} + {{ $user->country_flag }} {{ $user->country_code }} @else N/A @endif diff --git a/tests/Feature/UserCountryLookupTest.php b/tests/Feature/UserCountryLookupTest.php index d0ddbdb21..1864983ec 100644 --- a/tests/Feature/UserCountryLookupTest.php +++ b/tests/Feature/UserCountryLookupTest.php @@ -101,4 +101,31 @@ final class UserCountryLookupTest extends TestCase // Clean up Cache::forget('ip_country_lookup_'.md5($ip)); } + + public function test_country_flag_returns_emoji_flag(): void + { + // Set up a cache entry for US + $ip = '8.8.4.4'; + $cacheKey = 'ip_country_lookup_'.md5($ip); + + Cache::put($cacheKey, [ + 'country' => 'United States', + 'countryCode' => 'US', + ], 86400); + + $user = new User; + $user->host = $ip; + + $this->assertEquals('πŸ‡ΊπŸ‡Έ', $user->country_flag); + + Cache::forget($cacheKey); + } + + public function test_country_flag_returns_null_for_no_host(): void + { + $user = new User; + $user->host = null; + + $this->assertNull($user->country_flag); + } }