1
|
|
|
<?php namespace Comodojo\Cookies;
|
2
|
|
|
|
3
|
|
|
use \Comodojo\Exception\CookieException;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Plain cookie
|
7
|
|
|
*
|
8
|
|
|
* @package Comodojo Spare Parts
|
9
|
|
|
* @author Marco Giovinazzi <[email protected]>
|
10
|
|
|
* @license MIT
|
11
|
|
|
*
|
12
|
|
|
* LICENSE:
|
13
|
|
|
*
|
14
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
|
|
* THE SOFTWARE.
|
21
|
|
|
*/
|
22
|
|
|
|
23
|
|
|
class Cookie extends AbstractCookie {
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* Set cookie content
|
27
|
|
|
*
|
28
|
|
|
* @param mixed $value Cookie content
|
29
|
|
|
* @param bool $serialize If true (default) cookie will be serialized first
|
30
|
|
|
*
|
31
|
|
|
* @return \Comodojo\Cookies\Cookie
|
32
|
|
|
*
|
33
|
|
|
* @throws \Comodojo\Exception\CookieException
|
34
|
|
|
*/
|
35
|
15 |
|
public function setValue($value, $serialize = true) {
|
36
|
|
|
|
37
|
15 |
|
if ( !is_scalar($value) && $serialize === false ) throw new CookieException("Cannot set non-scalar value without serialization");
|
38
|
|
|
|
39
|
15 |
|
$cookie_value = $serialize === true ? serialize($value) : $value;
|
40
|
|
|
|
41
|
15 |
View Code Duplication |
if ( strlen($cookie_value) > $this->max_cookie_size ) throw new CookieException("Cookie size larger than ".$this->max_cookie_size." bytes");
|
|
|
|
|
42
|
|
|
|
43
|
12 |
|
$this->value = $cookie_value;
|
44
|
|
|
|
45
|
12 |
|
return $this;
|
46
|
|
|
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* Get cookie content
|
51
|
|
|
*
|
52
|
|
|
* @param bool $unserialize If true (default) cookie will be unserialized first
|
53
|
|
|
*
|
54
|
|
|
* @return mixed
|
55
|
|
|
*/
|
56
|
12 |
|
public function getValue($unserialize = true) {
|
57
|
|
|
|
58
|
12 |
|
return $unserialize ? unserialize($this->value) : $this->value;
|
59
|
|
|
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* Static method to create a cookie quickly
|
64
|
|
|
*
|
65
|
|
|
* @param string $name The cookie name
|
66
|
|
|
*
|
67
|
|
|
* @param array $properties Array of properties cookie should have
|
68
|
|
|
*
|
69
|
|
|
* @return \Comodojo\Cookies\Cookie
|
70
|
|
|
*
|
71
|
|
|
* @throws \Comodojo\Exception\CookieException
|
72
|
|
|
*/
|
73
|
3 |
|
public static function create($name, $properties = [], $serialize = true) {
|
74
|
|
|
|
75
|
|
|
try {
|
76
|
|
|
|
77
|
3 |
|
$class = get_called_class();
|
78
|
|
|
|
79
|
3 |
|
$cookie = new $class($name);
|
80
|
|
|
|
81
|
3 |
|
self::cookieProperties($cookie, $properties, $serialize);
|
82
|
|
|
|
83
|
3 |
|
} catch (CookieException $ce) {
|
84
|
|
|
|
85
|
|
|
throw $ce;
|
86
|
|
|
|
87
|
|
|
}
|
88
|
|
|
|
89
|
3 |
|
return $cookie;
|
90
|
|
|
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* Static method to get a cookie quickly
|
95
|
|
|
*
|
96
|
|
|
* @param string $name The cookie name
|
97
|
|
|
*
|
98
|
|
|
* @return \Comodojo\Cookies\Cookie
|
99
|
|
|
*
|
100
|
|
|
* @throws \Comodojo\Exception\CookieException
|
101
|
|
|
*/
|
102
|
3 |
View Code Duplication |
public static function retrieve($name) {
|
|
|
|
|
103
|
|
|
|
104
|
|
|
try {
|
105
|
|
|
|
106
|
3 |
|
$class = get_called_class();
|
107
|
|
|
|
108
|
3 |
|
$cookie = new $class($name);
|
109
|
|
|
|
110
|
3 |
|
$return = $cookie->load();
|
111
|
|
|
|
112
|
3 |
|
} catch (CookieException $ce) {
|
113
|
|
|
|
114
|
3 |
|
throw $ce;
|
115
|
|
|
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
return $return;
|
119
|
|
|
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
}
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.