Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Laravel specific package to monorepo #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/.php-cs-fixer.php export-ignore
/docs export-ignore
/build export-ignore
/src/Laravel export-ignore
/test_files export-ignore
/phpstan.neon export-ignore
/phpunit.xml export-ignore
Expand Down
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"src/Test**"
]
},
"autoload-dev": {
"files": [
"src/Laravel/helpers.php"
]
},
"require": {
"ext-json": "*",
"php": "^8.0",
Expand All @@ -40,6 +45,11 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
"illuminate/config": "^8.0 || ^9.0",
"illuminate/support": "^8.0 || ^9.0",
"moneyphp/money": "^3.0 || ^4.0",
"nunomaduro/larastan": "^1.0.0 || ^2.1",
"orchestra/testbench": "^6.6.0 || ^7.5",
"phpstan/phpstan": "^1.7",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.1",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- vendor/nunomaduro/larastan/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
Expand Down
9 changes: 9 additions & 0 deletions src/Laravel/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* text=auto

/.gitattributes export-ignore
/.github export-ignore
/test_files export-ignore
/CHANGELOG.md export-ignore
/README.md export-ignore
/**/*Test.php export-ignore
/**/Test** export-ignore
1 change: 1 addition & 0 deletions src/Laravel/.github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [nyamsprod]
55 changes: 55 additions & 0 deletions src/Laravel/.github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
name: build
on:
push: ~
pull_request: ~

jobs:
linux_tests:
name: PHP on ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.composer-flags }}
runs-on: ubuntu-20.04
strategy:
matrix:
php: ['7.4', '8.0', '8.1']
stability: [prefer-stable]
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
tools: composer:v2
- name: Check PHP Version
run: php -v

- name: Validate composer files
run: composer validate --strict

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.stability }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-${{ matrix.stability }}-
${{ runner.os }}-composer-

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}

- name: Run Unit tests with coverage
run: composer phpunit

- name: Run static analysis
run: composer phpstan
if: ${{ matrix.php == '8.1' && matrix.stability == 'prefer-stable'}}

- name: Run Coding style rules
run: composer phpcs:fix
if: ${{ matrix.php == '8.1' && matrix.stability == 'prefer-stable'}}
37 changes: 37 additions & 0 deletions src/Laravel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Changelog

All Notable changes to `bakame/laravel-intl-formatter` will be documented in this file

## [0.3.0] - 2022-06-14

### Added

- Support for `Money` package.
- Added `IntlFactory` facade.
- `NumberFactory` and `DateFactory` are now accessible as readonly property of the `Factory` class.

### Fixed

- **[BC Break]** Renamed `Factory::newInstance` in `Factory::newFormatter`
- **[BC Break]** Keys used for container registration are updated.

### Deprecated

- None

### Removed

**[BC Break]** `Factory::newInstance` replaced by `Factory::newFormatter`

## [0.2.0] - 2022-06-06

** Dependencies fixes

## [0.1.0] - 2022-06-04

**Initial release!**

[Next]: https://github.com/bakame-php/laravel-intl-formatter/compare/0.3.0...main
[Next]: https://github.com/bakame-php/laravel-intl-formatter/compare/0.2.0...0.3.0
[0.2.0]: https://github.com/bakame-php/laravel-intl-formatter/compare/0.1.0...0.2.0
[0.1.0]: https://github.com/bakame-php/laravel-intl-formatter/releases/tag/0.1.0
85 changes: 85 additions & 0 deletions src/Laravel/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Bakame\Intl\Laravel;

use Bakame\Intl\DateFactory;
use Bakame\Intl\DateResolver;
use Bakame\Intl\Formatter;
use Bakame\Intl\NumberFactory;
use Bakame\Intl\Option\AttributeFormat;
use Bakame\Intl\Option\CalendarFormat;
use Bakame\Intl\Option\DateFormat;
use Bakame\Intl\Option\PaddingPosition;
use Bakame\Intl\Option\RoundingMode;
use Bakame\Intl\Option\StyleFormat;
use Bakame\Intl\Option\SymbolFormat;
use Bakame\Intl\Option\TextFormat;
use Bakame\Intl\Option\TimeFormat;
use Locale;
use Money\Currencies\ISOCurrencies;
use Money\Formatter\IntlMoneyFormatter;

final class Factory
{
/** @readonly */
public DateFactory $dateFactory;
/** @readonly */
public NumberFactory $numberFactory;

public function __construct(DateFactory $dateFactory, NumberFactory $numberFactory)
{
$this->dateFactory = $dateFactory;
$this->numberFactory = $numberFactory;
}

/**
* @param array{
* date:array{
* dateFormat:key-of<DateFormat::INTL_MAPPER>,
* timeFormat:key-of<TimeFormat::INTL_MAPPER>,
* calendar:key-of<CalendarFormat::INTL_MAPPER>,
* pattern?:?string,
* },
* number:array{
* style:key-of<StyleFormat::INTL_MAPPER>,
* pattern?:?string,
* attributes?:array<key-of<AttributeFormat::INTL_MAPPER>, int|float|key-of<RoundingMode::INTL_MAPPER>|key-of<PaddingPosition::INTL_MAPPER>>,
* textAttributes?:array<key-of<TextFormat::INTL_MAPPER>, string>,
* symbolAttributes?:array<key-of<SymbolFormat::INTL_MAPPER>, string>
* }
* } $settings
*/
public static function fromAssociative(array $settings): self
{
return new self(
DateFactory::fromAssociative($settings['date']),
NumberFactory::fromAssociative($settings['number'])
);
}

public function newFormatter(DateResolver $dateResolver): Formatter
{
return new Formatter($this->dateFactory, $this->numberFactory, $dateResolver);
}

/**
* @param StyleFormat|key-of<StyleFormat::INTL_MAPPER>|null $style
* @param array<key-of<AttributeFormat::INTL_MAPPER>, int|float|key-of<RoundingMode::INTL_MAPPER>|key-of<PaddingPosition::INTL_MAPPER>> $attrs
*/
public function newIntlMoneyFormatter(?string $locale = null, StyleFormat|string $style = null, array $attrs = []): IntlMoneyFormatter
{
$locale = $locale ?? Locale::getDefault();
$hash = $locale.'|'.json_encode($style).'|'.json_encode($attrs);
static $instances = [];
if (!isset($instances[$hash])) {
$instances[$hash] = new IntlMoneyFormatter(
$this->numberFactory->createNumberFormatter($locale, $style, $attrs),
new ISOCurrencies()
);
}

return $instances[$hash];
}
}
44 changes: 44 additions & 0 deletions src/Laravel/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Bakame\Intl\Laravel;

use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Artisan;
use Money\Money;
use Orchestra\Testbench\TestCase as LaravelTestCase;

final class FactoryTest extends LaravelTestCase
{
public function setUp(): void
{
parent::setUp();

Artisan::call('cache:clear');
}

/**
* @param Application $app
*
* @return array<int, class-string>
*/
protected function getPackageProviders($app): array
{
return [
Provider::class,
];
}

/** @test */
public function it_can_format_a_money_object(): void
{
$money = Money::EUR(250);

self::assertSame('2,50 €', IntlFactory::newIntlMoneyFormatter('fr', 'currency')->format($money));
self::assertSame('2,50 €', format_currency($money, null, 'fr'));

self::assertSame('deux virgule cinq', IntlFactory::newIntlMoneyFormatter('fr', 'spellout')->format($money));
self::assertSame('deux virgule cinq', format_number($money, 'fr', 'default', [], 'spellout'));
}
}
Loading