<?php


namespace CMSMSExt;

class Cookie
{
  /**
   * Cookie name - the name of the cookie.
   *
   * @var bool
   */
  private $name = FALSE;
  
  /**
   * Cookie value
   *
   * @var string
   */
  private $value = '';
  
  /**
   * Cookie life time
   *
   * @var int
   */
  private $expires;
  
  /**
   * Cookie domain
   *
   * @var string
   */
  private $domain = FALSE;
  
  /**
   * Cookie path
   *
   * @var string
   */
  private $path = FALSE;
  
  /**
   * Cookie secure
   *
   * @var bool
   */
  private $secure = FALSE;
  
  /**
   * Cookie http_only
   *
   * @var bool
   */
  private $http_only = FALSE;
  
  /**
   * Constructor
   *
   * @param string $name
   * @param string $value
   * @param int    $expires
   * @param string $path
   * @param string $domain
   * @param bool   $secure
   * @param bool   $http_only
   */
  public function __construct(
    string $name = '',
    string $value = '',
    int $expires = 0,
    string $path = '',
    string $domain = '',
    bool $secure = FALSE,
    bool $http_only = FALSE
  )
  {
    $this->setName($name);
    $this->setValue($value);
    $this->setExpires($expires);
    $this->setPath($path);
    $this->setDomain($domain);
    $this->setSecure($secure);
    $this->setHttpOnly($http_only);
  }
  
  /**
   * Create or Update cookie.
   */
  public function create()
  {
    return setcookie(
      $this->name,
      $this->getValue(),
      $this->getExpires(),
      $this->getPath(),
      $this->getDomain(),
      $this->getSecure(),
      $this->getHttpOnly()
    );
  }
  
  /**
   * Return a cookie
   *
   * @return mixed
   */
  public function get()
  {
    return $_COOKIE[$this->getName()];
  }
  
  /**
   * Delete cookie.
   *
   * @return bool
   */
  public function delete()
  {
    return setcookie($this->name, 1);
  }
  
  
  /**
   * @param $domain
   */
  public function setDomain($domain)
  {
    $this->domain = $domain;
  }
  
  /**
   * @return bool
   */
  public function getDomain()
  {
    return $this->domain;
  }
  
  /**
   * @param $id
   */
  public function setName($id)
  {
    $this->name = $id;
  }
  
  /**
   * @return bool
   */
  public function getName()
  {
    return $this->name;
  }
  
  /**
   * @param $path
   */
  public function setPath($path)
  {
    $this->path = $path;
  }
  
  /**
   * @return bool
   */
  public function getPath()
  {
    return $this->path;
  }
  
  /**
   * @param $secure
   */
  public function setSecure($secure)
  {
    $this->secure = $secure;
  }
  
  /**
   * @return bool
   */
  public function getSecure()
  {
    return $this->secure;
  }
  
  /**
   * @param $expires
   */
  public function setExpires($expires)
  {
    // Create a date
    $date = new DateTime();
    // Modify it (+1hours; +1days; +20years; -2days etc)
    $date->modify($expires);
    // Store the date in UNIX timestamp.
    $this->expires = $date->getTimestamp();
  }
  
  /**
   * @return bool|int
   */
  public function getExpires()
  {
    return $this->expires;
  }
  
  /**
   * @param string $value
   */
  public function setValue($value)
  {
    $this->value = $value;
  }
  
  /**
   * @return string
   */
  public function getValue()
  {
    return $this->value;
  }
  
  public function setHttpOnly(string $http_only)
  {
    $this->http_only = $http_only;
  }
  
  public function getHttpOnly()
  {
    return $this->http_only;
  }
}
