Skip to content

Commit 5df5461

Browse files
committed
Adding Laravel specific package to monorepo
1 parent 08c99e4 commit 5df5461

26 files changed

+1347
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/.php-cs-fixer.php export-ignore
88
/docs export-ignore
99
/build export-ignore
10+
/src/Laravel export-ignore
1011
/test_files export-ignore
1112
/phpstan.neon export-ignore
1213
/phpunit.xml export-ignore

composer.json

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
"src/Test**"
3333
]
3434
},
35+
"autoload-dev": {
36+
"files": [
37+
"src/Laravel/helpers.php"
38+
]
39+
},
3540
"require": {
3641
"ext-json": "*",
3742
"php": "^8.0",
@@ -40,6 +45,11 @@
4045
},
4146
"require-dev": {
4247
"friendsofphp/php-cs-fixer": "^3.8",
48+
"illuminate/config": "^8.0 || ^9.0",
49+
"illuminate/support": "^8.0 || ^9.0",
50+
"moneyphp/money": "^3.0 || ^4.0",
51+
"nunomaduro/larastan": "^1.0.0 || ^2.1",
52+
"orchestra/testbench": "^6.6.0 || ^7.5",
4353
"phpstan/phpstan": "^1.7",
4454
"phpstan/phpstan-deprecation-rules": "^1.0",
4555
"phpstan/phpstan-phpunit": "^1.1",

src/Laravel/.gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* text=auto
2+
3+
/.gitattributes export-ignore
4+
/.github export-ignore
5+
/test_files export-ignore
6+
/CHANGELOG.md export-ignore
7+
/README.md export-ignore
8+
/**/*Test.php export-ignore
9+
/**/Test** export-ignore

src/Laravel/.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [nyamsprod]
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
name: build
3+
on:
4+
push: ~
5+
pull_request: ~
6+
7+
jobs:
8+
linux_tests:
9+
name: PHP on ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.composer-flags }}
10+
runs-on: ubuntu-20.04
11+
strategy:
12+
matrix:
13+
php: ['7.4', '8.0', '8.1']
14+
stability: [prefer-stable]
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php }}
23+
coverage: xdebug
24+
tools: composer:v2
25+
- name: Check PHP Version
26+
run: php -v
27+
28+
- name: Validate composer files
29+
run: composer validate --strict
30+
31+
- name: Get Composer Cache Directory
32+
id: composer-cache
33+
run: |
34+
echo "::set-output name=dir::$(composer config cache-files-dir)"
35+
- uses: actions/cache@v2
36+
with:
37+
path: ${{ steps.composer-cache.outputs.dir }}
38+
key: ${{ runner.os }}-composer-${{ matrix.stability }}-${{ hashFiles('**/composer.lock') }}
39+
restore-keys: |
40+
${{ runner.os }}-composer-${{ matrix.stability }}-
41+
${{ runner.os }}-composer-
42+
43+
- name: Install dependencies
44+
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}
45+
46+
- name: Run Unit tests with coverage
47+
run: composer phpunit
48+
49+
- name: Run static analysis
50+
run: composer phpstan
51+
if: ${{ matrix.php == '8.1' && matrix.stability == 'prefer-stable'}}
52+
53+
- name: Run Coding style rules
54+
run: composer phpcs:fix
55+
if: ${{ matrix.php == '8.1' && matrix.stability == 'prefer-stable'}}

src/Laravel/CHANGELOG.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Changelog
2+
3+
All Notable changes to `bakame/laravel-intl-formatter` will be documented in this file
4+
5+
## [0.3.0] - 2022-06-14
6+
7+
### Added
8+
9+
- Support for `Money` package.
10+
- Added `IntlFactory` facade.
11+
- `NumberFactory` and `DateFactory` are now accessible as readonly property of the `Factory` class.
12+
13+
### Fixed
14+
15+
- **[BC Break]** Renamed `Factory::newInstance` in `Factory::newFormatter`
16+
- **[BC Break]** Keys used for container registration are updated.
17+
18+
### Deprecated
19+
20+
- None
21+
22+
### Removed
23+
24+
**[BC Break]** `Factory::newInstance` replaced by `Factory::newFormatter`
25+
26+
## [0.2.0] - 2022-06-06
27+
28+
** Dependencies fixes
29+
30+
## [0.1.0] - 2022-06-04
31+
32+
**Initial release!**
33+
34+
[Next]: https://github.com/bakame-php/laravel-intl-formatter/compare/0.3.0...main
35+
[Next]: https://github.com/bakame-php/laravel-intl-formatter/compare/0.2.0...0.3.0
36+
[0.2.0]: https://github.com/bakame-php/laravel-intl-formatter/compare/0.1.0...0.2.0
37+
[0.1.0]: https://github.com/bakame-php/laravel-intl-formatter/releases/tag/0.1.0

src/Laravel/Factory.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bakame\Intl\Laravel;
6+
7+
use Bakame\Intl\DateFactory;
8+
use Bakame\Intl\DateResolver;
9+
use Bakame\Intl\Formatter;
10+
use Bakame\Intl\NumberFactory;
11+
use Bakame\Intl\Option\AttributeFormat;
12+
use Bakame\Intl\Option\CalendarFormat;
13+
use Bakame\Intl\Option\DateFormat;
14+
use Bakame\Intl\Option\PaddingPosition;
15+
use Bakame\Intl\Option\RoundingMode;
16+
use Bakame\Intl\Option\StyleFormat;
17+
use Bakame\Intl\Option\SymbolFormat;
18+
use Bakame\Intl\Option\TextFormat;
19+
use Bakame\Intl\Option\TimeFormat;
20+
use Locale;
21+
use Money\Currencies\ISOCurrencies;
22+
use Money\Formatter\IntlMoneyFormatter;
23+
24+
final class Factory
25+
{
26+
/** @readonly */
27+
public DateFactory $dateFactory;
28+
/** @readonly */
29+
public NumberFactory $numberFactory;
30+
31+
public function __construct(DateFactory $dateFactory, NumberFactory $numberFactory)
32+
{
33+
$this->dateFactory = $dateFactory;
34+
$this->numberFactory = $numberFactory;
35+
}
36+
37+
/**
38+
* @param array{
39+
* date:array{
40+
* dateFormat:key-of<DateFormat::INTL_MAPPER>,
41+
* timeFormat:key-of<TimeFormat::INTL_MAPPER>,
42+
* calendar:key-of<CalendarFormat::INTL_MAPPER>,
43+
* pattern?:?string,
44+
* },
45+
* number:array{
46+
* style:key-of<StyleFormat::INTL_MAPPER>,
47+
* pattern?:?string,
48+
* attributes?:array<key-of<AttributeFormat::INTL_MAPPER>, int|float|key-of<RoundingMode::INTL_MAPPER>|key-of<PaddingPosition::INTL_MAPPER>>,
49+
* textAttributes?:array<key-of<TextFormat::INTL_MAPPER>, string>,
50+
* symbolAttributes?:array<key-of<SymbolFormat::INTL_MAPPER>, string>
51+
* }
52+
* } $settings
53+
*/
54+
public static function fromAssociative(array $settings): self
55+
{
56+
return new self(
57+
DateFactory::fromAssociative($settings['date']),
58+
NumberFactory::fromAssociative($settings['number'])
59+
);
60+
}
61+
62+
public function newFormatter(DateResolver $dateResolver): Formatter
63+
{
64+
return new Formatter($this->dateFactory, $this->numberFactory, $dateResolver);
65+
}
66+
67+
/**
68+
* @param array<key-of<AttributeFormat::INTL_MAPPER>, int|float|key-of<RoundingMode::INTL_MAPPER>|key-of<PaddingPosition::INTL_MAPPER>> $attrs
69+
*/
70+
public function newIntlMoneyFormatter(?string $locale = null, ?string $style = null, array $attrs = []): IntlMoneyFormatter
71+
{
72+
$locale = $locale ?? Locale::getDefault();
73+
$hash = $locale.'|'.json_encode($style).'|'.json_encode($attrs);
74+
static $instances = [];
75+
if (!isset($instances[$hash])) {
76+
$instances[$hash] = new IntlMoneyFormatter(
77+
$this->numberFactory->createNumberFormatter($locale, $style, $attrs),
78+
new ISOCurrencies()
79+
);
80+
}
81+
82+
return $instances[$hash];
83+
}
84+
}

src/Laravel/FactoryTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bakame\Intl\Laravel;
6+
7+
use Illuminate\Foundation\Application;
8+
use Illuminate\Support\Facades\Artisan;
9+
use Money\Money;
10+
use Orchestra\Testbench\TestCase as LaravelTestCase;
11+
12+
final class FactoryTest extends LaravelTestCase
13+
{
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
Artisan::call('cache:clear');
19+
}
20+
21+
/**
22+
* @param Application $app
23+
*
24+
* @return array<int, class-string>
25+
*/
26+
protected function getPackageProviders($app): array
27+
{
28+
return [
29+
Provider::class,
30+
];
31+
}
32+
33+
/** @test */
34+
public function it_can_format_a_money_object(): void
35+
{
36+
$money = Money::EUR(250);
37+
38+
self::assertSame('2,50 €', IntlFactory::newIntlMoneyFormatter('fr', 'currency')->format($money));
39+
self::assertSame('2,50 €', format_currency($money, null, 'fr'));
40+
41+
self::assertSame('deux virgule cinq', IntlFactory::newIntlMoneyFormatter('fr', 'spellout')->format($money));
42+
self::assertSame('deux virgule cinq', format_number($money, 'fr', 'default', [], 'spellout'));
43+
}
44+
}

0 commit comments

Comments
 (0)