Passed
Push — dev ( 6bd794...c11261 )
by 世昌
02:44
created

Cookie   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 301
rs 10
wmc 26

20 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 5 1
A getName() 0 3 1
A session() 0 4 1
A __construct() 0 5 1
A send() 0 3 1
A isHttpOnly() 0 3 1
A isSecure() 0 3 1
A getPath() 0 3 1
A full() 0 4 1
B __toString() 0 26 7
A secure() 0 4 1
A expire() 0 4 1
A path() 0 4 1
A getValue() 0 3 1
A httpOnly() 0 4 1
A domain() 0 4 1
A getExpire() 0 3 1
A isSession() 0 3 1
A getDomain() 0 3 1
A isFullTime() 0 3 1
1
<?php
2
namespace suda\framework\http;
3
4
use DateTime;
5
use DateTimeZone;
6
use JsonSerializable;
7
8
/**
9
 * Class Cookie
10
 * @package suda\framework\http
11
 */
12
class Cookie implements JsonSerializable
13
{
14
    /**
15
     * 名称
16
     *
17
     * @var string
18
     */
19
    protected $name;
20
    
21
    /**
22
     * 值
23
     *
24
     * @var string
25
     */
26
    protected $value;
27
28
    /**
29
     * 设置 HttpOnly
30
     *
31
     * @var boolean
32
     */
33
    protected $httpOnly = false;
34
35
    /**
36
     * Cookie Path
37
     *
38
     * @var string
39
     */
40
    protected $path = '/';
41
42
    /**
43
     * 设置域
44
     *
45
     * @var string|null
46
     */
47
    protected $domain = null;
48
49
    /**
50
     * 设置过期时间
51
     *
52
     * @var int
53
     */
54
    protected $expire = 0;
55
56
    /**
57
     * 是否使用HTTPS
58
     *
59
     * @var boolean
60
     */
61
    protected $secure = false;
62
63
    /**
64
     * 会话过期时间
65
     *
66
     * @var boolean
67
     */
68
    protected $session = false;
69
70
    /**
71
     * @var bool
72
     */
73
    private $fulltime = true;
74
75
76
    /**
77
     * Cookie constructor.
78
     * @param string $name
79
     * @param string $value
80
     * @param int $expire
81
     */
82
    public function __construct(string $name, string $value, int $expire = 0)
83
    {
84
        $this->name = $name;
85
        $this->value = $value;
86
        $this->expire = $expire;
87
    }
88
    
89
    /**
90
     * 设置 HTTP Only
91
     *
92
     * @param boolean $set
93
     * @return $this
94
     */
95
    public function httpOnly(bool $set = true)
96
    {
97
        $this->httpOnly = $set;
98
        return $this;
99
    }
100
101
    /**
102
     * 时长全部
103
     *
104
     * @param boolean $set
105
     * @return $this
106
     */
107
    public function full(bool $set = true)
108
    {
109
        $this->fulltime = $set;
110
        return $this;
111
    }
112
113
    /**
114
     * 设置安全模式
115
     *
116
     * @param boolean $set
117
     * @return $this
118
     */
119
    public function secure(bool $set = true)
120
    {
121
        $this->secure = $set;
122
        return $this;
123
    }
124
125
    /**
126
     * 设置路径
127
     *
128
     * @param string $set
129
     * @return $this
130
     */
131
    public function path(string $set = '/')
132
    {
133
        $this->path = $set;
134
        return $this;
135
    }
136
137
    /**
138
     * 设置过期时间
139
     *
140
     * @param integer $time
141
     * @return $this
142
     */
143
    public function expire(int $time = 1440)
144
    {
145
        $this->expire = $time;
146
        return $this;
147
    }
148
149
    /**
150
     * 设置cookie域
151
     *
152
     * @param string $set
153
     * @return $this
154
     *
155
     */
156
    public function domain(string $set)
157
    {
158
        $this->domain = $set;
159
        return $this;
160
    }
161
162
    /**
163
     * 获取值
164
     *
165
     * @return mixed
166
     */
167
    public function getValue()
168
    {
169
        return $this->value;
170
    }
171
    
172
    /**
173
     * Get the value of name
174
     */
175
    public function getName()
176
    {
177
        return $this->name;
178
    }
179
180
    /**
181
     * Get 设置 HttpOnly
182
     *
183
     * @return  boolean
184
     */
185
    public function isHttpOnly():bool
186
    {
187
        return $this->httpOnly;
188
    }
189
190
    /**
191
     * Get 设置域
192
     *
193
     * @return  string|null
194
     */
195
    public function getDomain()
196
    {
197
        return $this->domain;
198
    }
199
200
    /**
201
     * Get cookie Path
202
     *
203
     * @return  string
204
     */
205
    public function getPath()
206
    {
207
        return $this->path;
208
    }
209
210
    /**
211
     * Get 设置过期时间
212
     *
213
     * @return  int
214
     */
215
    public function getExpire()
216
    {
217
        return $this->expire;
218
    }
219
220
    /**
221
     * Get 是否使用HTTPS
222
     *
223
     * @return  boolean
224
     */
225
    public function isSecure():bool
226
    {
227
        return $this->secure;
228
    }
229
230
    /**
231
     * Get 会话过期时间
232
     *
233
     * @return  boolean
234
     */
235
    public function isSession():bool
236
    {
237
        return $this->session;
238
    }
239
240
    /**
241
     * Get the value of fulltime
242
     */
243
    public function isFullTime():bool
244
    {
245
        return $this->fulltime;
246
    }
247
    
248
    /**
249
     * 设置Session
250
     *
251
     * @param boolean $session
252
     * @return $this
253
     */
254
    public function session(bool $session = true)
255
    {
256
        $this->session = $session;
257
        return $this;
258
    }
259
260
    /**
261
     * 发送COOKIE
262
     *
263
     * @param Response $response
264
     * @return void
265
     */
266
    public function send(Response $response)
267
    {
268
        $response->header('Set-Cookie', $this);
269
    }
270
271
    /**
272
     * 发送文本
273
     *
274
     * @return string
275
     */
276
277
    public function __toString()
278
    {
279
        $cookie = sprintf('%s=%s', $this->name, $this->value);
280
281
        if ($this->expire !== 0) {
282
            $time = $this->fulltime ? $this->expire : time() + $this->expire;
283
            $dateTime = DateTime::createFromFormat('U', $time, new DateTimeZone('GMT'));
284
            $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format('D, d M Y H:i:s T'));
285
        }
286
287
        if ($this->domain !== null) {
288
            $cookie .= '; domain='.$this->domain;
289
        }
290
291
        if ($this->path) {
292
            $cookie .= '; path='.$this->path;
293
        }
294
295
        if ($this->secure) {
296
            $cookie .= '; secure';
297
        }
298
299
        if ($this->httpOnly) {
300
            $cookie .= '; httponly';
301
        }
302
        return $cookie;
303
    }
304
305
    /**
306
     * @return array|mixed
307
     */
308
    public function jsonSerialize()
309
    {
310
        return [
311
            'name' => $this->name,
312
            'value' => $this->value,
313
        ];
314
    }
315
}
316