Passed
Push — master ( b45126...8af3f1 )
by 世昌
03:18
created

Cookie   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
dl 0
loc 327
rs 10
c 1
b 0
f 0
wmc 29

22 Methods

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