cookie.php 3.06 KB
Newer Older
1 2 3 4 5
<?php namespace System;

class Cookie {

	/**
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	 * The cookie name.
	 *
	 * @var string
	 */
	public $name;

	/**
	 * The cookie value.
	 *
	 * @var mixed
	 */
	public $value;

	/**
	 * The number of minutes the cookie should live.
	 *
	 * @var int
	 */
	public $lifetime = 0;

	/**
	 * The path for which the cookie is available.
	 *
	 * @var string
	 */
	public $path = '/';

	/**
	 * The domain for which the cookie is available.
	 *
	 * @var string
	 */
	public $domain = null;

	/**
	 * Indicates if the cookie should only be sent over HTTPS.
	 *
	 * @var bool
	 */
	public $secure = false;

	/**
	 * Create a new Cookie instance.
	 *
50 51 52 53 54
	 * Note: Cookies can be sent using the Cookie::put method.
	 *       However, the number of parameters that method requires
	 *       is somewhat cumbersome. Instantiating a new Cookie class
	 *       and setting the properties can be a little easier on the eyes.
	 *
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	 * @param  string  $name
	 * @return void
	 */
	public function __construct($name, $value = null)
	{
		$this->name = $name;
		$this->value = $value;
	}

	/**
	 * Create a new Cookie instance.
	 *
	 * @param  string  $name
	 * @return Cookie
	 */
	public static function make($name, $value = null)
	{
		return new static($name, $value);
	}

	/**
	 * Send the current cookie instance to the user's machine.
	 *
	 * @return bool
	 */
	public function send()
	{
		if (is_null($this->name))
		{
84
			throw new \Exception("Attempting to send cookie without a name.");
85 86 87 88 89 90
		}

		return static::put($this->name, $this->value, $this->lifetime, $this->path, $this->domain, $this->secure);
	}

	/**
91 92
	 * Determine if a cookie exists.
	 *
93
	 * @param  string  $name
94 95
	 * @return bool
	 */
96
	public static function has($name)
97
	{
Taylor Otwell committed
98
		return ! is_null(static::get($name));
99 100 101 102 103
	}

	/**
	 * Get the value of a cookie.
	 *
104
	 * @param  string  $name
105 106 107
	 * @param  mixed   $default
	 * @return string
	 */
108
	public static function get($name, $default = null)
109
	{
Taylor Otwell committed
110
		return Arr::get($_COOKIE, $name, $default);
111 112 113 114 115
	}

	/**
	 * Set a "permanent" cookie. The cookie will last 5 years.
	 *
116
	 * @param  string   $name
117 118 119 120 121 122
	 * @param  string   $value
	 * @param  string   $path
	 * @param  string   $domain
	 * @param  bool     $secure
	 * @return bool
	 */
123
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
124
	{
125
		return static::put($name, $value, 2628000, $path, $domain, $secure);
126 127 128
	}

	/**
129 130
	 * Set the value of a cookie. If a negative number of minutes is
	 * specified, the cookie will be deleted.
131
	 *
132
	 * @param  string   $name
133 134 135 136 137 138 139
	 * @param  string   $value
	 * @param  int      $minutes
	 * @param  string   $path
	 * @param  string   $domain
	 * @param  bool     $secure
	 * @return bool
	 */
140
	public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
141 142 143
	{
		if ($minutes < 0)
		{
144
			unset($_COOKIE[$name]);
145 146
		}

147
		return setcookie($name, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure);
148 149 150 151 152
	}

	/**
	 * Delete a cookie.
	 *
153
	 * @param  string  $name
154 155
	 * @return bool
	 */
156
	public static function forget($name)
157 158 159 160 161
	{
		return static::put($key, null, -60);
	}

}