From 5121a4704f263211201295e299fbd7092006394e Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 16 Feb 2021 16:52:27 +0000 Subject: [PATCH 1/3] Nullable primitives --- .../InvalidNullablePrimitiveException.php | 6 +++ src/Result/Row.php | 45 ++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 src/Result/InvalidNullablePrimitiveException.php diff --git a/src/Result/InvalidNullablePrimitiveException.php b/src/Result/InvalidNullablePrimitiveException.php new file mode 100644 index 0000000..339661e --- /dev/null +++ b/src/Result/InvalidNullablePrimitiveException.php @@ -0,0 +1,6 @@ +data[$columnName] ?? null; + return $this->getAsNullablePrimitive($columnName, "string"); } public function getInt(string $columnName):?int { - return (int)($this->data[$columnName] ?? null); + return $this->getAsNullablePrimitive($columnName, "int"); } public function getFloat(string $columnName):?float { - return (float)($this->data[$columnName] ?? null); + return $this->getAsNullablePrimitive($columnName, "float"); } public function getBool(string $columnName):?bool { - return (bool)($this->data[$columnName] ?? null); + return $this->getAsNullablePrimitive($columnName, "bool"); } public function getDateTime(string $columnName):?DateTime { @@ -54,12 +54,7 @@ public function getDateTime(string $columnName):?DateTime { return $dateTime; } - $dateTime = new DateTime($dateString); - if(!$dateTime) { - throw new BadlyFormedDataException($columnName); - } - - return $dateTime; + return new DateTime($dateString); } public function asArray():array { @@ -95,4 +90,32 @@ public function current():?string { $key = $this->key(); return $this->$key; } -} \ No newline at end of file + + /** @return mixed */ + private function getAsNullablePrimitive(string $key, string $type) { + $value = $this->get($key); + if(is_null($value)) { + return null; + } + + switch($type) { + case "string": + return $value; + + case "int": + case "integer": + return (int)$value; + + case "float": + case "double": + case "decimal": + return (float)$value; + + case "bool": + case "boolean": + return (bool)$value; + } + + throw new InvalidNullablePrimitiveException($type); + } +} From 09ccc63b0cbdd79b272b8272577f3713d982f1f5 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 16 Feb 2021 17:05:28 +0000 Subject: [PATCH 2/3] Add tests for nullable primitives --- src/Result/Row.php | 4 ++-- test/phpunit/Result/RowTest.php | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Result/Row.php b/src/Result/Row.php index c6ae8c3..5c0058b 100644 --- a/src/Result/Row.php +++ b/src/Result/Row.php @@ -5,7 +5,7 @@ use Iterator; class Row implements Iterator { - /** @var array */ + /** @var array */ protected $data; protected $iterator_index = 0; protected $iterator_data_key_list = []; @@ -23,7 +23,7 @@ public function __isset(string $name):bool { } public function get(string $columnName):?string { - return $this->getString($columnName); + return $this->data[$columnName] ?? null; } public function getString(string $columnName):?string { diff --git a/test/phpunit/Result/RowTest.php b/test/phpunit/Result/RowTest.php index 2f3ab22..94370a7 100644 --- a/test/phpunit/Result/RowTest.php +++ b/test/phpunit/Result/RowTest.php @@ -92,7 +92,7 @@ public function testGetFloat(array $data) { $row = new Row($data); $float = $row->getFloat("exampleFloat"); self::assertIsFloat($float); - self::assertSame((float)$data["exampleFloat"], $float); + self::assertSame(round($data["exampleFloat"], 7), round($float, 7)); } /** @dataProvider data_getTestRow */ @@ -114,6 +114,14 @@ public function testGetDateTime(array $data) { ); } + public function testGetIntNullable() { + $key = uniqid(); + $value = 123; + $row = new Row([$key => $value]); + self::assertEquals($value, $row->getInt($key)); + self::assertNull($row->getInt("does_not_exist")); + } + public function data_getTestRow():array { $data = []; @@ -154,4 +162,4 @@ public function data_getTestRow():array { return $data; } -} \ No newline at end of file +} From 3c4479dd3a53c98b63ac74298678189fe2c062c8 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 16 Feb 2021 17:13:08 +0000 Subject: [PATCH 3/3] Fix precision --- test/phpunit/Migration/MigratorTest.php | 4 ++-- test/phpunit/Result/RowTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/phpunit/Migration/MigratorTest.php b/test/phpunit/Migration/MigratorTest.php index f30530e..acc8de7 100644 --- a/test/phpunit/Migration/MigratorTest.php +++ b/test/phpunit/Migration/MigratorTest.php @@ -114,7 +114,7 @@ public function testCheckFileListOrderMissing(array $fileList) { $settings = $this->createSettings($path); $migrator = new Migrator($settings, $path); $actualFileList = $migrator->getMigrationFileList(); - $this->expectException(MigrationSequenceOrderException::class); + self::expectException(MigrationSequenceOrderException::class); $migrator->checkFileListOrder($actualFileList); } @@ -710,4 +710,4 @@ protected function createFiles(array $files, string $path):void { touch($pathName); } } -} \ No newline at end of file +} diff --git a/test/phpunit/Result/RowTest.php b/test/phpunit/Result/RowTest.php index 94370a7..508ef48 100644 --- a/test/phpunit/Result/RowTest.php +++ b/test/phpunit/Result/RowTest.php @@ -92,7 +92,7 @@ public function testGetFloat(array $data) { $row = new Row($data); $float = $row->getFloat("exampleFloat"); self::assertIsFloat($float); - self::assertSame(round($data["exampleFloat"], 7), round($float, 7)); + self::assertSame(round($data["exampleFloat"], 6), round($float, 6)); } /** @dataProvider data_getTestRow */