From 2c7020f44d07fb67f209225ac9feca173b421675 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 24 Jun 2025 02:18:18 +0400 Subject: [PATCH] filtre de base --- php/src/db/Capacitor.php | 5 ++++- php/src/db/CapacitorChannel.php | 28 +++++++++++++++++++++++++++- php/src/db/CapacitorStorage.php | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/php/src/db/Capacitor.php b/php/src/db/Capacitor.php index 05404d0..8c62b42 100644 --- a/php/src/db/Capacitor.php +++ b/php/src/db/Capacitor.php @@ -146,8 +146,11 @@ class Capacitor implements ITransactor { function chargeAll(?iterable $items, $func=null, ?array $args=null): int { $count = 0; if ($items !== null) { + if ($func !== null) { + $func = func::with($func, $args)->bind($this->channel); + } foreach ($items as $item) { - $count += $this->charge($item, $func, $args); + $count += $this->charge($item, $func); } } return $count; diff --git a/php/src/db/CapacitorChannel.php b/php/src/db/CapacitorChannel.php index ad384ca..fdb908c 100644 --- a/php/src/db/CapacitorChannel.php +++ b/php/src/db/CapacitorChannel.php @@ -444,7 +444,7 @@ class CapacitorChannel implements ITransactor { return $this->capacitor->charge($item, $func, $args, $row); } - function chargeAll(iterable $items, $func=null, ?array $args=null): int { + function chargeAll(?iterable $items, $func=null, ?array $args=null): int { return $this->capacitor->chargeAll($items, $func, $args); } @@ -452,23 +452,49 @@ class CapacitorChannel implements ITransactor { return $this->capacitor->discharge($reset); } + /** + * retourner le filtre de base: les filtres de toutes les fonctions ci-dessous + * sont fusionnées avec le filtre de base + * + * cela permet de limiter toutes les opérations à un sous-ensemble des données + * du canal + */ + function getBaseFilter(): ?array { + return null; + } + + protected function verifixFilter(&$filter): void { + if ($filter !== null && !is_array($filter)) { + $primaryKeys = $this->primaryKeys ?? ["id_"]; + $id = $filter; + $this->verifixId($id); + $filter = [$primaryKeys[0] => $id]; + } + $filter = cl::merge($this->getBaseFilter(), $filter); + } + function count($filter=null): int { + $this->verifixFilter($filter); return $this->capacitor->count($filter); } function one($filter, ?array $mergeQuery=null): ?array { + $this->verifixFilter($filter); return $this->capacitor->one($filter, $mergeQuery); } function all($filter, ?array $mergeQuery=null): Traversable { + $this->verifixFilter($filter); return $this->capacitor->all($filter, $mergeQuery); } function each($filter, $func=null, ?array $args=null, ?array $mergeQuery=null, ?int &$nbUpdated=null): int { + $this->verifixFilter($filter); return $this->capacitor->each($filter, $func, $args, $mergeQuery, $nbUpdated); } function delete($filter, $func=null, ?array $args=null): int { + $this->verifixFilter($filter); return $this->capacitor->delete($filter, $func, $args); } diff --git a/php/src/db/CapacitorStorage.php b/php/src/db/CapacitorStorage.php index 1d8e785..c948691 100644 --- a/php/src/db/CapacitorStorage.php +++ b/php/src/db/CapacitorStorage.php @@ -434,7 +434,7 @@ abstract class CapacitorStorage { } if ($func !== null) { - $updates = func::with($func) + $updates = func::with($func, $args) ->prependArgs([$item, $row, $prow]) ->bind($channel) ->invoke();