Skip to content

Commit fc86908

Browse files
committed
Reimplement without "preg_replace"
1 parent 4c3258b commit fc86908

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/Types/BigIntType.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99

1010
use function assert;
11+
use function ctype_digit;
1112
use function is_int;
1213
use function is_string;
13-
use function preg_replace;
1414
use function rtrim;
15+
use function str_starts_with;
1516
use function strpos;
1617
use function substr;
1718

@@ -53,13 +54,27 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): int
5354
'DBAL assumes values outside of the integer range to be returned as string by the database driver.',
5455
);
5556

56-
// workaround https://github.com/php/php-src/issues/14345
57+
if (str_starts_with($value, '-') || str_starts_with($value, '+')) {
58+
$hasNegativeSign = str_starts_with($value, '-');
59+
$value = substr($value, 1);
60+
} else {
61+
$hasNegativeSign = false;
62+
}
63+
64+
while (substr($value, 0, 1) === '0' && ctype_digit(substr($value, 1, 1))) {
65+
$value = substr($value, 1);
66+
}
67+
5768
$dotPos = strpos($value, '.');
5869
if ($dotPos !== false && rtrim(substr($value, $dotPos + 1), '0') === '') {
5970
$value = substr($value, 0, $dotPos);
6071
}
6172

62-
if (preg_replace('~^(\+|-(?=0+$))|(?<=^|^[+\-])0+(?=\d)~', '', $value) === (string) (int) $value) {
73+
if ($hasNegativeSign && $value !== '0') {
74+
$value = '-' . $value;
75+
}
76+
77+
if ($value === (string) (int) $value) {
6378
return (int) $value;
6479
}
6580

0 commit comments

Comments
 (0)