<?php

class Document {

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $id;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $user;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $path;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $type;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $time;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $status;

    public function __construct() {

    }

    /**
     * Returns the email of the user
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getPath() {
        return $this->path;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getType() {
        return $this->type;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getTime() {
        return $this->time;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getStatus() {
        return $this->status;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getId() {
        return $this->id;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $id ARGDESCRIPTION
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $user ARGDESCRIPTION
     */
    public function setUser($user) {
        $this->user = $user;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $path ARGDESCRIPTION
     */
    public function setPath($path) {
        $this->path = $path;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $type ARGDESCRIPTION
     */
    public function setType($type) {
        $this->type = $type;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $time ARGDESCRIPTION
     */
    public function setTime($time) {
        $this->time = $time;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $status ARGDESCRIPTION
     */
    public function setStatus($status) {
        $this->status = $status;
    }

    /**
     * Get all document on the system, if email is provided
     * It fetches the documents of the particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Document Array of documents or null;
     */
    public static function getDocuments($link, $email = NULL) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        if (is_null($email)) {
            $query = "select * from documents";
        } else {
            $query = "select * from documents WHERE `user` = '$email'";
        }
        $documents = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $document = mysqli_fetch_object($result, Document::class);
            array_push($documents, $document);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $documents;
    }

}
