améliorer cursor
This commit is contained in:
parent
ef13b8afc9
commit
c0da899b80
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace nulib\php\coll;
|
||||
|
||||
use ArrayAccess;
|
||||
use Iterator;
|
||||
use IteratorAggregate;
|
||||
use nulib\cl;
|
||||
@ -18,7 +19,7 @@ use Traversable;
|
||||
* @property-read array|null $value alias pour $row
|
||||
* @property-read iterable|null $rows la source des lignes
|
||||
*/
|
||||
class Cursor implements Iterator {
|
||||
class Cursor implements Iterator, ArrayAccess {
|
||||
const PARAMS_SCHEMA = [
|
||||
"rows" => ["?iterable"],
|
||||
"rows_func" => ["?callable"],
|
||||
@ -70,10 +71,14 @@ class Cursor implements Iterator {
|
||||
/** un générateur de lignes */
|
||||
private ?Traversable $rowsGenerator;
|
||||
|
||||
/** une fonction de signature <code>function(mixed $rows, Cursor): ?iterable</code> */
|
||||
/**
|
||||
* une fonction de signature <code>function(mixed $rows, Cursor): ?iterable</code>
|
||||
*/
|
||||
private ?func $rowsFunc;
|
||||
|
||||
/** une fonction de signature <code>function(?array $row, Cursor): bool</code> */
|
||||
/**
|
||||
* une fonction de signature <code>function(?array $row, Cursor): bool</code>
|
||||
*/
|
||||
private ?func $filterFunc = null;
|
||||
|
||||
function setFilter(array $filter): self {
|
||||
@ -89,7 +94,9 @@ class Cursor implements Iterator {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** une fonction de signature <code>function(?array $row, Cursor): ?array</code> */
|
||||
/**
|
||||
* une fonction de signature <code>function(?array $row, Cursor): ?array</code>
|
||||
*/
|
||||
private ?func $mapFunc = null;
|
||||
|
||||
function setMap(array $map): self {
|
||||
@ -105,7 +112,9 @@ class Cursor implements Iterator {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** une fonction de signature <code>function(?array $row, Cursor): ?array</code> */
|
||||
/**
|
||||
* une fonction de signature <code>function(?array $row, Cursor): ?array</code>
|
||||
*/
|
||||
private ?func $colsFunc = null;
|
||||
|
||||
function setColsFunc(?callable $func): self {
|
||||
@ -151,6 +160,50 @@ class Cursor implements Iterator {
|
||||
return null;
|
||||
}
|
||||
|
||||
function offsetExists($offset): bool {
|
||||
return cl::has($this->row, $offset);
|
||||
}
|
||||
|
||||
function offsetGet($offset) {
|
||||
return cl::get($this->row, $offset);
|
||||
}
|
||||
|
||||
function offsetSet($offset, $value): void {
|
||||
cl::set($this->row, $offset, $value);
|
||||
}
|
||||
|
||||
function offsetUnset($offset): void {
|
||||
cl::del($this->row, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* données de session: cela permet de maintenir certaines informations pendant
|
||||
* le parcours des données
|
||||
*/
|
||||
protected ?array $data;
|
||||
|
||||
/** @param string|int $key */
|
||||
function has($key): bool {
|
||||
return cl::has($this->data, $key);
|
||||
}
|
||||
|
||||
/** @param string|int $key */
|
||||
function get($key) {
|
||||
return cl::get($this->data, $key);
|
||||
}
|
||||
|
||||
/** @param string|int $key */
|
||||
function set($key, $value): void {
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
/** @param string|int $key */
|
||||
function del($key) {
|
||||
$orig = cl::get($this->data, $key);
|
||||
unset($this->data[$key]);
|
||||
return $orig;
|
||||
}
|
||||
|
||||
protected function convertToRow($raw): ?array {
|
||||
return cl::withn($raw);
|
||||
}
|
||||
@ -177,6 +230,7 @@ class Cursor implements Iterator {
|
||||
$this->key = null;
|
||||
$this->raw = null;
|
||||
$this->row = null;
|
||||
$this->data = null;
|
||||
if ($this->rowsGenerator !== null) {
|
||||
$rows = $this->rowsGenerator;
|
||||
if ($rows instanceof IteratorAggregate) $rows = $rows->getIterator();
|
||||
@ -223,6 +277,8 @@ class Cursor implements Iterator {
|
||||
$this->key = null;
|
||||
$this->raw = null;
|
||||
$this->row = null;
|
||||
# ne pas toucher à data, l'utilisateur peut vouloir continuer à consulter
|
||||
# les valeurs
|
||||
}
|
||||
return $valid;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user