File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ title : Format Money Modifier
3+ description : " The `format_money` modifier converts an amount (in pence) to a nicely formatted string, like `£190.55`."
4+ ---
5+
6+ The ` format_money ` modifier converts an amount (in pence) to a nicely formatted string, like ` £190.55 ` .
7+
8+ ``` php
9+ special_calculation: '19055'
10+ ```
11+
12+ :: tabs
13+ ::tab antlers
14+ ``` antlers
15+ {{ special_calculation | format_money }}
16+ ```
17+ ::tab blade
18+ ``` blade
19+ {{ Statamic::modify($specialCalculation)->formatMoney() }}
20+ ```
21+ ::
22+
23+ ``` php
24+ £190.55
25+ ```
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DuncanMcClean \Cargo \Modifiers ;
4+
5+ use DuncanMcClean \Cargo \Support \Money ;
6+ use Statamic \Facades \Site ;
7+ use Statamic \Modifiers \Modifier ;
8+
9+ class FormatMoney extends Modifier
10+ {
11+ public function index ($ value , $ params , $ context )
12+ {
13+ if (! $ value ) {
14+ return $ value ;
15+ }
16+
17+ if (! is_numeric ($ value )) {
18+ throw new \Exception ('The format_money modifier requires a numeric value. ' );
19+ }
20+
21+ return Money::format ($ value , Site::current ());
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Modifiers ;
4+
5+ use PHPUnit \Framework \Attributes \DataProvider ;
6+ use PHPUnit \Framework \Attributes \Test ;
7+ use Statamic \Modifiers \Modify ;
8+ use Tests \TestCase ;
9+
10+ class FormatMoneyTest extends TestCase
11+ {
12+ public static function amountProvider (): array
13+ {
14+ return [
15+ [null , null ],
16+ ['1599 ' , '£15.99 ' ],
17+ ['15.99 ' , '£15.99 ' ],
18+ [1599 , '£15.99 ' ],
19+ [15.99 , '£15.99 ' ],
20+ ];
21+ }
22+
23+ #[Test]
24+ #[DataProvider('amountProvider ' )]
25+ public function it_formats_money ($ input , $ expected )
26+ {
27+ $ this ->assertEquals ($ expected , $ this ->modify ($ input ));
28+ }
29+
30+ #[Test]
31+ public function exception_is_thrown_when_non_numeric_value_is_provided ()
32+ {
33+ $ this ->expectException (\Exception::class);
34+ $ this ->expectExceptionMessage ('The format_money modifier requires a numeric value. ' );
35+
36+ $ this ->modify ('Hello World! ' );
37+ }
38+
39+ private function modify ($ value )
40+ {
41+ return Modify::value ($ value )->formatMoney ()->fetch ();
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments