Skip to content

Commit

Permalink
feat: improve TypeMoney with an export to JSON method
Browse files Browse the repository at this point in the history
  • Loading branch information
Alcides Ramos committed May 20, 2024
1 parent b7bd2e7 commit 4110201
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 34 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions src/TypeMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ public function __invoke(): array
return [$this->amount, $this->currency];
}

public function toJson(): string
public function toJson(): string|false
{
return json_encode([
($this->amount)(),
$this->currency->value
]);
}

public static function fromJson(string $data): self
{
[$amount, $currency] = json_decode($data, true);
return self::set($amount, $currency);
}
}
22 changes: 0 additions & 22 deletions tests/Unit/TypeMoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,4 @@ public static function dataProviderForToJson(): array
'Negative' => [TypeMoney::set(-123.45, 'USD'), '[-123.45,"USD"]'],
];
}

// ---------------------------------------------------------------------------------------------------------------

#[Test]
#[DataProvider('dataProviderForFromJson')]
public function checkFromJson(string $data, TypeMoney $expected): void
{
$aux = TypeMoney::fromJson($data);

self::assertTrue($aux->isEqualTo($expected));
}

/**
* @return array<string, array<int, mixed>>
*/
public static function dataProviderForFromJson(): array
{
return [
'Positive' => ['[123.45,"EUR"]', TypeMoney::set(123.45, 'EUR')],
'Negative' => ['[-123.45,"USD"]', TypeMoney::set(-123.45, 'USD')],
];
}
}

0 comments on commit 4110201

Please sign in to comment.