cookie.php 2.72 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2

3 4 5
class Cookie {

	/**
6 7 8 9 10 11 12
	 * The cookies that have been set.
	 *
	 * @var array
	 */
	public static $jar = array();

	/**
13 14
	 * Determine if a cookie exists.
	 *
15
	 * @param  string  $name
16 17
	 * @return bool
	 */
18
	public static function has($name)
19
	{
20
		return ! is_null(static::get($name));
21 22 23 24 25
	}

	/**
	 * Get the value of a cookie.
	 *
26 27 28 29
	 * <code>
	 *		// Get the value of the "favorite" cookie
	 *		$favorite = Cookie::get('favorite');
	 *
30
	 *		// Get the value of a cookie or return a default value 
31 32 33
	 *		$favorite = Cookie::get('framework', 'Laravel');
	 * </code>
	 *
34
	 * @param  string  $name
35 36 37
	 * @param  mixed   $default
	 * @return string
	 */
38
	public static function get($name, $default = null)
39
	{
40
		if (isset(static::$jar[$name])) return static::$jar[$name];
41

42
		return array_get(Request::foundation()->cookies->all(), $name, $default);
43 44 45
	}

	/**
46 47 48 49 50 51 52 53 54
	 * Set the value of a cookie.
	 *
	 * <code>
	 *		// Set the value of the "favorite" cookie
	 *		Cookie::put('favorite', 'Laravel');
	 *
	 *		// Set the value of the "favorite" cookie for twenty minutes
	 *		Cookie::put('favorite', 'Laravel', 20);
	 * </code>
55
	 *
56 57
	 * @param  string  $name
	 * @param  string  $value
58
	 * @param  int     $expiration
59 60 61
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
Phill Sparks committed
62
	 * @return void
63
	 */
64
	public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
65
	{
66 67 68 69 70
		if ($expiration !== 0)
		{
			$expiration = time() + ($expiration * 60);
		}

Taylor Otwell committed
71 72 73 74 75 76 77 78
		// If the secure option is set to true, yet the request is not over HTTPS
		// we'll throw an exception to let the developer know that they are
		// attempting to send a secure cookie over the unsecure HTTP.
		if ($secure and ! Request::secure())
		{
			throw new \Exception("Attempting to set secure cookie over HTTP.");
		}

79
		static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
80 81 82
	}

	/**
83
	 * Set a "permanent" cookie. The cookie will last for one year.
84
	 *
85 86 87 88
	 * <code>
	 *		// Set a cookie that should last one year
	 *		Cookie::forever('favorite', 'Blue');
	 * </code>
89
	 *
90 91 92 93 94
	 * @param  string  $name
	 * @param  string  $value
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
95 96
	 * @return bool
	 */
97
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
98
	{
99
		return static::put($name, $value, 525600, $path, $domain, $secure);
Taylor Otwell committed
100 101 102
	}

	/**
103 104
	 * Delete a cookie.
	 *
105
	 * @param  string  $name
106 107 108
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
109 110
	 * @return bool
	 */
111
	public static function forget($name, $path = '/', $domain = null, $secure = false)
112
	{
113
		return static::put($name, null, -2000, $path, $domain, $secure);
114 115
	}

116
}