Skip to content

Commit

Permalink
Result::fetchAssoc() WIP #357
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 28, 2020
1 parent 34e1603 commit 348d61f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ final public function fetchAssoc(string $assoc): array
}

$this->seek(0);
$row = $this->fetch();
$driver = $this->getResultDriver();
$row = $driver->fetch(true);
if (!$row) {
return []; // empty result set
}
Expand All @@ -246,7 +247,7 @@ final public function fetchAssoc(string $assoc): array
// check columns
foreach ($assoc as $as) {
// offsetExists ignores null in PHP 5.2.1, isset() surprisingly null accepts
if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !property_exists($row, $as)) {
if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !array_key_exists($as, $row)) {
throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor.");
}
}
Expand All @@ -261,6 +262,7 @@ final public function fetchAssoc(string $assoc): array

// make associative tree
do {
$this->normalize($row);
$x = &$data;

// iterative deepening
Expand All @@ -269,27 +271,33 @@ final public function fetchAssoc(string $assoc): array
$x = &$x[];

} elseif ($as === '=') { // "value" node
$x = $row->{$assoc[$i + 1]};
$x = $row[$assoc[$i + 1]];
continue 2;

} elseif ($as === '->') { // "object" node
if ($x === null) {
$x = clone $row;
$x = new $this->rowClass($row);
$x = &$x->{$assoc[$i + 1]};
$x = null; // prepare child node
} else {
$x = &$x->{$assoc[$i + 1]};
}

} elseif ($as !== '|') { // associative-array node
$x = &$x[$row->$as];
$x = &$x[$row[$as]];
}
}

if ($x === null) { // build leaf
$x = $row;
if ($this->rowFactory) {
$x = ($this->rowFactory)($row);
} elseif ($this->rowClass) {
$x = new $this->rowClass($row);
} else {
$x = $row;
}
}
} while ($row = $this->fetch());
} while ($row = $driver->fetch(true));

unset($x);
return $data;
Expand Down

0 comments on commit 348d61f

Please sign in to comment.