Skip to content

Commit

Permalink
Nullable primitives (#203)
Browse files Browse the repository at this point in the history
* Nullable primitives

* Add tests for nullable primitives

* Fix precision
  • Loading branch information
g105b authored Feb 16, 2021
1 parent ae67229 commit f8ef292
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/Result/InvalidNullablePrimitiveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Gt\Database\Result;

use Gt\Database\DatabaseException;

class InvalidNullablePrimitiveException extends DatabaseException {}
49 changes: 36 additions & 13 deletions src/Result/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Iterator;

class Row implements Iterator {
/** @var array */
/** @var array<string, string> */
protected $data;
protected $iterator_index = 0;
protected $iterator_data_key_list = [];
Expand All @@ -23,23 +23,23 @@ 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 {
return $this->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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -95,4 +90,32 @@ public function current():?string {
$key = $this->key();
return $this->$key;
}
}

/** @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);
}
}
4 changes: 2 additions & 2 deletions test/phpunit/Migration/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -710,4 +710,4 @@ protected function createFiles(array $files, string $path):void {
touch($pathName);
}
}
}
}
12 changes: 10 additions & 2 deletions test/phpunit/Result/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"], 6), round($float, 6));
}

/** @dataProvider data_getTestRow */
Expand All @@ -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 = [];

Expand Down Expand Up @@ -154,4 +162,4 @@ public function data_getTestRow():array {

return $data;
}
}
}

0 comments on commit f8ef292

Please sign in to comment.