rosavox/source/services/doc.php

266 lines
4.7 KiB
PHP

<?php
namespace rosavox\services\doc;
require_once('helpers/string.php');
require_once('helpers/misc.php');
require_once('entities/doc.php');
require_once('repositories/doc.php');
/**
*/
function name(int $id) : string
{
return \sprintf('doc-%04u', $id);
}
/**
*/
function readable_path(int $id) : string
{
return \sprintf('docs/%s.md', name($id));
}
/**
*/
function audible_path(int $id) : string
{
return \sprintf('docs/%s.oga', name($id));
}
/**
*/
function generate_readable(int $id, \rosavox\entities\doc\entity $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
);
}
/**
*/
function remove_readable(int $id) : void
{
$path = readable_path($id);
if (! \file_exists($path))
{
// do nothing
}
else
{
\unlink($path);
}
}
/**
*/
function generate_audible(int $id, \rosavox\entities\doc\entity $doc) : void
{
$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,
audible_path($id),
[
'blocking' => false,
]
);
}
/**
*/
function remove_audible(int $id) : void
{
$path = audible_path($id);
if (! \file_exists($path))
{
// do nothing
}
else
{
\unlink($path);
}
}
/**
*/
function dump() : array
{
return \rosavox\repositories\doc\repo::get_instance()->list_();
}
/**
*/
function get(int $id) : \rosavox\entities\doc\entity
{
return \rosavox\repositories\doc\repo::get_instance()->read($id);
}
/**
*/
function add(\rosavox\entities\doc\entity $doc) : int
{
$id = \rosavox\repositories\doc\repo::get_instance()->create($doc);
generate_readable($id, $doc);
generate_audible($id, $doc);
return $id;
}
/**
*/
function add_examples() : void
{
$entities = [
new \rosavox\entities\doc\entity(
'Freibier bei Parteitagen',
[
'Björn Biernot',
'Doreen Dauerdurst',
],
'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.',
'Wir haben Durst!',
),
new \rosavox\entities\doc\entity(
'Götterdämmerung',
[
'Fenriswolf',
],
'Der Allvater hat mich betrogen. Ich werde ihn und seine elende Asenbrut verschlingen. Diese Welt wird enden.',
null,
),
];
foreach ($entities as $entity)
{
add($entity);
}
}
/**
*/
function change(int $id, \rosavox\entities\doc\entity $doc) : void
{
\rosavox\repositories\doc\repo::get_instance()->update($id, $doc);
remove_readable($id);
remove_audible($id);
generate_readable($id, $doc);
generate_audible($id, $doc);
}
/**
*/
function remove(int $id) : void
{
\rosavox\repositories\doc\repo::get_instance()->delete($id);
remove_readable($id);
remove_audible($id);
}
/**
*/
function init() : void
{
\rosavox\repositories\doc\repo::get_instance()->setup();
if (empty(dump()))
{
add_examples();
}
else
{
// do nothing
}
}
?>