1. Go to this page and download the library: Download tobento/service-country library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tobento / service-country example snippets
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)
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"
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)
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\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)
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)
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)
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
);
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'),
);
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()
);
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'])
);
use Tobento\Service\Country\CountryRepository;
$countryRepository = new CountryRepository();
$countries = $countryRepository->findCountries();
$countries = $countries->code(code: 'US');
use Tobento\Service\Country\CountryRepository;
$countryRepository = new CountryRepository();
$countries = $countryRepository->findCountries();
$countries = $countries->locale(locale: 'de');
use Tobento\Service\Country\CountryRepository;
$countryRepository = new CountryRepository();
$countries = $countryRepository->findCountries();
$countries = $countries->group(group: 'shipping');
use Tobento\Service\Country\CountryRepository;
$countryRepository = new CountryRepository();
$countries = $countryRepository->findCountries();
$countries = $countries->region(region: 'nearBy');
use Tobento\Service\Country\CountryRepository;
$countryRepository = new CountryRepository();
$countries = $countryRepository->findCountries();
$countries = $countries->continent(continent: 'Europe');
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)
}
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)
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)
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" ... }