Skip to content

Commit

Permalink
fix and add test for precision 0
Browse files Browse the repository at this point in the history
fix typo percision to precision
  • Loading branch information
riccardonar authored and goetas committed Jan 6, 2023
1 parent 0a242c4 commit 9eceb7e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 52 deletions.
11 changes: 7 additions & 4 deletions doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,13 @@ Available Types:
| | Rounding Mode. |
| | (HALF_UP, HALF_DOWN, HALF_EVEN HALF_ODD) |
+------------------------------------------------------------+--------------------------------------------------+
| double<2, 'HALF_DOWN', 2> or float<2, 'HALF_DOWN', 2> | Primitive double with percision, |
| double<2, 'HALF_DOWN', 3> or float<2, 'HALF_DOWN', 3> | Rounding Mode and fixed decimals (default 1). |
| | (HALF_UP, HALF_DOWN, HALF_EVEN HALF_ODD) |
| | NOTE: for json the value is cast to string |
| double<2, 'HALF_DOWN', 2> or float<2, 'HALF_DOWN', 2> | Primitive double with precision, |
| double<2, 'HALF_DOWN', 3> or float<2, 'HALF_DOWN', 3> | Rounding Mode and decimals padding up to |
| | N digits. As example, the float ``1.23456`` when |
| | specified as ``double<2, 'HALF_DOWN', 5>`` will |
| | be serialized as ``1.23000``. |
| | NOTE: this is available only for the XML |
| | serializer. |
+------------------------------------------------------------+--------------------------------------------------+
| string | Primitive string |
+------------------------------------------------------------+--------------------------------------------------+
Expand Down
17 changes: 6 additions & 11 deletions src/JsonSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,15 @@ public function visitInteger(int $data, array $type)
*/
public function visitDouble(float $data, array $type)
{
$dataResult = $data;
$percision = $type['params'][0] ?? null;
if ($percision) {
$roundMode = $type['params'][1] ?? null;
$roundMode = $this->mapRoundMode($roundMode);
$dataResult = round($dataResult, $percision, $roundMode);
$precision = $type['params'][0] ?? null;
if (!is_int($precision)) {
return $data;
}

$decimalsNumbers = $type['params'][2] ?? null;
if ($decimalsNumbers !== null) {
$dataResult = number_format($dataResult, $decimalsNumbers, '.', '');
}
$roundMode = $type['params'][1] ?? null;
$roundMode = $this->mapRoundMode($roundMode);

return $dataResult;
return round($data, $precision, $roundMode);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/XmlSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,22 @@ public function visitInteger(int $data, array $type)
public function visitDouble(float $data, array $type)
{
$dataResult = $data;
$percision = $type['params'][0] ?? null;
if ($percision) {
$precision = $type['params'][0] ?? null;
if (is_int($precision)) {
$roundMode = $type['params'][1] ?? null;
$roundMode = $this->mapRoundMode($roundMode);
$dataResult = round($dataResult, $percision, $roundMode);
$dataResult = round($dataResult, $precision, $roundMode);
}

$decimalsNumbers = $type['params'][2] ?? null;
if ($decimalsNumbers === null) {
if (null === $decimalsNumbers) {
$parts = explode('.', (string) $dataResult);
if (count($parts) < 2 || !$parts[1]) {
$decimalsNumbers = 1;
}
}
if ($decimalsNumbers !== null) {

if (null !== $decimalsNumbers) {
$dataResult = number_format($dataResult, $decimalsNumbers, '.', '');
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/ObjectWithFloatProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class ObjectWithFloatProperty
#[Type(name: 'float')]
private $floatingPointUnchanged;

/**
* @Type("float<0>")
* @var float
*/
#[Type(name: 'float<0>')]
private $floatingPointPrecisionZero;

/**
* @Type("float<2,'HALF_DOWN'>")
* @var float
Expand Down Expand Up @@ -66,6 +73,7 @@ class ObjectWithFloatProperty

public function __construct(
float $floatingPointUnchanged,
float $floatingPointPrecisionZero,
float $floatingPointHalfDown,
float $floatingPointHalfEven,
float $floatingPointHalfOdd,
Expand All @@ -75,6 +83,7 @@ public function __construct(
float $floatingPointFixedDecimalsMore
) {
$this->floatingPointUnchanged = $floatingPointUnchanged;
$this->floatingPointPrecisionZero = $floatingPointPrecisionZero;
$this->floatingPointHalfDown = $floatingPointHalfDown;
$this->floatingPointHalfEven = $floatingPointHalfEven;
$this->floatingPointHalfOdd = $floatingPointHalfOdd;
Expand Down
31 changes: 0 additions & 31 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use JMS\Serializer\Tests\Fixtures\AuthorList;
use JMS\Serializer\Tests\Fixtures\FirstClassMapCollection;
use JMS\Serializer\Tests\Fixtures\ObjectWithEmptyArrayAndHash;
use JMS\Serializer\Tests\Fixtures\ObjectWithFloatProperty;
use JMS\Serializer\Tests\Fixtures\ObjectWithInlineArray;
use JMS\Serializer\Tests\Fixtures\Tag;
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
Expand Down Expand Up @@ -446,36 +445,6 @@ public function testTypeHintedArrayAndStdClassSerialization(array $array, $expec
self::assertEquals($expected, $this->serialize($array, $context));
}

public function testSerialisationWithPercisionForFloat(): void
{
$objectWithFloat = new ObjectWithFloatProperty(
1.555555555,
1.555,
1.15,
1.15,
1.555,
1.5,
1.555,
1.555
);

$result = $this->serialize($objectWithFloat, SerializationContext::create());

static::assertEquals(
'{'
. '"floating_point_unchanged":1.555555555,'
. '"floating_point_half_down":1.55,'
. '"floating_point_half_even":1.2,'
. '"floating_point_half_odd":1.1,'
. '"floating_point_half_up":1.56,'
. '"floating_point_fixed_decimals":"1.50",'
. '"floating_point_fixed_decimals_less":"1.6",'
. '"floating_point_fixed_decimals_more":"1.560"'
. '}',
$result
);
}

protected function getFormat()
{
return 'json';
Expand Down
4 changes: 3 additions & 1 deletion tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,10 @@ public function testDoubleEncoding()
setlocale(LC_ALL, $locale);
}

public function testSerialisationWithPercisionForFloat(): void
public function testSerialisationWithPrecisionForFloat(): void
{
$objectWithFloat = new ObjectWithFloatProperty(
1.555555555,
1.555555555,
1.555,
1.15,
Expand All @@ -589,6 +590,7 @@ public function testSerialisationWithPercisionForFloat(): void
'<?xml version="1.0" encoding="UTF-8"?>
<result>
<floating_point_unchanged>1.555555555</floating_point_unchanged>
<floating_point_precision_zero>2.0</floating_point_precision_zero>
<floating_point_half_down>1.55</floating_point_half_down>
<floating_point_half_even>1.2</floating_point_half_even>
<floating_point_half_odd>1.1</floating_point_half_odd>
Expand Down

0 comments on commit 9eceb7e

Please sign in to comment.