-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractContents.php
More file actions
79 lines (66 loc) · 2.16 KB
/
ExtractContents.php
File metadata and controls
79 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace App\Actions\Extract\Portal;
use App\Enums\PakSlug;
use App\Enums\Portal\ArticlePostType;
use App\Models\Portal\Article;
final readonly class ExtractContents
{
public function __construct(
private FindFileInfo $findFileInfo,
) {}
/**
* @return array{title:string,text:string,paks:PakSlug[]}
*/
public function __invoke(Article $article): array
{
$title = $article->title;
$text = $this->extractText($article);
$pakSlugs = $this->extractPaks($article);
return [
'title' => $title,
'text' => $text,
'paks' => $pakSlugs,
];
}
private function extractText(Article $article): string
{
$fields = [];
$fields[] = $article->contents['description'] ?? '';
$fields[] = $article->contents['thanks'] ?? '';
$fields[] = $article->contents['license'] ?? '';
$fields[] = $article->tags->pluck('name')->implode("\n");
$fields[] = $article->tags->pluck('description')->implode("\n");
if ($article->post_type === ArticlePostType::AddonPost) {
$fileId = $article->contents['file'] ?? null;
if (is_string($fileId) && is_numeric($fileId)) {
$fileInfo = ($this->findFileInfo)(intval($fileId));
if ($fileInfo instanceof \App\Models\Portal\FileInfo) {
$fields[] = $fileInfo->data ?? '';
}
}
}
return implode("\n", $fields);
}
/**
* @see https://github.com/128na/simutrans-portal/blob/master/database/seeders/CategorySeeder.php
*
* @return array<PakSlug>
*/
private function extractPaks(Article $article): array
{
$result = [];
foreach ($article->categories as $category) {
if ($category->slug === '64') {
$result[] = PakSlug::Pak64;
}
if ($category->slug === '128') {
$result[] = PakSlug::Pak128;
}
if ($category->slug === '128-japan') {
$result[] = PakSlug::Pak128Jp;
}
}
return $result;
}
}