2025-05-22 06:26:16 +00:00
< ? php
namespace rosavox\services\doc ;
require_once ( 'helpers/string.php' );
require_once ( 'helpers/storage.php' );
require_once ( 'helpers/misc.php' );
/**
*/
class state
{
public static ? \rosavox\helpers\storage\interface_ $storage = null ;
}
/**
*/
function name ( int $id ) : string
{
return \sprintf ( 'doc-%04u' , $id );
}
/**
*/
2025-05-22 19:56:16 +00:00
function readable_path ( int $id ) : string
2025-05-22 06:26:16 +00:00
{
2025-05-22 19:56:16 +00:00
return \sprintf ( 'docs/%s.md' , name ( $id ));
2025-05-22 06:26:16 +00:00
}
/**
*/
2025-05-22 19:56:16 +00:00
function audible_path ( int $id ) : string
2025-05-22 06:26:16 +00:00
{
2025-05-22 19:56:16 +00:00
return \sprintf ( 'docs/%s.oga' , name ( $id ));
2025-05-22 06:26:16 +00:00
}
/**
*/
function generate_readable ( int $id , $doc ) : void
{
$markdown = \rosavox\helpers\string_\coin (
" # { { value_title}}
## {{label_authors}}
{{ value_authors }}
## {{label_content}}
{{ value_content }}
{{ macro_reasoning }} " ,
[
'label_title' => \rosavox\helpers\misc\translate ( 'domain.doc.title' ),
'value_title' => $doc [ 'title' ],
'label_authors' => \rosavox\helpers\misc\translate ( 'domain.doc.authors' ),
'value_authors' => \implode (
" \n " ,
\array_map (
fn ( $author ) => \rosavox\helpers\string_\coin (
'- {{author}}' ,
[
'author' => $author ,
]
),
$doc [ 'authors' ]
)
),
'label_content' => \rosavox\helpers\misc\translate ( 'domain.doc.content' ),
'value_content' => $doc [ 'content' ],
'macro_reasoning' => (
( $doc [ 'reasoning' ] === null )
?
''
:
\rosavox\helpers\string_\coin (
"
## {{label_reasoning}}
{{ value_reasoning }} " ,
[
'label_reasoning' => \rosavox\helpers\misc\translate ( 'domain.doc.reasoning' ),
'value_reasoning' => $doc [ 'reasoning' ],
]
)
)
]
);
\file_put_contents (
readable_path ( $id ),
$markdown
);
}
/**
*/
2025-05-22 19:56:16 +00:00
function remove_readable (
int $id
) : void
{
$path = readable_path ( $id );
if ( ! \file_exists ( $path ))
{
// do nothing
}
else
{
\unlink ( $path );
}
}
/**
*/
function generate_audible (
int $id ,
$doc
) : void
2025-05-22 06:26:16 +00:00
{
$pause = " . \n " ;
$text = \rosavox\helpers\string_\coin (
" { { value_title}} { { pause}} { { label_authors}}: { { value_authors}} { { pause}} { { label_content}}: { { value_content}} { { macro_reasoning}} " ,
[
'pause' => $pause ,
'label_title' => \rosavox\helpers\misc\translate ( 'domain.doc.title' ),
'value_title' => $doc [ 'title' ],
'label_authors' => \rosavox\helpers\misc\translate ( 'domain.doc.authors' ),
'value_authors' => \implode ( $pause , $doc [ 'authors' ]),
'label_content' => \rosavox\helpers\misc\translate ( 'domain.doc.content' ),
'value_content' => $doc [ 'content' ],
'macro_reasoning' => (
( $doc [ 'reasoning' ] === null )
?
''
:
\rosavox\helpers\string_\coin (
" { { pause}} { { label_reasoning}}: { { value_reasoning}} " ,
[
'pause' => $pause ,
'label_reasoning' => \rosavox\helpers\misc\translate ( 'domain.doc.reasoning' ),
'value_reasoning' => $doc [ 'reasoning' ],
]
)
)
]
);
\rosavox\helpers\misc\generate_audio (
$text ,
2025-05-22 19:56:16 +00:00
audible_path ( $id ),
[
'blocking' => false ,
]
2025-05-22 06:26:16 +00:00
);
}
2025-05-22 19:56:16 +00:00
/**
*/
function remove_audible (
int $id
) : void
{
$path = audible_path ( $id );
if ( ! \file_exists ( $path ))
{
// do nothing
}
else
{
\unlink ( $path );
}
}
2025-05-22 06:26:16 +00:00
/**
*/
function empty_ () : array
{
return [
'title' => '' ,
'authors' => [],
'content' => '' ,
'reasoning' => null ,
];
}
/**
*/
function list_ () : array
{
return state :: $storage -> list_ ();
}
/**
*/
function read ( int $id ) : array
{
return state :: $storage -> read ( $id );
}
/**
* @ todo async generating
*/
function create ( array $doc ) : int
{
$id = state :: $storage -> create ( $doc );
generate_readable ( $id , $doc );
2025-05-22 19:56:16 +00:00
generate_audible ( $id , $doc );
2025-05-22 06:26:16 +00:00
return $id ;
}
/**
*/
function update ( int $id , array $doc ) : void
{
state :: $storage -> update ( $id , $doc );
2025-05-22 19:56:16 +00:00
remove_readable ( $id );
remove_audible ( $id );
2025-05-22 06:26:16 +00:00
generate_readable ( $id , $doc );
2025-05-22 19:56:16 +00:00
generate_audible ( $id , $doc );
2025-05-22 06:26:16 +00:00
}
/**
*/
function delete ( int $id ) : void
{
state :: $storage -> delete ( $id );
2025-05-22 19:56:16 +00:00
remove_readable ( $id );
remove_audible ( $id );
2025-05-22 06:26:16 +00:00
}
/**
*/
function add_examples () : void
{
create (
[
'title' => 'Freibier bei Parteitagen' ,
'authors' => [
'Björn Biernot' ,
'Doreen Dauerdurst' ,
],
'content' => 'Der Landesverband möge beschließen, dass zu Beginn eines jeden Parteitags für jeden Deligierten mindestens zwei Flaschen Bier auf den zugehörigen Platz zu stellen sind.' ,
'reasoning' => 'Wir haben Durst!' ,
]
);
create (
[
'title' => 'Götterdämmerung' ,
'authors' => [
'Fenriswolf' ,
],
2025-05-22 19:56:16 +00:00
'content' => 'Der Allvater hat mich betrogen. Ich werde ihn und seine elende Asenbrut verschlingen. Diese Welt wird enden.' ,
2025-05-22 06:26:16 +00:00
'reasoning' => null ,
]
);
}
/**
*/
function init () : void
{
state :: $storage = new \rosavox\helpers\storage\class_jsonfile (
'docs.json' ,
fn ( $id ) => \sprintf ( '%u' , $id ),
fn ( $id_encoded ) => \intval ( $id_encoded )
);
if ( empty ( state :: $storage -> list_ ()))
{
add_examples ();
}
else
{
// do nothing
}
}
?>