130 lines
1.8 KiB
PHP
130 lines
1.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace rosavox\logic;
|
||
|
|
||
|
require_once(__DIR__ . '/helpers.php');
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
class docs_state
|
||
|
{
|
||
|
public static int $current_id = 0;
|
||
|
|
||
|
/**
|
||
|
* @param array {record<string,array>}
|
||
|
*/
|
||
|
public static array $pool = [];
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_id_encode(int $id) : string
|
||
|
{
|
||
|
return \sprintf('%u', $id);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_id_decode(string $id_encoded) : int
|
||
|
{
|
||
|
return intval($id_encoded);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_list() : array
|
||
|
{
|
||
|
return \array_map(
|
||
|
fn ($id_encoded) => [
|
||
|
'id' => docs_id_decode($id_encoded),
|
||
|
'doc' => docs_state::$pool[$id_encoded],
|
||
|
],
|
||
|
\array_keys(docs_state::$pool)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_read(int $id) : array
|
||
|
{
|
||
|
$id_encoded = docs_id_encode($id);
|
||
|
if (! \array_key_exists($id_encoded, docs_state::$pool))
|
||
|
{
|
||
|
throw (new Error('not found'));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return docs_state::$pool[$id_encoded];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_create(array $doc) : int
|
||
|
{
|
||
|
docs_state::$current_id += 1;
|
||
|
$id = docs_state::$current_id;
|
||
|
$id_encoded = docs_id_encode($id);
|
||
|
docs_state::$pool[$id_encoded] = $doc;
|
||
|
return $id;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_update(int $id, array $doc) : void
|
||
|
{
|
||
|
$id_encoded = docs_id_encode($id);
|
||
|
if (! \array_key_exists($id_encoded, docs_state::$pool))
|
||
|
{
|
||
|
throw (new Error('not found'));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
docs_state::$pool[$id_encoded] = $doc;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_delete(int $id) : void
|
||
|
{
|
||
|
$id_encoded = docs_id_encode($id);
|
||
|
if (! \array_key_exists($id_encoded, docs_state::$pool))
|
||
|
{
|
||
|
throw (new Error('not found'));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
docs_state::$pool[$id_encoded] = null;
|
||
|
unset(docs_state::$pool[$id_encoded]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function docs_add_examples() : void
|
||
|
{
|
||
|
docs_create(
|
||
|
[
|
||
|
'title' => 'Freibier bei Parteitagen',
|
||
|
'authors' => [
|
||
|
'Björn Biernot',
|
||
|
'Doreen Dauerdurst',
|
||
|
],
|
||
|
'content' => 'Wir haben Durst!',
|
||
|
'reasoning' => null,
|
||
|
]
|
||
|
);
|
||
|
}
|
||
|
|
||
|
?>
|