This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/News/Category.php

58 lines
1.1 KiB
PHP
Raw Normal View History

2016-03-28 14:47:43 +00:00
<?php
/**
* Holds the news category object.
* @package Sakura
*/
namespace Sakura\News;
use Sakura\DB;
/**
* News category object.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Category
{
2016-08-05 02:35:37 +00:00
/**
* The name over this news category.
* @var string
*/
2016-03-28 14:47:43 +00:00
public $name = "";
2016-08-05 02:35:37 +00:00
/**
* Constructor.
* @param string $name
*/
2016-12-04 16:33:52 +00:00
public function __construct(string $name)
2016-03-28 14:47:43 +00:00
{
$this->name = $name;
}
2016-08-05 02:35:37 +00:00
/**
* Gets the news posts in this category.
* @param int $limit
2016-12-04 16:33:52 +00:00
* @return array
2016-08-05 02:35:37 +00:00
*/
2016-12-04 16:33:52 +00:00
public function posts(int $limit = 0): array
2016-03-28 14:47:43 +00:00
{
$postIds = DB::table('news')
->where('news_category', $this->name)
->orderBy('news_id', 'desc');
if ($limit) {
$postIds->limit($limit);
}
$postIds = $postIds->get(['news_id']);
$postIds = array_column($postIds, 'news_id');
$posts = [];
foreach ($postIds as $post) {
$posts[$post] = new Post($post);
}
return $posts;
}
}