tobento / service-country
PHP应用程序的国家接口和资源。
Requires
- php: >=8.0
- tobento/service-collection: ^1.0
- tobento/service-support: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.0
README
PHP应用程序的国家接口。
目录
入门
运行以下命令以安装国家服务项目的最新版本。
composer require tobento/service-country
要求
- PHP 8.0 或更高版本
亮点
- 框架无关,可与任何项目一起使用
- 解耦设计
文档
国家存储库
国家存储库从 json 文件中加载国家。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryRepositoryInterface; use Tobento\Service\Country\CountriesFactoryInterface; $countryRepository = new CountryRepository( locale: 'en', // default localeFallbacks: ['es' => 'fr'], localeMapping: ['en-GB' => 'en'], countriesFactory: null, // null|CountriesFactoryInterface directory: null, // if null it loads from the provided country files. ); var_dump($countryRepository instanceof CountryRepositoryInterface); // bool(true)
国家存储库接口
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryRepositoryInterface; $countryRepository = new CountryRepository(); var_dump($countryRepository instanceof CountryRepositoryInterface); // bool(true)
findCountry
根据指定的参数返回单个国家,如果未找到则返回 null。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $country = $countryRepository->findCountry(code: 'US'); var_dump($country instanceof CountryInterface); // bool(true) var_dump($country->name()); // string(13) "United States" // find by specific locale $country = $countryRepository->findCountry( code: 'US', locale: 'de' ); var_dump($country->name()); string(18) "Vereinigte Staaten"
查看国家接口了解更多信息。
findCountries
返回根据指定参数找到的所有国家。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountriesInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); var_dump($countries instanceof CountriesInterface); // bool(true) // find by specific locale and group $countries = $countryRepository->findCountries( locale: 'de', group: 'shipping', );
查看国家接口了解更多信息。
国家
创建国家
use Tobento\Service\Country\Country; use Tobento\Service\Country\CountryInterface; $country = new Country(code: 'US'); var_dump($country instanceof CountryInterface); // bool(true)
查看国家接口了解更多信息。
国家工厂
使用提供的 countryfactory 轻松创建国家
createCountry
use Tobento\Service\Country\CountryFactory; use Tobento\Service\Country\CountryFactoryInterface; use Tobento\Service\Country\CountryInterface; $countryFactory = new CountryFactory(); var_dump($countryFactory instanceof CountryFactoryInterface); // bool(true) $country = $countryFactory->createCountry(code: 'CH'); var_dump($country instanceof CountryInterface); // bool(true)
参数
use Tobento\Service\Country\CountryFactory; $countryFactory = new CountryFactory(); $country = $countryFactory->createCountry( code: 'US', code3: 'USA', numericCode: '840', currencyKey: 'USD', locale: 'en', name: 'United States', region: '', continent: 'North America', id: 840, active: true, group: 'shipping', priority: 100, );
createCountryFromArray
use Tobento\Service\Country\CountryFactory; $countryFactory = new CountryFactory(); $country = $countryFactory->createCountryFromArray([ 'code' => 'US', ]);
国家接口
use Tobento\Service\Country\CountryInterface; use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $country = $countryRepository->findCountry('US'); var_dump($country instanceof CountryInterface); // bool(true) var_dump($country->code()); // string(2) "US" var_dump($country->code3()); // string(3) "USA" var_dump($country->numericCode()); // string(3) "840" var_dump($country->currencyKey()); // string(3) "USD" var_dump($country->locale()); // string(2) "en" var_dump($country->name()); // string(13) "United States" var_dump($country->region()); // string(0) "" var_dump($country->continent()); // string(13) "North America" var_dump($country->timezones()); // array(29) { [0]=> string(12) "America/Adak" ... } var_dump($country->id()); // int(0) var_dump($country->active()); // bool(true) var_dump($country->group()); // string(0) "" var_dump($country->priority()); // int(0)
方法
with 方法将返回一个新的实例。
use Tobento\Service\Country\CountryInterface; use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $country = $countryRepository->findCountry('US'); var_dump($country instanceof CountryInterface); // bool(true) $country = $country->withCurrencyKey('USD'); $country = $country->withLocale('de'); $country = $country->withName('Vereinigte Staaten'); $country = $country->withRegion('Region'); $country = $country->withId(2345); $country = $country->withActive(false); $country = $country->withGroup('payment'); $country = $country->withPriority(150);
国家
创建国家
use Tobento\Service\Country\Countries; use Tobento\Service\Country\CountriesInterface; use Tobento\Service\Country\Country; use Tobento\Service\Country\CountryInterface; $countries = new Countries( new Country(code: 'US'), // CountryInterface new Country(code: 'CH'), ); var_dump($countries instanceof CountriesInterface); // bool(true)
查看国家接口了解更多信息。
国家工厂
使用提供的 countries factory 轻松创建 Countries 对象
createCountries
use Tobento\Service\Country\CountriesFactory; use Tobento\Service\Country\CountriesFactoryInterface; use Tobento\Service\Country\CountryFactoryInterface; use Tobento\Service\Country\CountriesInterface; use Tobento\Service\Country\CountryInterface; use Tobento\Service\Country\Country; $countriesFactory = new CountriesFactory( countryFactory: null // null|CountryFactoryInterface ); var_dump($countriesFactory instanceof CountriesFactoryInterface); // bool(true) $countries = $countriesFactory->createCountries( new Country(code: 'US'), // CountryInterface new Country(code: 'CH'), ); var_dump($countries instanceof CountriesInterface); // bool(true)
查看国家接口了解更多信息。
createCountriesFromArray
use Tobento\Service\Country\CountriesFactory; use Tobento\Service\Country\CountriesInterface; $countriesFactory = new CountriesFactory(); $countries = $countriesFactory->createCountriesFromArray([ ['code' => 'US'], ['code' => 'CH'], ]); var_dump($countries instanceof CountriesInterface); // bool(true)
查看国家接口了解更多信息。
国家接口
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountriesInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); var_dump($countries instanceof CountriesInterface); // bool(true)
addCountry
添加国家。
use Tobento\Service\Country\CountriesFactory; use Tobento\Service\Country\CountryInterface; use Tobento\Service\Country\Country; $countries = (new CountriesFactory())->createCountries(); $countries->addCountry( country: new Country(code: 'US') // CountryInterface );
addCountries
use Tobento\Service\Country\CountriesFactory; use Tobento\Service\Country\CountryInterface; use Tobento\Service\Country\Country; $countries = (new CountriesFactory())->createCountries(); $countries->addCountries( new Country(code: 'US'), // CountryInterface new Country(code: 'CH'), );
sort
返回一个包含排序国家的新实例。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); // sorted by country name. $countries = $countries->sort(); // sort by callback. $countries = $countries->sort( fn(CountryInterface $a, CountryInterface $b): int => $a->priority() <=> $b->priority() );
filter
返回一个包含过滤国家的新实例。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->filter( fn(CountryInterface $c): bool => in_array($c->locale(), ['de', 'en']) );
code
返回一个包含指定代码过滤的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->code(code: 'US');
locale
返回一个包含指定区域过滤的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->locale(locale: 'de');
group
返回一个包含指定组过滤的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->group(group: 'shipping');
region
返回一个包含指定地区过滤的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->region(region: 'nearBy');
continent
返回一个包含指定大陆过滤的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->continent(continent: 'Europe');
all
返回所有国家。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); foreach($countries->all() as $country) { var_dump($country instanceof CountryInterface); // bool(true) } // or just foreach($countries as $country) { var_dump($country instanceof CountryInterface); // bool(true) }
first
返回第一个国家,如果没有则返回 null。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $country = $countries->first(); var_dump($country instanceof CountryInterface); // bool(true)
get
返回根据代码、code3、numericCode 和/或区域找到的第一个国家。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $country = $countries->get(code: 'US'); var_dump($country instanceof CountryInterface); // bool(true) // and with locale $country = $countries->get(code: 'US', locale: 'en'); var_dump($country instanceof CountryInterface); // bool(true)
column
返回国家的列。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $column = $countries->column('name'); var_dump($column); // array(249) { [0]=> string(11) "Afghanistan" ... } // indexed by country code $column = $countries->column(column: 'name', index: 'code'); var_dump($column); // { ["AF"]=> string(11) "Afghanistan" ... }
groupedColumn
返回分组的国家的列。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $column = $countries->groupedColumn( group: 'continent', column: 'name', index: 'code', // optional ); print_r($column); /*Array ( [Asia] => Array ( [AF] => Afghanistan ... ) [Europe] => Array ( [AX] => Åland Islands ... ) ... )*/
only
返回一个只包含指定代码的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->only(['CH', 'US']);
except
返回一个除指定代码之外的国家的新实例。
use Tobento\Service\Country\CountryRepository; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->except(['CH', 'US']);
map
返回一个包含映射的国家的新实例。
use Tobento\Service\Country\CountryRepository; use Tobento\Service\Country\CountryInterface; $countryRepository = new CountryRepository(); $countries = $countryRepository->findCountries(); $countries = $countries->map(function(CountryInterface $c): CountryInterface { if (in_array($c->code(), ['CH', 'FR'])) { return $c->withRegion('Near by')->withPriority(100); } return $c->withRegion('All Others'); });