Skip to content

Commit 088f311

Browse files
committed
do not allow negative leading zeros
1 parent 1a23f0e commit 088f311

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Number.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private static function parseIntegerPart(string $number): string
248248
throw new InvalidArgumentException('Leading zeros are not allowed');
249249
}
250250

251-
$nonZero = true;
251+
$nonZero = $digit !== '-';
252252
}
253253

254254
return $number;

tests/MoneyTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,18 @@ public function itThrowsWithDecimal(): void
436436
new Money('5.1', new Currency(self::CURRENCY));
437437
}
438438

439+
/**
440+
* @phpstan-param numeric-string $amount
441+
*
442+
* @test
443+
* @dataProvider leadingZeroExamples
444+
*/
445+
public function itThrowsWithLeadingZeros($amount): void
446+
{
447+
$this->expectException(InvalidArgumentException::class);
448+
new Money($amount, new Currency(self::CURRENCY));
449+
}
450+
439451
/**
440452
* @test
441453
*/
@@ -635,4 +647,19 @@ public static function roundToUnitExamples(): array
635647
[10, 2, 0, Money::ROUND_HALF_UP],
636648
];
637649
}
650+
651+
/**
652+
* @phpstan-return non-empty-list<array{
653+
* numeric-string
654+
* }>
655+
*/
656+
public static function leadingZeroExamples(): array
657+
{
658+
return [
659+
['01'],
660+
['001'],
661+
['-01'],
662+
['-001'],
663+
];
664+
}
638665
}

0 commit comments

Comments
 (0)