2024-05-20 09:34:54 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\db;
|
|
|
|
|
|
|
|
/**
|
2024-05-20 11:03:20 +04:00
|
|
|
* Class CapacitorStorage: objet permettant d'accumuler des données pour les
|
|
|
|
* réutiliser plus tard
|
2024-05-20 09:34:54 +04:00
|
|
|
*/
|
2024-05-20 11:03:20 +04:00
|
|
|
abstract class CapacitorStorage {
|
2024-05-20 10:46:18 +04:00
|
|
|
abstract protected function getChannel(?string $name): CapacitorChannel;
|
2024-05-20 09:34:54 +04:00
|
|
|
|
|
|
|
abstract function _exists(CapacitorChannel $channel): bool;
|
|
|
|
|
|
|
|
/** tester si le canal spécifié existe */
|
2024-05-20 10:46:18 +04:00
|
|
|
function exists(?string $channel): bool {
|
2024-05-20 09:34:54 +04:00
|
|
|
return $this->_exists($this->getChannel($channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract function _reset(CapacitorChannel $channel): void;
|
|
|
|
|
|
|
|
/** supprimer le canal spécifié */
|
2024-05-20 10:46:18 +04:00
|
|
|
function reset(?string $channel): void {
|
2024-05-20 09:34:54 +04:00
|
|
|
$this->_reset($this->getChannel($channel));
|
|
|
|
}
|
|
|
|
|
2024-05-20 10:46:18 +04:00
|
|
|
abstract function _charge(CapacitorChannel $channel, $item, ?callable $func, ?array $args): bool;
|
2024-05-20 09:34:54 +04:00
|
|
|
|
2024-05-20 11:03:20 +04:00
|
|
|
/**
|
|
|
|
* charger une valeur dans le canal
|
|
|
|
*
|
|
|
|
* Si $func!==null, après avoir calculé les valeurs des clés supplémentaires
|
|
|
|
* avec {@link CapacitorChannel::getKeyValues()}, la fonction est appelée avec
|
|
|
|
* les arguments ($item, $keyValues, $row, ...$args)
|
|
|
|
* Si la fonction retourne un tableau, il est utilisé pour modifié les valeurs
|
|
|
|
* insérées/mises à jour
|
|
|
|
*
|
|
|
|
* @return true si l'objet a été chargé ou mis à jour, false s'il existait
|
|
|
|
* déjà à l'identique dans le canal
|
|
|
|
*/
|
2024-05-20 10:46:18 +04:00
|
|
|
function charge(?string $channel, $item, ?callable $func=null, ?array $args=null): bool {
|
|
|
|
return $this->_charge($this->getChannel($channel), $item, $func, $args);
|
2024-05-20 09:34:54 +04:00
|
|
|
}
|
|
|
|
|
2024-05-20 10:46:18 +04:00
|
|
|
abstract function _discharge(CapacitorChannel $channel, $filter, ?bool $reset): iterable;
|
2024-05-20 09:34:54 +04:00
|
|
|
|
2024-05-20 11:03:20 +04:00
|
|
|
/** décharger les données du canal spécifié */
|
2024-05-20 10:46:18 +04:00
|
|
|
function discharge(?string $channel, $filter=null, ?bool $reset=null): iterable {
|
|
|
|
return $this->_discharge($this->getChannel($channel), $filter, $reset);
|
2024-05-20 09:34:54 +04:00
|
|
|
}
|
|
|
|
|
2024-05-20 10:46:18 +04:00
|
|
|
abstract function _get(CapacitorChannel $channel, $filter);
|
2024-05-20 09:34:54 +04:00
|
|
|
|
2024-05-20 11:03:20 +04:00
|
|
|
/**
|
|
|
|
* obtenir l'élément identifié par les clés spécifiées sur le canal spécifié
|
|
|
|
*
|
|
|
|
* si $filter n'est pas un tableau, il est transformé en ["_id" => $filter]
|
|
|
|
*/
|
2024-05-20 10:46:18 +04:00
|
|
|
function get(?string $channel, $filter) {
|
|
|
|
return $this->_get($this->getChannel($channel), $filter);
|
2024-05-20 09:34:54 +04:00
|
|
|
}
|
|
|
|
|
2024-05-20 10:46:18 +04:00
|
|
|
abstract function _each(CapacitorChannel $channel, $filter, callable $func, ?array $args): void;
|
2024-05-20 09:34:54 +04:00
|
|
|
|
2024-05-20 11:03:20 +04:00
|
|
|
/**
|
|
|
|
* appeler une fonction pour chaque élément du canal spécifié.
|
|
|
|
*
|
|
|
|
* $filter permet de filtrer parmi les élements chargés
|
|
|
|
*
|
|
|
|
* si $func retourne un tableau, il est utilisé pour mettre à jour
|
|
|
|
* l'enregistrement.
|
|
|
|
*/
|
2024-05-20 10:46:18 +04:00
|
|
|
function each(?string $channel, $filter, callable $func, ?array $args=null): void {
|
|
|
|
$this->_each($this->getChannel($channel), $filter, $func, $args);
|
2024-05-20 09:34:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract function close(): void;
|
|
|
|
}
|