Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Builder/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
76 changes: 44 additions & 32 deletions src/Builder/BindingCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -97,30 +110,30 @@ public function addNamed(string $name, mixed $value, string $type = 'where', ?in
*
* @return list<mixed>|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;
}

/**
* Récupère tous les bindings dans l'ordre de compilation
*
* @param list<string> $types
* @param list<string> $contexts
*
* @return list<mixed>
*/
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]);
}
}

Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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;
Expand Down