164 lines
2.5 KiB
PHP
164 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace rosavox\repositories\doc;
|
|
|
|
require_once('helpers/storage-interface.php');
|
|
require_once('helpers/storage-jsonfile.php');
|
|
require_once('helpers/storage-sqlitetable.php');
|
|
|
|
require_once('entities/doc.php');
|
|
|
|
|
|
/**
|
|
*/
|
|
class repo implements \rosavox\helpers\storage\interface_/*<int,\rosavox\entities\doc\entity>*/
|
|
{
|
|
|
|
/**
|
|
*/
|
|
private \rosavox\helpers\storage\interface_ $core;
|
|
|
|
|
|
/**
|
|
*/
|
|
private function __construct()
|
|
{
|
|
/*
|
|
$this->core = new \rosavox\helpers\storage\class_jsonfile(
|
|
'docs.json',
|
|
fn ($id) => \sprintf('%u', $id),
|
|
fn ($id_encoded) => \intval($id_encoded)
|
|
);
|
|
*/
|
|
$this->core = new \rosavox\helpers\storage\class_sqlitetable(
|
|
'data.sqlite',
|
|
'docs',
|
|
[
|
|
[
|
|
'name' => 'title',
|
|
'type' => 'string',
|
|
'nullable' => false,
|
|
],
|
|
[
|
|
'name' => 'authors',
|
|
'type' => 'string',
|
|
'nullable' => false,
|
|
],
|
|
[
|
|
'name' => 'content',
|
|
'type' => 'string',
|
|
'nullable' => false,
|
|
],
|
|
[
|
|
'name' => 'reasoning',
|
|
'type' => 'string',
|
|
'nullable' => true,
|
|
],
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
private static ?repo $instance = null;
|
|
|
|
|
|
/**
|
|
*/
|
|
public static function get_instance()
|
|
{
|
|
if (self::$instance === null)
|
|
{
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
private static function encode(\rosavox\entities\doc\entity $doc) : array
|
|
{
|
|
return [
|
|
$doc->title,
|
|
\implode(',', $doc->authors),
|
|
$doc->content,
|
|
$doc->reasoning,
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
private static function decode(array $row) : \rosavox\entities\doc\entity
|
|
{
|
|
return (new \rosavox\entities\doc\entity(
|
|
$row[0],
|
|
\explode(',', $row[1]),
|
|
$row[2],
|
|
$row[3],
|
|
));
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function setup() : void
|
|
{
|
|
$this->core->setup();
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function list_() : array
|
|
{
|
|
return \rosavox\helpers\list_\map(
|
|
$this->core->list_(),
|
|
fn ($entry) => [
|
|
'id' => $entry['id'],
|
|
'value' => self::decode($entry['value']),
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function read($id)
|
|
{
|
|
return self::decode($this->core->read($id));
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function create($doc)
|
|
{
|
|
return $this->core->create(self::encode($doc));
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function update($id, $doc) : void
|
|
{
|
|
$this->core->update($id, self::encode($doc));
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function delete($id) : void
|
|
{
|
|
$this->core->delete($id);
|
|
}
|
|
|
|
}
|