implement text repo for json files
This commit is contained in:
parent
878e4840a2
commit
e9195fe991
1 changed files with 108 additions and 0 deletions
108
app/Text/JsonTextRepository.php
Normal file
108
app/Text/JsonTextRepository.php
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Text;
|
||||||
|
|
||||||
|
use App\Text\Text;
|
||||||
|
use App\Text\CreateTextDto;
|
||||||
|
use App\Text\TextRepository;
|
||||||
|
|
||||||
|
class JsonTextRepository implements TextRepository
|
||||||
|
{
|
||||||
|
private string $filePath;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->filePath = __DIR__.'/../../data/texts.json';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(CreateTextDto $dto): Text
|
||||||
|
{
|
||||||
|
$texts = $this->readTexts();
|
||||||
|
$id = $this->getNextId($texts);
|
||||||
|
|
||||||
|
$text = new Text(id: $id, name: $dto->name);
|
||||||
|
$texts[] = ['id' => $id, 'name' => $dto->name];
|
||||||
|
|
||||||
|
$this->writeTexts($texts);
|
||||||
|
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find(int $id): ?Text
|
||||||
|
{
|
||||||
|
$texts = $this->readTexts();
|
||||||
|
|
||||||
|
foreach ($texts as $data) {
|
||||||
|
if ($data['id'] === $id) {
|
||||||
|
return new Text(
|
||||||
|
id: $data['id'],
|
||||||
|
name: $data['name'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Text[]
|
||||||
|
*/
|
||||||
|
public function getAll(): array
|
||||||
|
{
|
||||||
|
$texts = $this->readTexts();
|
||||||
|
|
||||||
|
return array_map(
|
||||||
|
function (array $data) {
|
||||||
|
return new Text(
|
||||||
|
id: $data['id'],
|
||||||
|
name: $data['name'],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
$texts
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function readTexts(): array
|
||||||
|
{
|
||||||
|
if (!file_exists($this->filePath)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = file_get_contents($this->filePath);
|
||||||
|
|
||||||
|
return json_decode($content, true) ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $texts
|
||||||
|
*/
|
||||||
|
private function writeTexts(array $texts): void
|
||||||
|
{
|
||||||
|
file_put_contents(
|
||||||
|
$this->filePath,
|
||||||
|
json_encode($texts, JSON_PRETTY_PRINT)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $texts
|
||||||
|
*/
|
||||||
|
private function getNextId(array $texts): int
|
||||||
|
{
|
||||||
|
if (empty($texts)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$maxId = 0;
|
||||||
|
foreach ($texts as $text) {
|
||||||
|
if ($text['id'] > $maxId) {
|
||||||
|
$maxId = $text['id'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $maxId + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue