Add mapToArray to doc list
This commit is contained in:
@@ -103,6 +103,24 @@ class DocumentList
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate the generator, extracting key/value pairs returned as an associative array
|
||||
*
|
||||
* @template TValue The type for the mapped value
|
||||
* @param callable(TDoc): (int|string) $keyFunc The function to extract a key from the document
|
||||
* @param callable(TDoc): TValue $valueFunc The function to extract a value from the document
|
||||
* @return TValue[] An associative array of values, keyed by the extracted keys
|
||||
* @throws DocumentException If this is called once the generator has been consumed
|
||||
*/
|
||||
public function mapToArray(callable $keyFunc, callable $valueFunc): array
|
||||
{
|
||||
$results = [];
|
||||
foreach ($this->items() as $item) {
|
||||
$results[$keyFunc($item)] = $valueFunc($item);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the statement is destroyed if the generator is not exhausted
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user