Files
spa-api/laravel/cookie.php

93 lines
1.9 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class Cookie {
/**
* All of the cookies for the current request.
*
* @var array
*/
private $cookies;
/**
* Create a new cookie manager instance.
*
* @param array $cookies
* @return void
*/
public function __construct(&$cookies)
{
$this->cookies = &$cookies;
}
2011-06-08 23:45:08 -05:00
/**
* Determine if a cookie exists.
*
* @param string $name
2011-06-08 23:45:08 -05:00
* @return bool
*/
public function has($name)
2011-06-08 23:45:08 -05:00
{
return ! is_null($this->get($name));
2011-06-08 23:45:08 -05:00
}
/**
* Get the value of a cookie.
*
* @param string $name
2011-06-08 23:45:08 -05:00
* @param mixed $default
* @return string
*/
public function get($name, $default = null)
2011-06-08 23:45:08 -05:00
{
return Arr::get($this->cookies, $name, $default);
2011-06-08 23:45:08 -05:00
}
/**
* Set a "permanent" cookie. The cookie will last 5 years.
*
* @param string $name
* @param string $value
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $http_only
2011-06-08 23:45:08 -05:00
* @return bool
*/
public function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false)
2011-06-08 23:45:08 -05:00
{
return $this->put($name, $value, 2628000, $path, $domain, $secure, $http_only);
2011-06-08 23:45:08 -05:00
}
/**
2011-06-16 20:29:21 -05:00
* Set the value of a cookie. If a negative number of minutes is
* specified, the cookie will be deleted.
2011-06-08 23:45:08 -05:00
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $http_only
2011-06-08 23:45:08 -05:00
* @return bool
*/
public function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false)
2011-06-08 23:45:08 -05:00
{
2011-08-08 08:49:29 -05:00
if ($minutes < 0) unset($_COOKIE[$name]);
2011-06-08 23:45:08 -05:00
return setcookie($name, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure, $http_only);
2011-06-08 23:45:08 -05:00
}
/**
* Delete a cookie.
*
* @param string $name
2011-06-08 23:45:08 -05:00
* @return bool
*/
public function forget($name)
2011-06-08 23:45:08 -05:00
{
return $this->put($name, null, -60);
2011-06-08 23:45:08 -05:00
}
}