定制 digitalrevolution/intl 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

digitalrevolution/intl

最新稳定版本:v1.8.0

Composer 安装命令:

composer require digitalrevolution/intl

包简介

Digital Revolution Internationalization library

README 文档

README

Minimum PHP Version Run tests

Digital Revolution Internationalization

Library to format Numbers, Dates, \Money\Money objects and currencies to string according to the locale.

Getting Started

composer require digitalrevolution/intl

Usage

NumberFormatService

Format number and currencies

use DR\Internationalization\Currency\CurrencyFormatOptions;
use DR\Internationalization\Number\NumberFormatOptions;
use DR\Internationalization\NumberFormatService;
use Money\Money;

// set default configuration
$currencyOptions = (new CurrencyFormatOptions())
    ->setLocale('nl_NL')
    ->setCurrencyCode('EUR')
    ->setGrouping(false);
$numberOptions = (new NumberFormatOptions())
    ->setLocale('nl_NL')
    ->setDecimals(2)
    ->setTrimDecimals(true); 
$service = new NumberFormatService($currencyOptions, $numberOptions);

Format currencies:

$service->currency(1500.5);                                                       
// output: € 1500,50

$service->currency(new Money('150050', new Currency('EUR')));                    
// output: € 1500,50

$service->currency(1500.5, (new CurrencyFormatOptions())->setGrouping(true));
// output: € 1.500,50

Format numbers:

$service->number(1500.5);                                                        
// output: 1500,50

$service->number(1500.5, (new NumberFormatOptions())->setGrouping(true));   
// output: 1.500,50

$service->number(1500.0, (new NumberFormatOptions())->setTrimDecimals(NumberFormatOptions::TRIM_DECIMAL_ALL_OR_NOTHING));  
// output: 1500

$service->number(1500.5, (new NumberFormatOptions())->setTrimDecimals(NumberFormatOptions::TRIM_DECIMAL_ALL_OR_NOTHING));  
// output: 1500.50

$service->number(1500.5, (new NumberFormatOptions())->setTrimDecimals(NumberFormatOptions::TRIM_DECIMAL_ANY));  
// output: 1500.5

NumberParser

Parse float number from string determining the user's input for thousands and decimals separator.

NumberParser::parseFloat('1050');
// output: 1050.0

NumberParser::parseFloat('1050.5');
// output: 1050.5

NumberParser::parseFloat('1050,5');
// output: 1050.5

NumberParser::parseFloat('1.050,5');
// output: 1050.5

NumberParser::parseFloat('1,050.5');
// output: 1050.5

NumberParser::parseFloat('1,000,050.5');
// output: 1000050.5

DateFormatService

Formats dates and times. Input can be timestamps, strings (compatible with strtotime) and DateTimeInterface objects.

Use the following for format: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table

$dateFormatOptions = new DateFormatOptions('nl_NL', date_default_timezone_get())
$dateFormatter = new DateFormatService($dateFormatOptions);

$dateFormatter->format(time(), 'eeee dd LLLL Y - HH:mm:ss');
// example output: zaterdag 02 juni 2040 - 05:57:02

$dateFormatter->format('next saturday', 'eeee dd LLLL Y - HH:mm:ss');
// example output: zaterdag 02 juni 2040 - 05:57:02

$dateFormatter->format(new DateTime(), 'eeee dd LLLL Y - HH:mm:ss');
// example output: zaterdag 02 juni 2040 - 05:57:02

It is also possible to format dates and times to relative dates, such as 'today' and 'tomorrow' The RelativeDateFormatOptions decides how many days ahead it will try to convert a date to a relative date.

$dateFormatOptions = new DateFormatOptions('nl_NL', date_default_timezone_get())
$dateFormatter = new DateFormatService($dateFormatOptions);

$dateFormatter->formatRelative(time(), 'Y-m-d', new RelativeDateFormatOptions(1));
// example output: Vandaag

$dateFormatter->formatRelative(new DateTime('+1 day'), 'Y-m-d', new RelativeDateFormatOptions(1);
// example output: Morgen

$dateFormatter->formatRelative(new DateTime('+2 days'), 'Y-m-d', new RelativeDateFormatOptions(2));
// example output: Overmorgen

$dateFormatter->formatRelative(new DateTime('-2 days'), 'Y-m-d', new RelativeDateFormatOptions(2));
// example output: Eergisteren

// This will not convert the date to a relative date, as the options limit it one day ahead. Instead, it formats the date to the given pattern.
$dateFormatter->formatRelative(new DateTime('+2 days'), 'Y-m-d', new RelativeDateFormatOptions(1));
// example output: 2024-01-03

DayOfTheWeekFormatter

Format the PHP Date day of the week to string

$formatter = new DayOfTheWeekFormatter('nl_NL');

$formatter->format(DayOfTheWeekFormatter::MONDAY);
// output: maandag

$formatter->format(DayOfTheWeekFormatter::MONDAY, 'en_US');
// output: Monday

PhoneNumberFormatService

Format phoneNumbers

use DR\Internationalization\PhoneNumber\PhoneNumberFormatOptions;
use DR\Internationalization\PhoneNumberFormatService;

// set default configuration
$phoneNumberOptions = (new PhoneNumberFormatOptions())
    ->setDefaultCountryCode('NL')
    ->setFormat(PhoneNumberFormatOptions::FORMAT_INTERNATIONAL_DIAL);
$service = new PhoneNumberFormatService($phoneNumberOptions);

$service->format("+31612345678");                                                       
// output: 0031612345678

$service->format("0612345678");                                                       
// output: 0031612345678

PhoneNumberParseService

use DR\Internationalization\PhoneNumberParseService;

$parseService = new PhoneNumberParseService("NL");
$parsedPhoneNumber = $parseService->parse("+31612345678");

Project structure

Directory Description
Currency Format int, float or Money value to locale specific format. Use NumberFormatService::currency
Date Format ISO-8601 day of the week to user friendly names
Money Create Money object from float
Number Format int or float value to locale specific format. Use NumberFormatService::number
PhoneNumber Format phoneNumber value to specified format. Use PhoneNumberFormatService::format

Development

Run code quality checks:

composer run check

Run unit tests:

composer run test

About us

At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.

统计信息

  • 总下载量: 35.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-24