diff --git a/src/Builder/BaseBuilder.php b/src/Builder/BaseBuilder.php index 07989b9..d6f29e9 100644 --- a/src/Builder/BaseBuilder.php +++ b/src/Builder/BaseBuilder.php @@ -1109,11 +1109,11 @@ public function sql(bool $preserve = false): string } /** - * Nettoyage des bindings + * Vide les bindings d'un context */ - public function cleanBindings(array $bindings): array + public function clearBindings(?string $context = null): void { - return $this->bindings->clean($bindings); + $this->bindings->clear($context); } /** diff --git a/src/Builder/BindingCollection.php b/src/Builder/BindingCollection.php index b9f5a9d..3fd3f7a 100644 --- a/src/Builder/BindingCollection.php +++ b/src/Builder/BindingCollection.php @@ -81,13 +81,26 @@ public function addMany(array $values, string $type = 'where'): self return $this; } + /** + * (Re)Définit un ensemble de bindings pour un contexte + * + * Cet méthode écrase les bindings existants pour le contexte donné + */ + public function set(array $values, string $context = 'where'): static + { + $this->clear($context); + $this->addMany($values, $context); + + return $this; + } + /** * Ajoute un binding nommé */ - public function addNamed(string $name, mixed $value, string $type = 'where', ?int $pdoType = null): self + public function addNamed(string $name, mixed $value, string $context = 'where', ?int $pdoType = null): self { - $this->bindings[$type][$name] = $value; - $this->types[$type][$name] = $pdoType ?? $this->guessType($value); + $this->bindings[$context][$name] = $value; + $this->types[$context][$name] = $pdoType ?? $this->guessType($value); return $this; } @@ -97,9 +110,9 @@ public function addNamed(string $name, mixed $value, string $type = 'where', ?in * * @return list|mixed */ - public function get(string $type, ?string $name = null) + public function get(string $context, ?string $name = null) { - $bindings = $this->bindings[$type] ?? []; + $bindings = $this->bindings[$context] ?? []; return $name ? ($bindings[$name] ?? null) : $bindings; } @@ -107,20 +120,20 @@ public function get(string $type, ?string $name = null) /** * Récupère tous les bindings dans l'ordre de compilation * - * @param list $types + * @param list $contexts * * @return list */ - public function getOrdered(array $types = []): array + public function getOrdered(array $contexts = []): array { - if ($types === []) { - $types = self::TYPES; + if ($contexts === []) { + $contexts = self::TYPES; } $result = []; - foreach ($types as $type) { - if (!empty($this->bindings[$type])) { - array_push($result, ...$this->bindings[$type]); + foreach ($contexts as $context) { + if (!empty($this->bindings[$context])) { + array_push($result, ...$this->bindings[$context]); } } @@ -153,18 +166,18 @@ public function getTypesOrdered(array $types = []): array /** * Vérifie si un contexte a des bindings */ - public function has(string $type): bool + public function has(string $context): bool { - return !empty($this->bindings[$type]); + return !empty($this->bindings[$context]); } /** * Compte le nombre total de bindings */ - public function count(?string $type = null): int + public function count(?string $context = null): int { - if ($type !== null) { - return count($this->bindings[$type] ?? []); + if ($context !== null) { + return count($this->bindings[$context] ?? []); } return array_sum(array_map('count', $this->bindings)); @@ -173,24 +186,23 @@ public function count(?string $type = null): int /** * Vérifie si un contexte est vide */ - public function isEmpty(?string $type = null): bool + public function isEmpty(?string $context = null): bool { - return $this->count($type) === 0; + return $this->count($context) === 0; } /** * Vide tous les bindings */ - public function clear(?string $type = null): self + public function clear(?string $context = null): self { - if ($type !== null) { - $this->bindings[$type] = []; - $this->types[$type] = []; - } else { - foreach (self::TYPES as $t) { - $this->bindings[$t] = []; - $this->types[$t] = []; - } + if ($context !== null) { + return $this->clearContext($context); + } + + foreach (self::TYPES as $t) { + $this->bindings[$t] = []; + $this->types[$t] = []; } return $this; @@ -199,11 +211,11 @@ public function clear(?string $type = null): self /** * Vide les bindings d'un contexte spécifique */ - public function clearType(string $type): self + public function clearContext(string $context): self { - if (isset($this->bindings[$type])) { - $this->bindings[$type] = []; - $this->types[$type] = []; + if (isset($this->bindings[$context])) { + $this->bindings[$context] = []; + $this->types[$context] = []; } return $this;