1: 2: 3: 4: 5: 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: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
<?php
/**
* TipyCookie
*
* @package tipy
*/
/**
* Access cookies the same way as any other tipy input/output objects
*
* <code>
* class MyController extends TipyController {
* public function index() {
* $value = $this->cookie->get('myVar1');
* $this->cookie->set('myVar2', 'Hello World!');
* // ...
* }
* }
* </code>
*/
class TipyCookie {
private $cookies;
/**
* Construct TipyCookie instance from $_COOKIE.
*/
public function __construct() {
$this->cookies = $_COOKIE;
}
/**
* Get cookie variable by its name
* @param string $key
* @param mixed $defaultValue
* @return mixed
*/
public function get($key) {
if (isset($this->cookies[$key])) {
return $this->cookies[$key];
} else {
if (func_num_args() > 1) {
return func_get_arg(1);
} else {
return null;
}
}
}
/**
* Set cookie variable. You may pass unix timestamp as optional argument to set cookie expire time.
* @param string $key
* @param mixed $value
* @param integer $expireTime
* @return mixed
*/
public function set($key, $value, $expireTime = 0) {
setcookie($key, $value, $expireTime, '/');
$this->cookies[$key] = $value;
}
/**
* Get all cookies as array
* @return array
*/
public function getAll() {
return $this->cookies;
}
}