} */ public static array $pool = []; } /** */ function docs_push() : void { $path = 'docs.json'; $data = [ 'current_id' => docs_state::$current_id, 'pool' => docs_state::$pool, ]; $content = \json_encode($data, \JSON_PRETTY_PRINT); \file_put_contents($path, $content); } /** */ function docs_pull() : void { $path = 'docs.json'; $content = (\file_exists($path) ? \file_get_contents($path) : null); $data = ( ($content === null) ? ['current_id' => 0, 'pool' => []] : \json_decode($content, true) ); docs_state::$current_id = $data['current_id']; docs_state::$pool = $data['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 \Exception('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; docs_push(); 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 \Exception('not found')); } else { docs_state::$pool[$id_encoded] = $doc; docs_push(); } } /** */ function docs_delete(int $id) : void { $id_encoded = docs_id_encode($id); if (! \array_key_exists($id_encoded, docs_state::$pool)) { throw (new \Exception('not found')); } else { docs_state::$pool[$id_encoded] = null; unset(docs_state::$pool[$id_encoded]); docs_push(); } } /** */ function docs_init() : void { docs_pull(); } /** */ function docs_add_examples() : void { docs_create( [ 'title' => 'Freibier bei Parteitagen', 'authors' => [ 'Björn Biernot', 'Doreen Dauerdurst', ], 'content' => 'Wir haben Durst!', 'reasoning' => null, ] ); } ?>