nur-sery/src/db/CapacitorStorage.php

93 lines
3.2 KiB
PHP

<?php
namespace nur\sery\db;
/**
* Class CapacitorStorage: objet permettant d'accumuler des données pour les
* réutiliser plus tard
*/
abstract class CapacitorStorage {
abstract protected function getChannel(?string $name): CapacitorChannel;
abstract function _exists(CapacitorChannel $channel): bool;
/** tester si le canal spécifié existe */
function exists(?string $channel): bool {
return $this->_exists($this->getChannel($channel));
}
abstract function _ensureExists(CapacitorChannel $channel): void;
/** s'assurer que le canal spécifié existe */
function ensureExists(?string $channel): void {
$this->_ensureExists($this->getChannel($channel));
}
abstract function _reset(CapacitorChannel $channel): void;
/** supprimer le canal spécifié */
function reset(?string $channel): void {
$this->_reset($this->getChannel($channel));
}
abstract function _charge(CapacitorChannel $channel, $item, ?callable $func, ?array $args): int;
/**
* 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
* la signature ($item, $keyValues, $row, ...$args)
* Si la fonction retourne un tableau, il est utilisé pour modifier les valeurs
* insérées/mises à jour
*
* @return int 1 si l'objet a été chargé ou mis à jour, 0 s'il existait
* déjà à l'identique dans le canal
*/
function charge(?string $channel, $item, ?callable $func=null, ?array $args=null): int {
return $this->_charge($this->getChannel($channel), $item, $func, $args);
}
abstract function _count(CapacitorChannel $channel, $filter): int;
/** indiquer le nombre d'éléments du canal spécifié */
function count(?string $channel, $filter=null): int {
return $this->_count($this->getChannel($channel), $filter);
}
abstract function _discharge(CapacitorChannel $channel, $filter, ?bool $reset): iterable;
/** décharger les données du canal spécifié */
function discharge(?string $channel, $filter=null, ?bool $reset=null): iterable {
return $this->_discharge($this->getChannel($channel), $filter, $reset);
}
abstract function _get(CapacitorChannel $channel, $filter);
/**
* 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]
*/
function get(?string $channel, $filter) {
return $this->_get($this->getChannel($channel), $filter);
}
abstract function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): int;
/**
* appeler une fonction pour chaque élément du canal spécifié.
*
* $filter permet de filtrer parmi les élements chargés
*
* $func est appelé avec la signature ($item, $row, ...$args). si la fonction
* retourne un tableau, il est utilisé pour mettre à jour la ligne
*
* @return int le nombre de lignes parcourues
*/
function each(?string $channel, $filter, ?callable $func=null, ?array $args=null): int {
return $this->_each($this->getChannel($channel), $filter, $func, $args);
}
abstract function close(): void;
}