Skip to content

Money class accepts leading zeros for negative numbers #808

Open
@Joris-van-der-Wel

Description

@Joris-van-der-Wel

When I attempt to construct a Money instance by passing a value such as "01", anInvalidArgumentException will be thrown with the message "Leading zeros are not allowed". However, if I pass a value such as "-01" no exception is thrown and the Money class is created in an invalid state.

Example:

$ git clone https://github.com/moneyphp/money.git
$ cd money
$ git show HEAD
commit 51ebdac0f5337550bcb1876f2e84755da92b56b8 (HEAD -> master, origin/master, origin/HEAD)
Merge: e82ae81 37cc563
Author: Frederik Bosch <[email protected]>
Date:   Mon Nov 25 09:41:53 2024 +0100

$ composer install
$ php -a

php > require 'vendor/autoload.php';

php > var_dump(Money\Money::EUR('01'));
PHP Warning:  Uncaught InvalidArgumentException: Leading zeros are not allowed in /projects/money/src/Number.php:248

php > var_dump(Money\Money::EUR('-01'));
object(Money\Money)#6 (2) {
  ["amount":"Money\Money":private]=>
  string(3) "-01"
  ["currency":"Money\Money":private]=>
  object(Money\Currency)#4 (1) {
    ["code":"Money\Currency":private]=>
    string(3) "EUR"
  }
}

Thanks for maintaining 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions