Commit cf31a7e Melvin Drost
authored
1 parent e288f8e commit cf31a7e Copy full SHA for cf31a7e
File tree 3 files changed +20
-2
lines changed
3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 17
17
],
18
18
"require" : {
19
19
"php" : " ^7.4|^8.0" ,
20
- "ext-bcmath" : " *"
20
+ "ext-bcmath" : " *" ,
21
+ "ext-json" : " *"
21
22
},
22
23
"require-dev" : {
23
24
"friendsofphp/php-cs-fixer" : " ^3.0" ,
Original file line number Diff line number Diff line change 9
9
use MadeByBob \Number \Exception \InvalidNumberInputTypeException ;
10
10
use MadeByBob \Number \Exception \InvalidRoundingModeException ;
11
11
12
- abstract class AbstractNumber
12
+ abstract class AbstractNumber implements \JsonSerializable
13
13
{
14
14
protected const INTERNAL_SCALE = 12 ;
15
15
protected const DEFAULT_SCALE = 4 ;
@@ -541,6 +541,14 @@ public function __toString(): string
541
541
return $ this ->toString ();
542
542
}
543
543
544
+ /**
545
+ * Converts the current MadeByBob\Number instance into a string.
546
+ */
547
+ public function jsonSerialize ()
548
+ {
549
+ return $ this ->toString ();
550
+ }
551
+
544
552
/**
545
553
* @internal Provides value with internal scale.
546
554
*/
Original file line number Diff line number Diff line change @@ -699,4 +699,13 @@ public function testFormatNumber(): void
699
699
$ number = new Number ('5943.000000 ' );
700
700
$ this ->assertEquals ('5.943 ' , $ number ->format (0 , 0 ));
701
701
}
702
+
703
+ public function testCanJsonSerialize (): void
704
+ {
705
+ $ number = new Number ('9342.1557 ' );
706
+ $ array = ['number ' => $ number ];
707
+
708
+ $ this ->assertEquals ('{"number":"9342.1557"} ' , json_encode ($ array ));
709
+ $ this ->assertEquals ('"9342.1557" ' , json_encode ($ number ));
710
+ }
702
711
}
You can’t perform that action at this time.
0 commit comments