From c0da899b80886912db3730f460382c8a1ab274cd Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 25 Jun 2025 10:01:48 +0400 Subject: [PATCH] =?UTF-8?q?am=C3=A9liorer=20cursor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/php/coll/Cursor.php | 66 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/src/php/coll/Cursor.php b/src/php/coll/Cursor.php index f9a2bdb..bc5c9a3 100644 --- a/src/php/coll/Cursor.php +++ b/src/php/coll/Cursor.php @@ -1,6 +1,7 @@ ["?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 function(mixed $rows, Cursor): ?iterable */ + /** + * une fonction de signature function(mixed $rows, Cursor): ?iterable + */ private ?func $rowsFunc; - /** une fonction de signature function(?array $row, Cursor): bool */ + /** + * une fonction de signature function(?array $row, Cursor): bool + */ private ?func $filterFunc = null; function setFilter(array $filter): self { @@ -89,7 +94,9 @@ class Cursor implements Iterator { return $this; } - /** une fonction de signature function(?array $row, Cursor): ?array */ + /** + * une fonction de signature function(?array $row, Cursor): ?array + */ private ?func $mapFunc = null; function setMap(array $map): self { @@ -105,7 +112,9 @@ class Cursor implements Iterator { return $this; } - /** une fonction de signature function(?array $row, Cursor): ?array */ + /** + * une fonction de signature function(?array $row, Cursor): ?array + */ 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; }