Skip to content

Commit

Permalink
Implement category functionality (#16)
Browse files Browse the repository at this point in the history
* Implement category functionality

* Resolve the test code error by adding the category field
  • Loading branch information
cable8mm committed Mar 5, 2024
1 parent 62034d5 commit ef0c6a6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
16 changes: 13 additions & 3 deletions database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private function __construct()
{
$this->connection = new Medoo([
'type' => 'sqlite',
'database' => __DIR__.'/database.sqlite',
'database' => __DIR__.'/../database/database.sqlite',
]);

$this->table = DB::table();
Expand Down Expand Up @@ -91,7 +91,7 @@ public function factory(): ?\PDOStatement
return $this->connection->insert($this->table, $factory);
}

public function getField(string $field, array|string $title, string $slug, string $body, string $published_at): array
public function getField(string $field, array|string $title, string $slug, string $categories, string $body, string $published_at): array
{
if ($field === 'id') {
return ['id' => [
Expand All @@ -115,6 +115,12 @@ public function getField(string $field, array|string $title, string $slug, strin
];
}

if ($field === $categories) {
$description = [
'VARCHAR',
];
}

if ($field === $body) {
$description = [
'TEXT',
Expand All @@ -134,7 +140,7 @@ public function getField(string $field, array|string $title, string $slug, strin
return [$field => $description];
}

public function getFake(string $field, array|string $title, string $slug, string $body, string $published_at): array|bool
public function getFake(string $field, array|string $title, string $slug, string $categories, string $body, string $published_at): array|bool
{
if ($field === 'id') {
return false;
Expand All @@ -150,6 +156,10 @@ public function getFake(string $field, array|string $title, string $slug, string
$fake = $this->faker->word();
}

if ($field === $categories) {
$fake = $this->faker->word();
}

if ($field === $body) {
$fake = $this->faker->text();
}
Expand Down
17 changes: 15 additions & 2 deletions src/Command/CreateJekyllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ class CreateJekyllCommand extends Command
{
private Medoo $database;

private array $categories = [
0 => '기타',
'행동+심리',
'의료/건강',
'감동',
'입양',
'재미',
'장소',
'상품',
];

protected function configure()
{
$this->database = DB::getInstance()->getConnection();
Expand All @@ -42,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($articles as $row) {
$article = Article::make($map)
->in($row)
->setBodyCallback(
function ($item) {
return preg_replace('/<img[^>]+>/', '', $item);
Expand All @@ -50,15 +62,16 @@ function ($item) {
}
)
->setAddHours(24 * 365 + 24 * 120)
->in($row);
->resolveCategories($this->categories);

$jekyll = new Jekyll(
layout: 'post',
title: $article->title,
date: $article->publishedAt,
author: 'Samgu Lee',
body: $article->markdown(),
slug: $article->slug
slug: $article->slug,
categories: $article->categories,
);

file_put_contents(__DIR__.'/../../dist/'.$jekyll->path(), $jekyll->render());
Expand Down
1 change: 1 addition & 0 deletions src/Mappers/DogStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return [
'title' => ['cover_title', 'front_title1', 'front_title2'],
'slug' => 'id',
'categories' => 'cast_category_id',
'body' => 'contents',
'published_at' => 'display_at',
];
6 changes: 5 additions & 1 deletion src/Mappers/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ class Mapper

private string $slug;

private string $categories;

private string $body;

private string|array $published_at;

public function __construct(string|array $title, string $slug, string $body, string|array $published_at)
public function __construct(string|array $title, string $slug, string $categories, string $body, string|array $published_at)
{
$this->title = $title;
$this->slug = $slug;
$this->categories = $categories;
$this->body = $body;
$this->published_at = $published_at;
}
Expand All @@ -27,6 +30,7 @@ public function fields(): array
$properties = [
$this->title,
$this->slug,
$this->categories,
$this->body,
$this->published_at,
];
Expand Down
27 changes: 27 additions & 0 deletions src/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cable8mm\DbToMarkdown\Models;

use Carbon\Carbon;
use InvalidArgumentException;
use League\HTMLToMarkdown\HtmlConverter;

class Article
Expand All @@ -12,6 +13,8 @@ class Article

public string $slug;

public ?string $categories = null;

public string $body;

private $bodyCallbacks;
Expand Down Expand Up @@ -57,6 +60,11 @@ private function slug(): void
$this->slug = preg_replace('/[_ ]/', '-', $this->row[$this->map['slug']]);
}

private function categories(): void
{
$this->categories = $this->row[$this->map['categories']];
}

private function body(): void
{
$converter = new HtmlConverter();
Expand Down Expand Up @@ -97,6 +105,7 @@ public function toArray(): array
return [
'title' => $this->title,
'slug' => $this->slug,
'categories' => $this->categories,
'body' => $this->body,
'published_at' => $this->publishedAt,
];
Expand All @@ -108,6 +117,7 @@ public function in(array $row): static

$this->title();
$this->slug();
$this->categories();
$this->body();
$this->publishedAt();

Expand All @@ -128,6 +138,23 @@ public function setAddHours(int $hours): static
return $this;
}

public function resolveCategories(?array $categories = null): static
{
if (! is_null($categories)) {
if (is_null($this->categories)) {
throw new InvalidArgumentException($this->slug.' slug\'s category must exist.');
}

if (! in_array($this->categories, array_keys($categories))) {
throw new InvalidArgumentException($this->categories.' is an invalid category. Categories must include one of '.implode(',', array_keys($categories)));
}

$this->categories = $categories[$this->categories];
}

return $this;
}

public static function make($map): static
{
$article = new static();
Expand Down
1 change: 1 addition & 0 deletions tests/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function test_callbacks_to_work_collectly(): void
'front_title2' => $faker->word(),
'id' => 1,
'contents' => $html,
'cast_category_id' => $faker->numberBetween(0, 7),
'display_at' => new Carbon(),
];

Expand Down

0 comments on commit ef0c6a6

Please sign in to comment.