améliorer cursor
This commit is contained in:
parent
ef13b8afc9
commit
c0da899b80
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace nulib\php\coll;
|
namespace nulib\php\coll;
|
||||||
|
|
||||||
|
use ArrayAccess;
|
||||||
use Iterator;
|
use Iterator;
|
||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
use nulib\cl;
|
use nulib\cl;
|
||||||
@ -18,7 +19,7 @@ use Traversable;
|
|||||||
* @property-read array|null $value alias pour $row
|
* @property-read array|null $value alias pour $row
|
||||||
* @property-read iterable|null $rows la source des lignes
|
* @property-read iterable|null $rows la source des lignes
|
||||||
*/
|
*/
|
||||||
class Cursor implements Iterator {
|
class Cursor implements Iterator, ArrayAccess {
|
||||||
const PARAMS_SCHEMA = [
|
const PARAMS_SCHEMA = [
|
||||||
"rows" => ["?iterable"],
|
"rows" => ["?iterable"],
|
||||||
"rows_func" => ["?callable"],
|
"rows_func" => ["?callable"],
|
||||||
@ -70,10 +71,14 @@ class Cursor implements Iterator {
|
|||||||
/** un générateur de lignes */
|
/** un générateur de lignes */
|
||||||
private ?Traversable $rowsGenerator;
|
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;
|
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;
|
private ?func $filterFunc = null;
|
||||||
|
|
||||||
function setFilter(array $filter): self {
|
function setFilter(array $filter): self {
|
||||||
@ -89,7 +94,9 @@ class Cursor implements Iterator {
|
|||||||
return $this;
|
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;
|
private ?func $mapFunc = null;
|
||||||
|
|
||||||
function setMap(array $map): self {
|
function setMap(array $map): self {
|
||||||
@ -105,7 +112,9 @@ class Cursor implements Iterator {
|
|||||||
return $this;
|
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;
|
private ?func $colsFunc = null;
|
||||||
|
|
||||||
function setColsFunc(?callable $func): self {
|
function setColsFunc(?callable $func): self {
|
||||||
@ -151,6 +160,50 @@ class Cursor implements Iterator {
|
|||||||
return null;
|
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 {
|
protected function convertToRow($raw): ?array {
|
||||||
return cl::withn($raw);
|
return cl::withn($raw);
|
||||||
}
|
}
|
||||||
@ -177,6 +230,7 @@ class Cursor implements Iterator {
|
|||||||
$this->key = null;
|
$this->key = null;
|
||||||
$this->raw = null;
|
$this->raw = null;
|
||||||
$this->row = null;
|
$this->row = null;
|
||||||
|
$this->data = null;
|
||||||
if ($this->rowsGenerator !== null) {
|
if ($this->rowsGenerator !== null) {
|
||||||
$rows = $this->rowsGenerator;
|
$rows = $this->rowsGenerator;
|
||||||
if ($rows instanceof IteratorAggregate) $rows = $rows->getIterator();
|
if ($rows instanceof IteratorAggregate) $rows = $rows->getIterator();
|
||||||
@ -223,6 +277,8 @@ class Cursor implements Iterator {
|
|||||||
$this->key = null;
|
$this->key = null;
|
||||||
$this->raw = null;
|
$this->raw = null;
|
||||||
$this->row = null;
|
$this->row = null;
|
||||||
|
# ne pas toucher à data, l'utilisateur peut vouloir continuer à consulter
|
||||||
|
# les valeurs
|
||||||
}
|
}
|
||||||
return $valid;
|
return $valid;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user