ArticleService.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Article;
  4. use App\Models\ArticleCollection;
  5. class ArticleService
  6. {
  7. public function getRawById(string $id)
  8. {
  9. return Article::find($id);
  10. }
  11. public function getRawByTitle(string $title)
  12. {
  13. $article = Article::where('title', $title)->first();
  14. return $article;
  15. }
  16. public function sentenceIds(string $id): ?array
  17. {
  18. $article = $this->getRawById($id);
  19. if (empty($article->content)) {
  20. return null;
  21. }
  22. $sentenceIds = $this->extractBracesContent($article->content);
  23. return $sentenceIds;
  24. }
  25. /**
  26. * 提取字符串中 {{ }} 之间的内容
  27. *
  28. * @param string $text
  29. * @return array
  30. */
  31. public function extractBracesContent(string $text): array
  32. {
  33. preg_match_all('/\{\{\s*(.*?)\s*\}\}/', $text, $matches);
  34. return $matches[1] ?? [];
  35. }
  36. public function articlesInAnthology($anthologyId)
  37. {
  38. $inCollection = ArticleCollection::where('collect_id', $anthologyId)
  39. ->select('article_id')
  40. ->get()->toArray();
  41. return array_map(fn($item) => $item['article_id'], $inCollection);
  42. }
  43. }