Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
cretueusebiu committed Aug 6, 2015
0 parents commit efb6625
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
### Installation

`composer require hazzard/cookie`

### Usage

```
use Hazzard\Cookie\Cookie;
// Set a cookie for 60 minutes.
Cookie::set('name', 'value', 60);
// Set a cookie "forever".
Cookie::forever('name', 'value');
// Retrieve a cookie.
$value = Cookie::get('name');
// Forget a cookie.
Cookie::forget('name');
```


##### Default path and domain

```
Cookie::setDefaultPathAndDomain('/path/to', 'domain.com');
```


##### Encrypted cookies

First `composer require hazzard/encryption`, then:

```
use Hazzard\Cookie\Cookie;
use Hazzard\Encryption\Encrypter;
$encrypter = new Encrypter('a random 32 character string', 'AES-256-CBC');
Cookie::setEncrypter($encrypter);
```

You can use any other encrypter as long as it has the `encrypt` and `decrypt` methods.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "hazzard/cookie",
"description": "A simple cookie package.",
"license": "MIT",
"authors": [
{
"name": "Cretu Eusebiu",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3"
},
"autoload": {
"psr-4": {
"Hazzard\\Cookie\\": "src/"
}
},
"suggest": {
"hazzard/encryption": "Required to for encrypted cookies."
}
}
39 changes: 39 additions & 0 deletions src/Cookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Hazzard\Cookie;

class Cookie
{
/**
* The cookie jar instance.
*
* @var \Hazzard\Cookie\CookieJar
*/
protected static $cookieJar;

/**
* Get the cookie jar instance.
*
* @return \Hazzard\Cookie\CookieJar
*/
public static function getCookieJar()
{
if (!isset(static::$cookieJar)) {
static::$cookieJar = new CookieJar;
}
}

/**
* Call static cookie jar methods dynamically.
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public static function __callStatic($method, $arguments)
{
$mailer = static::getCookieJar();

return call_user_func_array([$mailer, $method], $arguments);
}
}
142 changes: 142 additions & 0 deletions src/CookieJar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Hazzard\Cookie;

class CookieJar implements CookieJarInterface
{
/**
* The default path (if specified).
*
* @var string
*/
protected $path = '/';

/**
* The default domain (if specified).
*
* @var string
*/
protected $domain = null;

/**
* Encrypter instance.
*
* @var \Hazzard\Encryption\Encrypter
*/
protected $encrypter;

/**
* Set a cookie.
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return bool
*/
public function set($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
list($path, $domain) = $this->getPathAndDomain($path, $domain);

$time = ($minutes == 0) ? 0 : time() + ($minutes * 60);

if (isset($this->encrypter)) {
$value = $this->encrypter->encrypt($value);
}

return setcookie($name, $value, $time, $path, $domain, $secure, $httpOnly);
}

/**
* Retrieve a cookie.
*
* @param string $name
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
if (array_key_exists($key, $_COOKIE)) {
$value = $_COOKIE[$key];

if (isset($this->encrypter)) {
$value = $this->encrypter->decrypt($value);
}

return $value;
}

return $default;
}

/**
* Set a cookie that lasts "forever" (five years).
*
* @param string $name
* @param string $value
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return bool
*/
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
return $this->set($name, $value, 2628000, $path, $domain, $secure, $httpOnly);
}

/**
* Expire a cookie.
*
* @param string $name
* @param string $path
* @param string $domain
* @return bool
*/
public function forget($name, $path = null, $domain = null)
{
return $this->set($name, null, -2628000, $path, $domain);
}

/**
* Get the path and domain, or the default values.
*
* @param string $path
* @param string $domain
* @return array
*/
protected function getPathAndDomain($path, $domain)
{
return array($path ?: $this->path, $domain ?: $this->domain);
}

/**
* Set the default path and domain for the jar.
*
* @param string $path
* @param string $domain
* @return $this
*/
public function setDefaultPathAndDomain($path, $domain)
{
list($this->path, $this->domain) = array($path, $domain);

return $this;
}

/**
* Set encrypter.
*
* @param \Hazzard\Encryption\Encrypter $encrypter
* @return $this
*/
public function setEncrypter($encrypter)
{
$this->encrypter = $encrypter;

return $this;
}
}
52 changes: 52 additions & 0 deletions src/CookieJarInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Hazzard\Cookie;

interface CookieJarInterface
{
/**
* Set a cookie.
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return bool
*/
public function set($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true);

/**
* Retrieve a cookie.
*
* @param string $name
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null);

/**
* Set a cookie that lasts "forever" (five years).
*
* @param string $name
* @param string $value
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return bool
*/
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true);

/**
* Expire a cookie.
*
* @param string $name
* @param string $path
* @param string $domain
* @return bool
*/
public function forget($name, $path = null, $domain = null);
}

0 comments on commit efb6625

Please sign in to comment.