1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Mvc5\Cookie\Config; |
7
|
|
|
|
8
|
|
|
use Mvc5\Arg; |
9
|
|
|
use Mvc5\Cookie\Cookies; |
10
|
|
|
|
11
|
|
|
trait HttpCookies |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $config = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $defaults = [ |
22
|
|
|
Arg::EXPIRE => 0, |
23
|
|
|
Arg::PATH => '/', |
24
|
|
|
Arg::DOMAIN => '', |
25
|
|
|
Arg::SECURE => false, |
26
|
|
|
Arg::HTTP_ONLY => true |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $cookies |
31
|
|
|
* @param array $defaults |
32
|
|
|
*/ |
33
|
27 |
|
function __construct(array $cookies = [], array $defaults = []) |
34
|
|
|
{ |
35
|
27 |
|
$this->config = $cookies; |
36
|
27 |
|
$this->defaults = $defaults + $this->defaults; |
37
|
27 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
function all() : array |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return $this->config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
10 |
|
* @param string $value |
50
|
|
|
* @param int|string|null $expire |
51
|
|
|
* @param string|null $path |
52
|
|
|
* @param string|null $domain |
53
|
10 |
|
* @param bool|null $secure |
54
|
10 |
|
* @param bool|null $httponly |
55
|
10 |
|
* @return array |
56
|
10 |
|
*/ |
57
|
10 |
|
protected function cookie($name, $value, $expire = null, |
58
|
10 |
|
string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : array |
59
|
10 |
|
{ |
60
|
|
|
return [ |
61
|
|
|
Arg::NAME => (string) $name, |
62
|
|
|
Arg::VALUE => (string) $value, |
63
|
|
|
Arg::EXPIRE => is_string($expire ?? $expire = $this->defaults[Arg::EXPIRE]) ? $expire : (int) $expire, |
64
|
|
|
Arg::PATH => $path ?? $this->defaults[Arg::PATH], |
65
|
|
|
Arg::DOMAIN => $domain ?? $this->defaults[Arg::DOMAIN], |
66
|
|
|
Arg::SECURE => $secure ?? $this->defaults[Arg::SECURE], |
67
|
|
|
Arg::HTTP_ONLY => $httponly ?? $this->defaults[Arg::HTTP_ONLY] |
68
|
|
|
]; |
69
|
|
|
} |
70
|
3 |
|
|
71
|
|
|
/** |
72
|
3 |
|
* @param string $name |
73
|
3 |
|
* @param string|null $path |
74
|
|
|
* @param string|null $domain |
75
|
|
|
* @param bool|null $secure |
76
|
|
|
* @param bool|null $httponly |
77
|
|
|
*/ |
78
|
|
|
function remove($name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : void |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->set($name, '', 946706400, $path, $domain, $secure, $httponly); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $name |
85
|
3 |
|
* @param string $value |
86
|
|
|
* @param int|string|null $expire |
87
|
|
|
* @param string|null $path |
88
|
3 |
|
* @param string|null $domain |
89
|
3 |
|
* @param bool|null $secure |
90
|
|
|
* @param bool|null $httponly |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
function set($name, $value = null, $expire = null, |
|
|
|
|
94
|
|
|
string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) |
95
|
|
|
{ |
96
|
|
|
$this->config[$name] = $this->cookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
97
|
|
|
return $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $name |
102
|
2 |
|
* @param string|null $value |
103
|
|
|
* @param int|string|null $expire |
104
|
|
|
* @param string|null $path |
105
|
2 |
|
* @param string|null $domain |
106
|
2 |
|
* @param bool|null $secure |
107
|
2 |
|
* @param bool|null $httponly |
108
|
|
|
* @return self|mixed |
109
|
|
|
*/ |
110
|
|
|
function with($name, $value = null, $expire = null, |
|
|
|
|
111
|
|
|
string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : Cookies |
112
|
|
|
{ |
113
|
|
|
$new = clone $this; |
114
|
|
|
$new->set($name, $value, $expire, $path, $domain, $secure, $httponly); |
115
|
|
|
return $new; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
/** |
119
|
|
|
* @param string $name |
120
|
1 |
|
* @param string|null $path |
121
|
1 |
|
* @param string|null $domain |
122
|
1 |
|
* @param bool|null $secure |
123
|
|
|
* @param bool|null $httponly |
124
|
|
|
* @return self|mixed |
125
|
|
|
*/ |
126
|
|
|
function without($name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : Cookies |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$new = clone $this; |
129
|
|
|
$new->remove($name, $path, $domain, $secure, $httponly); |
130
|
|
|
return $new; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.