kslimani/laravel-geo

Laravel Geo.

1.0.4 2021-01-22 22:00 UTC

README

Laravel Geo

一个用于处理地区、国家和货币的简单服务提供者。

它使用以下依赖项:

安装

使用Composer将包添加到项目的依赖项中

composer require kslimani/laravel-geo

注意:Swap使用HTTPlug抽象,可能需要额外的依赖项。例如使用Curl

composer require php-http/curl-client nyholm/psr7 php-http/message

可选,在config/app.php中添加Geo外观

'aliases' => [
    // ...
    'Geo' => Sk\Geo\Facades\Geo::class,
];

发布配置文件 config/geo.php

php artisan vendor:publish --provider="Sk\Geo\GeoServiceProvider" --tag="config"

可选,发布Swap服务提供者配置文件

php artisan vendor:publish --provider="Swap\Laravel\SwapServiceProvider"

快速使用

use Sk\Geo\Facades\Geo;

// Get country code from ip address (US)
$countryCode = Geo::location()->ipCountry('8.8.8.8');

// Get country name (United States)
$countryName = Geo::locale()->country($countryCode);

// Get country language code (en)
$languageCode = Geo::locale()->countryLanguage($countryCode);

// Get country language name (English)
$languageName = Geo::locale()->language($languageCode);

// Get country currency code (USD)
$currencyCode = Geo::locale()->countryCurrency($countryCode);

// Get country currency name (US Dollar)
$currencyName = Geo::locale()->currency($currencyCode);

// Make money amount
$fiveDollars = Geo::money()->make('500', $currencyCode);

// Get amount converted to Euro
$euroAmount = Geo::money()->convert($fiveDollars, 'EUR');

// Get formatted amount ("Intl" formatter)
$intlFormattedAmount = Geo::money()->format($euroAmount);

// Get formatted amount ("Decimal" formatter)
$decFormattedAmount = Geo::money()->formatDec($euroAmount);

// Parse "Decimal" formatted amount
$newEuroAmount = Geo::money()->parse($decFormattedAmount, 'EUR');

// Decompose money amount
$keyValueArray = Geo::money()->decompose($fiveDollars);
// [
//   "locale" => "en"
//   "subunit" => 2
//   "sign" => "+"
//   "unsigned_part" => "5"
//   "decimal_part" => "00"
//   "grouping_separator" => ","
//   "decimal_separator" => "."
//   "symbol" => "$"
// ]

// Get all countries (country code -> name associative array)
$countries = Geo::locale()->countries();

// Get all languages (language code -> name associative array)
$languages = Geo::locale()->languages();

// Get all currencies (currency code -> name associative array)
$currencies = Geo::locale()->currencies();

// All methods returning a name accept an optional locale (default is application locale)
Geo::locale()->country('US', 'es');      // 'Estados Unidos'
Geo::locale()->language('en', 'de')      // 'Englisch'
Geo::locale()->currency('USD', 'ru')     // 'Доллар США'
Geo::locale()->countries('zh')           // [ 'BT' => '不丹', 'TL' => '东帝汶', ... ]
Geo::money()->format($fiveDollars, 'fr') // '5,00 $US'
Geo::money()->decompose($amount, 'fr');  // [ 'locale' => 'fr', ... ]

// Get instances using app helper
$location =  app('geo.location');
$locale = app('geo.locale');
$money = app('geo.money');

控制台命令

此包还提供了一些Artisan控制台命令

geo:exchange          Simple currency converter
geo:info              Display geo data for ip address
geo:list              Display countries with default language and currency
geo:maxmind           Display Maxmind DB countries with matched geo country name

注意:geo:listgeo:maxmind主要用于包开发。