-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.php
79 lines (56 loc) · 1.5 KB
/
cookie.php
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
71
72
73
74
75
76
77
78
79
<?php
/**
*
* Description: Cookies manager class
* Author: Denis Vororpaev
* Author Email: d.n.voropaev@gmail.com
* Version: 0.1.0
* Copyright: Denis Voropaev © 2016
*
**/
class Cookie {
public $name;
public $expire;
public function __construct ($name, $expire) {
$this->name = $name;
$this->expire = $expire;
}
public function get () {
if (empty ($_COOKIE[$this->name])) { return array (); }
$cart_cookie = json_decode (
base64_decode (
urldecode (
$_COOKIE[$this->name] )), true);
return is_array ($cart_cookie) ? $cart_cookie : array ();
}
public function set ($items) {
$data = base64_encode (json_encode ($items));
$result = setcookie ($this->name, $data, $this->expire, '/');
$_COOKIE[$this->name] = $data;
return $_COOKIE[$this->name];
}
public function add ($items) {
return (is_array ($items) ?
$this->set (array_merge ($this->get (), $items)) : $this->get ());
}
public function getProperty ($property) {
$cookie = $this->get ();
return isset ($cookie[$property]) ? $cookie[$property] : null;
}
public function setProperty ($property, $value) {
if (!is_int ($property) && !is_string ($property)) {
return $this->get ();
}
$cookie = $this->get ();
if (isset ($cookie[$property]) && ($value === null)) {
unset ($cookie[$property]);
} else {
$cookie[$property] = $value;
}
return $this->set ($cookie);
}
public function unsetProperty ($property) {
$this->setProperty ($property, null);
}
}
?>