Skip to content

Commit

Permalink
Datetime from timestamp (#361)
Browse files Browse the repository at this point in the history
* wip: datetime from timestamp
for #360

* wip: suppress excessive class complexity

* test: test getting datetime from timestamp
  • Loading branch information
g105b committed Oct 11, 2023
1 parent ceac0a6 commit 727750a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Fetchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PDOStatement;
use Gt\Database\Result\ResultSet;

/** @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */
class SqlQuery extends Query {
const SPECIAL_BINDINGS = [
"limit",
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 10 additions & 3 deletions test/phpunit/Result/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 727750a

Please sign in to comment.