From 66de02b6e2df93e4cae26d8a3af4a348169c52e5 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 11 Oct 2023 19:08:12 +0100 Subject: [PATCH 1/3] wip: datetime from timestamp for #360 --- src/Fetchable.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Fetchable.php b/src/Fetchable.php index aee8cf1..5ea58ab 100644 --- a/src/Fetchable.php +++ b/src/Fetchable.php @@ -144,6 +144,10 @@ protected function castRow( $key = key($assocArray); $value = $assocArray[$key]; + if($type === Type::DATETIME && is_numeric($value)) { + $value = "@$value"; + } + return match ($type) { Type::BOOL, "boolean" => (bool)$value, Type::INT, "integer" => (int)$value, From f4c10de7827d7f401128005e0a6c0e9387c7d0a0 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 11 Oct 2023 23:47:24 +0100 Subject: [PATCH 2/3] wip: suppress excessive class complexity --- src/Query/SqlQuery.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Query/SqlQuery.php b/src/Query/SqlQuery.php index 6bcf6c8..0435a3e 100644 --- a/src/Query/SqlQuery.php +++ b/src/Query/SqlQuery.php @@ -7,6 +7,7 @@ use PDOStatement; use Gt\Database\Result\ResultSet; +/** @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class SqlQuery extends Query { const SPECIAL_BINDINGS = [ "limit", @@ -204,7 +205,7 @@ private function injectDynamicOr(string $sql, array &$data):string { } $replacementString = ""; - foreach($data["__dynamicOr"] as $i => $kvp) { + foreach($data["__dynamicOr"] as $kvp) { $conditionString = ""; foreach($kvp as $key => $value) { if(is_string($value)) { From 0a6f9e05406eb9af2d1c7ada9e6ac96ae9ee4be4 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 12 Oct 2023 00:01:50 +0100 Subject: [PATCH 3/3] test: test getting datetime from timestamp --- test/phpunit/Result/ResultSetTest.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/phpunit/Result/ResultSetTest.php b/test/phpunit/Result/ResultSetTest.php index 72572d4..d060954 100644 --- a/test/phpunit/Result/ResultSetTest.php +++ b/test/phpunit/Result/ResultSetTest.php @@ -8,9 +8,9 @@ class ResultSetTest extends TestCase { const FAKE_DATA = [ - ["id" => 1, "name" => "Alice"], - ["id" => 2, "name" => "Bob"], - ["id" => 3, "name" => "Charlie"], + ["id" => 1, "name" => "Alice", "timestamp" => 576264240, "date" => "1988-04-05 17:24"], + ["id" => 2, "name" => "Bob", "timestamp" => 554900700, "date" => "1987-08-02 11:05"], + ["id" => 3, "name" => "Charlie", "timestamp" => 1433548800, "date" => "2015-06-06"], ]; private $fake_data_index = 0; @@ -130,6 +130,13 @@ public function testAsArray() { } } + public function testAsDateTime() { + $resultSet = new ResultSet($this->getStatementMock()); + $row = $resultSet->fetch(); + self::assertEquals("1988-04-05 17:24", $row->getDateTime("date")->format("Y-m-d H:i")); + self::assertEquals("1988-04-05 17:24", $row->getDateTime("timestamp")->format("Y-m-d H:i")); + } + private function getStatementMock():PDOStatement { $statement = $this->createMock(PDOStatement::class); $statement->method("fetch")