<?php

class News {

    private $id;
    private $title;
    private $body;
    private $date;
    private $fav;

    function setId($id) {
        $this->id = $id;
    }

    function getId() {
        return $this->id;
    }

    function setTitle($title) {
        $this->title = $title;
    }

    function getTitle() {
        return $this->title;
    }

    function setBody($body) {
        $this->body = $body;
    }

    function getBody() {
        return $this->body;
    }

    function setDate($date) {
        $this->date = $date;
    }

    function getDate() {
        return $this->date;
    }

    function setFav($fav) {
        $this->fav = $fav;
    }

    function getFav() {
        return $this->fav;
    }

    /**
     * Get news on system
     *
     * @param mysqli $link
     * @access public
     *
     * @return News Array of news or null;
     */
    public static function getNewsPage($link) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $query = "select * from news ORDER BY id desc";
        $news = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $n = mysqli_fetch_object($result, News::class);
            array_push($news, $n);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $news;
    }

}
