File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace PhpUnitConversion ;
3
3
4
+ use JsonSerializable ;
4
5
use InvalidArgumentException ;
5
6
use PhpUnitConversion \Map as UnitMap ;
6
7
use PhpUnitConversion \Exception \InvocationException ;
7
8
use PhpUnitConversion \Exception \UnsupportedUnitException ;
8
9
use PhpUnitConversion \Exception \UnsupportedConversionException ;
9
10
10
- class Unit
11
+ class Unit implements JsonSerializable
11
12
{
12
13
/** @var float */
13
14
protected $ value ;
@@ -467,4 +468,16 @@ public function __toString()
467
468
$ symbol = $ this ->getSymbol ();
468
469
return (string )$ this ->getValue () . ($ symbol ? ' ' . $ symbol : '' );
469
470
}
471
+
472
+ /**
473
+ * @return array
474
+ */
475
+ public function jsonSerialize ()
476
+ {
477
+ return [
478
+ 'value ' => $ this ->getValue (),
479
+ 'symbol ' => $ this ->getSymbol (),
480
+ 'label ' => $ this ->getLabel (),
481
+ ];
482
+ }
470
483
}
Original file line number Diff line number Diff line change @@ -108,6 +108,20 @@ public function testToString()
108
108
$ this ->assertEquals ('1 u ' , (string )$ unit );
109
109
}
110
110
111
+ public function testToJson ()
112
+ {
113
+ $ unit = new MyUnitType \OneUnit (1 );
114
+
115
+ $ expected = [
116
+ 'value ' => 1 ,
117
+ 'symbol ' => 'u ' ,
118
+ 'label ' => 'unit ' ,
119
+ ];
120
+
121
+ $ this ->assertEquals ($ expected , $ unit ->jsonSerialize ());
122
+ $ this ->assertEquals (json_encode ($ expected ), json_encode ($ unit ));
123
+ }
124
+
111
125
public function testInvalidInvocation ()
112
126
{
113
127
$ this ->expectException (Exception::class);
You can’t perform that action at this time.
0 commit comments