v2 RC1 Changes (#7)

- Changes `items` and `hasItems` on `DocumentList` to be properties
- Updates dependent option/result library, which contains similar changes

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2024-10-02 01:37:08 +00:00
parent df436c9ef4
commit 486028bd40
22 changed files with 266 additions and 247 deletions

View File

@@ -89,7 +89,7 @@ class Custom
*/
public static function array(string $query, array $parameters, Mapper $mapper): array
{
return iterator_to_array(self::list($query, $parameters, $mapper)->items());
return iterator_to_array(self::list($query, $parameters, $mapper)->items);
}
/**

View File

@@ -44,42 +44,36 @@ 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);
/** @var bool True if there are items still to be retrieved from the list, false if not */
public bool $hasItems {
get => !is_null($this->result);
}
/**
* The items from the query result
*
* @return Generator<TDoc> The items from the document list
* @var 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');
public Generator $items {
get {
if (!$this->result) {
if ($this->isConsumed) {
throw new DocumentException('Cannot call items() multiple times');
}
$this->isConsumed = true;
return;
}
if (!$this->first) {
$this->isConsumed = true;
$this->result = null;
return;
}
yield $this->first;
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
yield $this->mapper->map($row);
}
$this->isConsumed = true;
return;
$this->result = null;
}
if (!$this->first) {
$this->isConsumed = true;
$this->result = null;
return;
}
yield $this->first;
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
yield $this->mapper->map($row);
}
$this->isConsumed = true;
$this->result = null;
}
/**
@@ -92,7 +86,7 @@ class DocumentList
*/
public function map(callable $map): Generator
{
foreach ($this->items() as $item) {
foreach ($this->items as $item) {
yield $map($item);
}
}
@@ -105,7 +99,7 @@ class DocumentList
*/
public function iter(callable $f): void
{
foreach ($this->items() as $item) {
foreach ($this->items as $item) {
$f($item);
}
}
@@ -122,7 +116,7 @@ class DocumentList
public function mapToArray(callable $keyFunc, callable $valueFunc): array
{
$results = [];
foreach ($this->items() as $item) {
foreach ($this->items as $item) {
$results[$keyFunc($item)] = $valueFunc($item);
}
return $results;