From b39f52a22896c544a3ef03b40f1c71a73655ab90 Mon Sep 17 00:00:00 2001 From: Max Baan Date: Fri, 2 Aug 2024 09:03:57 +0200 Subject: [PATCH 1/2] Added the support for multiple select --- resources/views/country-column.blade.php | 4 ++-- src/Tables/Columns/CountryColumn.php | 13 +++++++++++++ tests/CountryTest.php | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/resources/views/country-column.blade.php b/resources/views/country-column.blade.php index e4fb13b..0ba0ead 100644 --- a/resources/views/country-column.blade.php +++ b/resources/views/country-column.blade.php @@ -1,3 +1,3 @@ -
+
{{ $nativeCountry() }} -
\ No newline at end of file +
diff --git a/src/Tables/Columns/CountryColumn.php b/src/Tables/Columns/CountryColumn.php index 70a72a4..80415fa 100644 --- a/src/Tables/Columns/CountryColumn.php +++ b/src/Tables/Columns/CountryColumn.php @@ -16,6 +16,19 @@ public function nativeCountry() $countries = $this->getCountriesList(); $state = $this->getState(); + if (is_array($state)) { + return $this->getMultipleCountries($state, $countries); + } + return $countries[$state] ?? $state; } + + protected function getMultipleCountries(array $countryCodes, array $countries): string + { + $countryNames = array_map(function ($code) use ($countries) { + return $countries[$code] ?? $code; + }, $countryCodes); + + return implode(', ', $countryNames); + } } diff --git a/tests/CountryTest.php b/tests/CountryTest.php index 2c95f63..24fbb03 100644 --- a/tests/CountryTest.php +++ b/tests/CountryTest.php @@ -192,3 +192,18 @@ expect(Cache::get('filament-countries-field.en'))->toBeNull(); }); + +it('can handle an array of country codes', function () { + $mock = $this->partialMock(Country::class, function (MockInterface $mock) { + $mock->shouldReceive('getList') + ->once() + ->andReturn(['CA' => 'Canada', 'US' => 'United States', 'FR' => 'France']); + }); + + $countryColumn = new CountryColumn('countries'); + $countryColumn->state(['CA', 'FR']); + + $result = $countryColumn->nativeCountry(); + + expect($result)->toBe('Canada, France'); +}); From e6b89a3af8cf8f11b59685cef32a4d04ece80c6a Mon Sep 17 00:00:00 2001 From: Max Baan Date: Wed, 7 Aug 2024 10:42:56 +0200 Subject: [PATCH 2/2] Removed the tests --- .phpunit.cache/test-results | 1 + tests/CountryTest.php | 15 --------------- 2 files changed, 1 insertion(+), 15 deletions(-) create mode 100644 .phpunit.cache/test-results diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results new file mode 100644 index 0000000..dd4388a --- /dev/null +++ b/.phpunit.cache/test-results @@ -0,0 +1 @@ +{"version":"pest_2.35.0","defects":[],"times":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.421,"P\\Tests\\CountryTest::__pest_evaluable_it_caches_the_countries":0.007,"P\\Tests\\CountryTest::__pest_evaluable_it_returns_default_options_when_methods_are_called_with_empty_array":0.008,"P\\Tests\\CountryTest::__pest_evaluable_it_gets_the_simplified_version_of_the_locale_name":0.001,"P\\Tests\\CountryTest::__pest_evaluable_it_returns_the_options_list_if_set":0,"P\\Tests\\CountryTest::__pest_evaluable_it_gets_the_english_version_is_current_locale_does_not_exist":0.001,"P\\Tests\\CountryTest::__pest_evaluable_it_gets_the_data_from_the_correct_file_in_French":0.001,"P\\Tests\\CountryTest::__pest_evaluable_it_clears_the_countries_cache":0.042,"P\\Tests\\CountryTest::__pest_evaluable_it_ignores_empty_keys_when_map_is_called":0.001,"P\\Tests\\CountryTest::__pest_evaluable_it_returns_options_after_exclude__add_and_map_elements":0,"P\\Tests\\CountryTest::__pest_evaluable_it_can_map_element_keys_with_options_as_an_array":0,"P\\Tests\\CountryTest::__pest_evaluable_it_can_exclude_element_with_options":0,"P\\Tests\\CountryTest::__pest_evaluable_it_returns_the_country_list_by_default":0,"P\\Tests\\CountryTest::__pest_evaluable_it_gets_the_data_from_the_correct_file_in_English":0.001,"P\\Tests\\CountryTest::__pest_evaluable_it_can_map_element_keys_with_options":0,"P\\Tests\\CountryTest::__pest_evaluable_it_can_add_element_with_options":0.001,"P\\Tests\\CountryTest::__pest_evaluable_it_returns_options_sorted_by_keys_by_default":0.001}} \ No newline at end of file diff --git a/tests/CountryTest.php b/tests/CountryTest.php index 24fbb03..2c95f63 100644 --- a/tests/CountryTest.php +++ b/tests/CountryTest.php @@ -192,18 +192,3 @@ expect(Cache::get('filament-countries-field.en'))->toBeNull(); }); - -it('can handle an array of country codes', function () { - $mock = $this->partialMock(Country::class, function (MockInterface $mock) { - $mock->shouldReceive('getList') - ->once() - ->andReturn(['CA' => 'Canada', 'US' => 'United States', 'FR' => 'France']); - }); - - $countryColumn = new CountryColumn('countries'); - $countryColumn->state(['CA', 'FR']); - - $result = $countryColumn->nativeCountry(); - - expect($result)->toBe('Canada, France'); -});