Skip to content

Commit 1b23a85

Browse files
committed
Added Blade directives to handle money
1 parent 7ca7d21 commit 1b23a85

13 files changed

+214
-21
lines changed

.travis.yml

-16
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,10 @@ cache:
66

77
matrix:
88
include:
9-
- php: 7.2
10-
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-lowest'
11-
- php: 7.2
12-
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-stable'
13-
- php: 7.3
14-
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-lowest'
15-
- php: 7.3
16-
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-stable'
179
- php: 7.4
1810
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-lowest'
1911
- php: 7.4
2012
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-stable'
21-
- php: 7.2
22-
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-lowest'
23-
- php: 7.2
24-
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-stable'
25-
- php: 7.3
26-
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-lowest'
27-
- php: 7.3
28-
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-stable'
2913
- php: 7.4
3014
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-lowest'
3115
- php: 7.4

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
],
1818
"require": {
1919
"php": "^7.4",
20-
"illuminate/mail": "^6.0 || ^7.0",
21-
"illuminate/view": "^6.0 || ^7.0"
20+
"illuminate/support": "^6.0 || ^7.0"
2221
},
2322
"require-dev": {
23+
"moneyphp/money": "^3.3",
2424
"orchestra/testbench": "^4.0 || ^5.0",
2525
"phpunit/phpunit": "^8.5"
2626
},
27+
"suggest": {
28+
"moneyphp/money": "Represent money"
29+
},
2730
"autoload": {
2831
"psr-4": {
2932
"ProtoneMedia\\LaravelMixins\\": "src"

src/Blade/BladeDirectiveHelpers.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelMixins\Blade;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
7+
trait BladeDirectiveHelpers
8+
{
9+
public static function parseExpression(string $expression, array $defaults = []): array
10+
{
11+
$parts = explode(',', Blade::stripParentheses($expression));
12+
13+
foreach ($defaults as $key => $default) {
14+
$parts[$key] = isset($parts[$key]) ? trim($parts[$key]) : $default;
15+
}
16+
17+
return $parts;
18+
}
19+
}

src/Blade/DecimalMoneyFormatter.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelMixins\Blade;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Money\Currencies\ISOCurrencies;
7+
use Money\Formatter\DecimalMoneyFormatter as BaseFormatter;
8+
use Money\Money;
9+
10+
class DecimalMoneyFormatter
11+
{
12+
use BladeDirectiveHelpers;
13+
14+
public static function handler(int $cents, string $code)
15+
{
16+
$formatter = new BaseFormatter(new ISOCurrencies);
17+
18+
$money = Money::$code($cents);
19+
20+
return $formatter->format($money);
21+
}
22+
23+
public static function directive(string $name = 'decimals', string $code = 'EUR')
24+
{
25+
Blade::directive($name, function ($expression) use ($code) {
26+
$parts = static::parseExpression($expression, [1 => "'{$code}'"]);
27+
28+
return "<?php echo \ProtoneMedia\LaravelMixins\Blade\DecimalMoneyFormatter::handler($parts[0], $parts[1]) ?>";
29+
});
30+
}
31+
}

src/Blade/IntlMoneyFormatter.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelMixins\Blade;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Money\Currencies\ISOCurrencies;
7+
use Money\Formatter\IntlMoneyFormatter as BaseFormatter;
8+
use Money\Money;
9+
use NumberFormatter;
10+
11+
class IntlMoneyFormatter
12+
{
13+
use BladeDirectiveHelpers;
14+
15+
public static function handler(int $cents, string $code, string $locale)
16+
{
17+
$formatter = new BaseFormatter(
18+
new NumberFormatter($locale, NumberFormatter::CURRENCY),
19+
new ISOCurrencies
20+
);
21+
22+
$money = Money::$code($cents);
23+
24+
return $formatter->format($money);
25+
}
26+
27+
public static function directive(string $name = 'money', string $code = 'EUR', string $locale = 'nl_NL')
28+
{
29+
Blade::directive($name, function ($expression) use ($code, $locale) {
30+
$parts = static::parseExpression($expression, [
31+
1 => "'{$code}'",
32+
2 => "'{$locale}'",
33+
]);
34+
35+
return "<?php echo \ProtoneMedia\LaravelMixins\Blade\IntlMoneyFormatter::handler($parts[0], $parts[1], $parts[2]) ?>";
36+
});
37+
}
38+
}

src/Blade/TestsBladeComponents.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelMixins\Blade;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Facades\Artisan;
7+
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Support\Facades\View;
9+
10+
trait TestsBladeComponents
11+
{
12+
protected function setViewPath($path)
13+
{
14+
Config::set('view.paths', Arr::wrap($path));
15+
}
16+
17+
protected function renderView(string $view, array $data = [])
18+
{
19+
Artisan::call('view:clear');
20+
21+
$view = View::make($view, $data);
22+
23+
return trim((string) ($view));
24+
}
25+
}

tests/Blade/DecimalMoneyFormatter.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Protonemedia\Mixins\Tests\Blade;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use ProtoneMedia\LaravelMixins\Blade\DecimalMoneyFormatter;
7+
use ProtoneMedia\LaravelMixins\Blade\TestsBladeComponents;
8+
9+
class DecimalMoneyFormatterTest extends TestCase
10+
{
11+
use TestsBladeComponents;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->setViewPath(__DIR__ . '/money');
18+
19+
DecimalMoneyFormatter::directive();
20+
}
21+
22+
/** @test */
23+
public function it_can_format_cents_in_decimals()
24+
{
25+
$this->assertEquals('0.99', $this->renderView('decimals', ['cents' => 99]));
26+
$this->assertEquals('1.00', $this->renderView('decimals', ['cents' => 100]));
27+
$this->assertEquals('100', $this->renderView('decimals', ['cents' => 100, 'code' => 'XTS']));
28+
29+
// or set a default
30+
DecimalMoneyFormatter::directive('decimals', 'XTS');
31+
$this->assertEquals('100', $this->renderView('decimals', ['cents' => 100]));
32+
}
33+
}

tests/Blade/IntlFormatterTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Protonemedia\Mixins\Tests\Blade;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use ProtoneMedia\LaravelMixins\Blade\IntlMoneyFormatter;
7+
use ProtoneMedia\LaravelMixins\Blade\TestsBladeComponents;
8+
9+
class IntlFormatterTest extends TestCase
10+
{
11+
use TestsBladeComponents;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->setViewPath(__DIR__ . '/money');
18+
19+
IntlMoneyFormatter::directive();
20+
}
21+
22+
/** @test */
23+
public function it_has_a_blade_directive_to_format_money()
24+
{
25+
// @money(99)
26+
// $this->assertEquals('€ 0,99', $this->renderView('intl', ['cents' => 99]));
27+
// $this->assertEquals('€ 1,00', $this->renderView('intl', ['cents' => 100]));
28+
// $this->assertEquals('€ 1.000,00', $this->renderView('intl', ['cents' => 100 * 1000]));
29+
30+
// @money(99, 'USD')
31+
$this->assertEquals('US$ 0,99', $this->renderView('intl', ['cents' => 99, 'code' => 'USD']));
32+
$this->assertEquals('US$ 1,00', $this->renderView('intl', ['cents' => 100, 'code' => 'USD']));
33+
$this->assertEquals('US$ 1.000,00', $this->renderView('intl', ['cents' => 100 * 1000, 'code' => 'USD']));
34+
35+
// or set a default
36+
IntlMoneyFormatter::directive('money', 'USD');
37+
$this->assertEquals('US$ 0,99', $this->renderView('intl', ['cents' => 99]));
38+
39+
// @money(99, 'USD', 'en')
40+
$this->assertEquals('$0.99', $this->renderView('intl', ['cents' => 99, 'code' => 'USD', 'locale' => 'en']));
41+
$this->assertEquals('1,00 $', $this->renderView('intl', ['cents' => 100, 'code' => 'USD', 'locale' => 'de']));
42+
$this->assertEquals('1 000,00 $US', $this->renderView('intl', ['cents' => 100 * 1000, 'code' => 'USD', 'locale' => 'fr']));
43+
44+
// or set a default
45+
IntlMoneyFormatter::directive('money', 'USD', 'fr');
46+
$this->assertEquals('1 000,00 $US', $this->renderView('intl', ['cents' => 100 * 1000]));
47+
}
48+
}

tests/Blade/money/decimals.blade.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@isset($code)
2+
@decimals($cents, $code)
3+
@else
4+
@decimals($cents)
5+
@endisset

tests/Blade/money/intl.blade.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@if(isset($code) && isset($locale))
2+
@money($cents, $code, $locale)
3+
@elseif(isset($code))
4+
@money($cents, $code)
5+
@else
6+
@money($cents)
7+
@endif

tests/String/CompactTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Protonemedia\Mixins\Tests;
3+
namespace Protonemedia\Mixins\Tests\String;
44

55
use Illuminate\Support\Str;
66
use Orchestra\Testbench\TestCase;

tests/String/HumanFilesizeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Protonemedia\Mixins\Tests;
3+
namespace Protonemedia\Mixins\Tests\String;
44

55
use Illuminate\Support\Str;
66
use Orchestra\Testbench\TestCase;

tests/UrlTest.php renamed to tests/String/UrlTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Protonemedia\Mixins\Tests;
3+
namespace Protonemedia\Mixins\Tests\String;
44

55
use Illuminate\Support\Str;
66
use Orchestra\Testbench\TestCase;

0 commit comments

Comments
 (0)