Add map and iter to doc list
This commit is contained in:
@@ -24,6 +24,9 @@ class DocumentList
|
||||
/** @var TDoc|null $first The first item from the results */
|
||||
private mixed $first = null;
|
||||
|
||||
/** @var bool $isConsumed This is set to true once the generator has been exhausted */
|
||||
private bool $isConsumed = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -39,6 +42,75 @@ class DocumentList
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this list have items remaining?
|
||||
*
|
||||
* @return bool True if there are items still to be retrieved from the list, false if not
|
||||
*/
|
||||
public function hasItems(): bool
|
||||
{
|
||||
return !is_null($this->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* The items from the query result
|
||||
*
|
||||
* @return Generator<TDoc> The items from the document list
|
||||
* @throws DocumentException If this is called once the generator has been consumed
|
||||
*/
|
||||
public function items(): Generator
|
||||
{
|
||||
if (!$this->result) {
|
||||
if ($this->isConsumed) {
|
||||
throw new DocumentException('Cannot call items() multiple times');
|
||||
}
|
||||
$this->isConsumed = true;
|
||||
return;
|
||||
}
|
||||
yield $this->first;
|
||||
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
|
||||
yield $this->mapper->map($row);
|
||||
}
|
||||
$this->isConsumed = true;
|
||||
$this->result = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map items by consuming the generator
|
||||
*
|
||||
* @template U The type to which each item should be mapped
|
||||
* @param callable(TDoc): U $map The mapping function
|
||||
* @return Generator<U> The result of the mapping function
|
||||
* @throws DocumentException If this is called once the generator has been consumed
|
||||
*/
|
||||
public function map(callable $map): Generator
|
||||
{
|
||||
foreach ($this->items() as $item) {
|
||||
yield $map($item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate the generator, running the given function for each item
|
||||
*
|
||||
* @param callable(TDoc): void $f The function to run for each item
|
||||
* @throws DocumentException If this is called once the generator has been consumed
|
||||
*/
|
||||
public function iter(callable $f): void
|
||||
{
|
||||
foreach ($this->items() as $item) {
|
||||
$f($item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the statement is destroyed if the generator is not exhausted
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if (!is_null($this->result)) $this->result = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new document list
|
||||
*
|
||||
@@ -53,37 +125,4 @@ class DocumentList
|
||||
$stmt = &Custom::runQuery($query, $parameters);
|
||||
return new static($stmt, $mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* The items from the query result
|
||||
*
|
||||
* @return Generator<TDoc> The items from the document list
|
||||
*/
|
||||
public function items(): Generator
|
||||
{
|
||||
if (!$this->result) return;
|
||||
yield $this->first;
|
||||
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
|
||||
yield $this->mapper->map($row);
|
||||
}
|
||||
$this->result = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this list have items remaining?
|
||||
*
|
||||
* @return bool True if there are items still to be retrieved from the list, false if not
|
||||
*/
|
||||
public function hasItems(): bool
|
||||
{
|
||||
return !is_null($this->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the statement is destroyed if the generator is not exhausted
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if (!is_null($this->result)) $this->result = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,8 @@ class Parameters
|
||||
$mode = Configuration::mode('generate field name parameters');
|
||||
return match ($mode) {
|
||||
Mode::PgSQL => [$paramName => "{" . implode(",", $fieldNames) . "}"],
|
||||
Mode::SQLite => array_combine(array_map(fn($idx) => $paramName . $idx, range(0, sizeof($fieldNames) - 1)),
|
||||
Mode::SQLite => array_combine(array_map(fn($idx) => $paramName . $idx,
|
||||
empty($fieldNames) ? [] : range(0, sizeof($fieldNames) - 1)),
|
||||
array_map(fn($field) => "$.$field", $fieldNames))
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user