A bare bones front-end for knockout designed for maximum compatibility with "obsolete" browsers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

81 satır
2.0KB

  1. <?php
  2. namespace App\Knockout;
  3. use App\Helper\BBCode;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\Cache;
  6. class Post {
  7. // meta
  8. public $id;
  9. public $date;
  10. public $content;
  11. // related
  12. public User $user;
  13. public $ratings = [];
  14. public static function unwrap($post)
  15. {
  16. $s = new self();
  17. $s->id = $post->id;
  18. $s->date = Carbon::parse($post->createdAt)->format('d/m/Y H:i');
  19. $s->content = $post->content;
  20. // grab user if available
  21. if (isset($post->user) && isset($post->user->id)) {
  22. $s->user = User::unwrap($post->user);
  23. }
  24. // grab ratings if available
  25. $s->ratings = array_map(function($rating) {
  26. return Rating::unwrap($rating);
  27. }, $post->ratings ?? []);
  28. return $s;
  29. }
  30. private static function requestAll(int $userId, int $page = 1): Dataset
  31. {
  32. $data = (new AbstractData)->httpGet(sprintf('/user/%u/posts/%u', $userId, $page));
  33. $json = json_decode($data);
  34. $records = array_map(function($user) {
  35. return self::unwrap($user);
  36. }, $json->posts);
  37. return (new Dataset($records))
  38. ->setTotalRecords($json->totalPosts)
  39. ->setCurrentPage($page)
  40. ->setRecordsPerPage(40);
  41. }
  42. public static function updateAll(int $userId, int $page = 0): Dataset
  43. {
  44. $key = sprintf('user-posts-%u-%u', $userId, $page);
  45. $data = self::requestAll($userId, $page);
  46. Cache::put($key, $data, 300);
  47. return $data;
  48. }
  49. public static function all(int $userId, int $page = 0): Dataset
  50. {
  51. $key = sprintf('user-posts-%u-%u', $userId, $page);
  52. return Cache::get($key, function() use($userId, $page) {
  53. return self::updateAll($userId, $page);
  54. });
  55. }
  56. public function dateDiff()
  57. {
  58. return Carbon::createFromFormat('d/m/Y H:i', $this->date)->diffForHumans();
  59. }
  60. public function render()
  61. {
  62. return (new BBCode($this->content))->render();
  63. }
  64. }