*/ class Category { /** * Id of this category. * @var int */ public $id = 0; /** * Name of this category. * @var string */ public $name = ""; /** * Description of this news category. * @var string */ public $description = null; /** * Whether this category should be hidden. * @var bool */ public $hidden = false; /** * Holds instances of Post. * @var array */ private $postsCache = []; /** * @param int $id */ public function __construct(int $id = 0) { if ($id !== 0) { $data = DB::table('news_categories') ->where('category_id', $id) ->first(); if ($data) { $this->id = intval($data->category_id); $this->name = $data->category_name; $this->category = $data->category_description; $this->hidden = boolval($data->category_hidden); } } } /** * Gets the news posts in this category. * @param int $limit * @param bool $excludeFuture * @param bool $excludeDeleted * @return array */ public function posts(int $limit = 0, bool $excludeDeleted = true): array { if (!$this->postsCache) { $posts = DB::table('news_posts') ->orderBy('post_id', 'desc'); if ($this->id !== 0) { $posts->where('category_id', $this->id); } if ($excludeDeleted) { $posts->whereNull('deleted_at'); } if ($limit) { $posts->limit($limit); } $this->postsCache = array_map(function (stdClass $post) { return new Post($post->post_id); }, $posts->get(['post_id'])); } return $this->postsCache; } }