1
|
|
|
<?php |
2
|
|
|
namespace FMUP; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Cookie |
6
|
|
|
* @package FMUP |
7
|
|
|
*/ |
8
|
|
|
class Cookie |
9
|
|
|
{ |
10
|
|
|
private static $instance; |
11
|
|
|
|
12
|
1 |
|
private function __construct() |
13
|
|
|
{ |
14
|
1 |
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @codeCoverageIgnore |
18
|
|
|
*/ |
19
|
|
|
private function __clone() |
20
|
|
|
{ |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Retrieve cookie system - start cookie if not started |
25
|
|
|
* @return Cookie |
26
|
|
|
*/ |
27
|
2 |
|
final public static function getInstance() |
28
|
|
|
{ |
29
|
2 |
|
if (!isset(self::$instance)) { |
30
|
1 |
|
$class = get_called_class(); |
31
|
1 |
|
self::$instance = new $class; |
32
|
|
|
} |
33
|
2 |
|
return self::$instance; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check whether a specific information exists in cookie |
38
|
|
|
* @param string $name |
39
|
|
|
* @throws Exception if parameter is not a string |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
4 |
|
public function has($name) |
|
|
|
|
43
|
|
|
{ |
44
|
4 |
|
if (!is_string($name)) { |
45
|
3 |
|
throw new Exception('Parameter must be a string'); |
46
|
|
|
} |
47
|
4 |
|
return isset($_COOKIE[$name]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Define a new cookie |
52
|
|
|
* @param string $name |
53
|
|
|
* @param string $value |
54
|
|
|
* @param int $expire |
55
|
|
|
* @param string $path |
56
|
|
|
* @param string $domain |
57
|
|
|
* @param bool $secure |
58
|
|
|
* @param bool $httpOnly |
59
|
|
|
* @throws Exception if parameter is not a string |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
1 |
|
public function set($name, $value, $expire = 0, $path = "/", $domain = "", $secure = false, $httpOnly = false) |
63
|
|
|
{ |
64
|
1 |
|
if (!is_string($name)) { |
65
|
1 |
|
throw new Exception('Parameter must be a string'); |
66
|
|
|
} |
67
|
1 |
|
$time = time(); |
68
|
1 |
|
if ($expire < $time) { |
69
|
1 |
|
$expire = $time + $expire; |
70
|
|
|
} |
71
|
1 |
|
$this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); |
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Method that sends cookie - Unit test only |
77
|
|
|
* @codeCoverageIgnore |
78
|
|
|
* @param $name |
79
|
|
|
* @param $value |
80
|
|
|
* @param int $expire |
81
|
|
|
* @param string $path |
82
|
|
|
* @param string $domain |
83
|
|
|
* @param bool|false $secure |
84
|
|
|
* @param bool|false $httpOnly |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function setCookie( |
88
|
|
|
$name, |
89
|
|
|
$value, |
90
|
|
|
$expire = 0, |
91
|
|
|
$path = "/", |
92
|
|
|
$domain = "", |
93
|
|
|
$secure = false, |
94
|
|
|
$httpOnly = false |
95
|
|
|
) { |
96
|
|
|
return setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve a specific Cookie value |
101
|
|
|
* @param string $name |
102
|
|
|
* @throws Exception if parameter is not a string |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
1 |
|
public function get($name) |
|
|
|
|
106
|
|
|
{ |
107
|
1 |
|
return $this->has($name) ? $_COOKIE[$name] : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Remove a specific Cookie |
112
|
|
|
* @param string $name |
113
|
|
|
* @param string $path |
114
|
|
|
* @param string $domain |
115
|
|
|
* @param bool $secure |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
2 |
|
public function remove($name, $path = "/", $domain = "", $secure = false) |
|
|
|
|
119
|
|
|
{ |
120
|
2 |
|
if ($this->has($name)) { |
121
|
2 |
|
$this->setCookie($name, "", time() - (3600 * 24), $path, $domain, $secure); |
122
|
2 |
|
unset($_COOKIE[$name]); |
123
|
|
|
} |
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove all Cookies |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
1 |
|
public function destroy() |
|
|
|
|
132
|
|
|
{ |
133
|
1 |
|
foreach (array_keys($_COOKIE) as $cookie) { |
134
|
1 |
|
$this->remove($cookie); |
135
|
|
|
} |
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: