From 9d802e75ba69f701ca06ddd213655bd3b744c6b9 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Thu, 22 Aug 2019 19:01:20 +0900 Subject: [PATCH] Accept 0 as @int value --- src/Query.php | 4 +++- tests/QueryTest.php | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Query.php b/src/Query.php index b72775c..5e58032 100644 --- a/src/Query.php +++ b/src/Query.php @@ -99,7 +99,9 @@ protected static function replaceHolder($pdo, $key, $type, $value, &$bind_values throw new \DomainException(sprintf('param "%s" is integer out of range.', $key)); } - if (!preg_match('/\A-?[1-9][0-9]*\z/', $s)) { + $is_zero = $s === 0 || $s === '0'; + + if (!$is_zero && !preg_match('/\A-?[1-9][0-9]*\z/', $s)) { throw new \DomainException(); } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 11de6f7..277948a 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -11,12 +11,14 @@ final class QueryTest extends \PHPUnit\Framework\TestCase public function test() { $pdo = new DummyPDO; - $orig = "string: :a@string\nint :b@int\nstring: :a@string\nint :b@int"; + $orig = "string: :a@string\nint :b@int\nstring: :a@string\nint :b@int\nint :c1@int\nint :c1@int"; $stmt = Query::build($pdo, $orig, [ ':a' => 'AAAA', ':b' => '2222', + ':c1' => '0', + ':c2' => 0, ]); - $expected = 'string: @AAAA@ int 2222 string: @AAAA@ int 2222'; + $expected = 'string: @AAAA@ int 2222 string: @AAAA@ int 2222 int 0 int 0'; $this->assertSame($expected, $stmt->queryString); } }