From 96a81323b4c115da74e6f32f4c5e0ec1ec4ebd48 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Tue, 24 Sep 2024 21:59:44 +0700 Subject: [PATCH] add bcdivmod --- src/Php84/Php84.php | 19 +++++++++++++++++++ src/Php84/bootstrap.php | 4 ++++ tests/Php84/Php84Test.php | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/Php84/Php84.php b/src/Php84/Php84.php index 1bca70b56..2b33777e9 100644 --- a/src/Php84/Php84.php +++ b/src/Php84/Php84.php @@ -169,4 +169,23 @@ private static function mb_internal_trim(string $regex, string $string, ?string return mb_convert_encoding($string, $encoding, 'UTF-8'); } + + public static function bcdivmod(string $num1, string $num2, ?int $scale = null): array { + if ($num2 === '0') { + throw new \DivisionByZeroError('Division by zero'); + } + + if (!is_numeric($num1)) { + throw new \ValueError('Argument #1 ($num1) is not well-formed'); + } + + if (!is_numeric($num2)) { + throw new \ValueError('Argument #2 ($num2) is not well-formed'); + } + + return [ + \bcdiv($num1, $num2, 0), + \bcmod($num1, $num2, $scale), + ]; + } } diff --git a/src/Php84/bootstrap.php b/src/Php84/bootstrap.php index 342158c63..5a770e3d2 100644 --- a/src/Php84/bootstrap.php +++ b/src/Php84/bootstrap.php @@ -60,3 +60,7 @@ function mb_ltrim(string $string, ?string $characters = null, ?string $encoding function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); } } } + +if (function_exists('bcdiv')) { + function bcdivmod(string $num1, string $num2, ?int $scale = null): array {return p\Php84::bcdivmod($num1, $num2, $scale); } +} diff --git a/tests/Php84/Php84Test.php b/tests/Php84/Php84Test.php index 9d573a186..712356429 100644 --- a/tests/Php84/Php84Test.php +++ b/tests/Php84/Php84Test.php @@ -256,6 +256,16 @@ public function testMbTrimCharactersEncoding() mb_internal_encoding($old); } + /** + * @covers \Symfony\Polyfill\Php84\Php84::bcdivmod + * + * @dataProvider bcDivModProvider + */ + public function testBcDivMod(string $num1, string $num2, ?int $scale = null, array $expected): void + { + $this->assertSame($expected, bcdivmod($num1, $num2, $scale)); + } + public static function mbTrimProvider(): iterable { yield ['ABC', 'ABC']; @@ -319,4 +329,9 @@ public static function mbRTrimProvider(): iterable yield ["foo\n", "foo\n", 'o']; } + + public static function bcDivModProvider(): iterable + { + yield ['10', '10', null, ['1', '0']]; + } }