Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Implemented JsonSerializable #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Litipk\BigNumbers;

use JsonSerializable;
use Litipk\BigNumbers\DecimalConstants as DecimalConstants;

use Litipk\BigNumbers\Errors\InfiniteInputError;
Expand All @@ -14,7 +15,7 @@
*
* @author Andreu Correa Casablanca <castarco@litipk.com>
*/
class Decimal
class Decimal implements JsonSerializable
{
const DEFAULT_SCALE = 16;
const CLASSIC_DECIMAL_NUMBER_REGEXP = '/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/';
Expand Down Expand Up @@ -1129,6 +1130,14 @@ public function __toString(): string
return $this->value;
}

/**
* @return string
*/
public function jsonSerialize(): string
{
return $this->value;
}

/*
*
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/Decimal/DecimalJsonSerialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Litipk\BigNumbers\Decimal as Decimal;
use PHPUnit\Framework\TestCase;

class DecimalJsonSerialize extends TestCase
{
public function testJsonSerialize()
{
$value = '123.456';
$this->assertEquals($value, Decimal::fromString($value)->jsonSerialize());
}

public function testJsonSerializable()
{
$value = '123.456';
$this->assertEquals(json_encode($value), json_encode(Decimal::fromString($value)));
}
}