Cookie::__toString()   B
last analyzed

Complexity

Conditions 8
Paths 96

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
nc 96
nop 0
dl 0
loc 30
rs 8.4444
c 1
b 0
f 0
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
    public function domain(string $set)
162
    {
163
        $this->domain = $set;
164
        return $this;
165
    }
166
167
    /**
168
     * 获取值
169
     *
170
     * @return mixed
171
     */
172
    public function getValue()
173
    {
174
        return $this->value;
175
    }
176
177
    /**
178
     * Get the value of name
179
     */
180
    public function getName()
181
    {
182
        return $this->name;
183
    }
184
185
    /**
186
     * Get 设置 HttpOnly
187
     *
188
     * @return  boolean
189
     */
190
    public function isHttpOnly(): bool
191
    {
192
        return $this->httpOnly;
193
    }
194
195
    /**
196
     * Get 设置域
197
     *
198
     * @return  string|null
199
     */
200
    public function getDomain()
201
    {
202
        return $this->domain;
203
    }
204
205
    /**
206
     * Get cookie Path
207
     *
208
     * @return  string
209
     */
210
    public function getPath()
211
    {
212
        return $this->path;
213
    }
214
215
    /**
216
     * Get 设置过期时间
217
     *
218
     * @return  int
219
     */
220
    public function getExpire()
221
    {
222
        return $this->expire;
223
    }
224
225
    /**
226
     * Get 是否使用HTTPS
227
     *
228
     * @return  boolean
229
     */
230
    public function isSecure(): bool
231
    {
232
        return $this->secure;
233
    }
234
235
    /**
236
     * Get 会话过期时间
237
     *
238
     * @return  boolean
239
     */
240
    public function isSession(): bool
241
    {
242
        return $this->session;
243
    }
244
245
    /**
246
     * Get the value of fulltime
247
     */
248
    public function isFullTime(): bool
249
    {
250
        return $this->fullTime;
251
    }
252
253
    /**
254
     * 设置Session
255
     *
256
     * @param boolean $session
257
     * @return $this
258
     */
259
    public function session(bool $session = true)
260
    {
261
        $this->session = $session;
262
        return $this;
263
    }
264
265
    /**
266
     * 发送COOKIE
267
     *
268
     * @param Response $response
269
     * @return void
270
     */
271
    public function send(Response $response)
272
    {
273
        $response->header('Set-Cookie', $this);
274
    }
275
276
    /**
277
     * @return bool
278
     */
279
    public function isSameSite(): bool
280
    {
281
        return $this->sameSite;
282
    }
283
284
    /**
285
     * @param bool $sameSite
286
     */
287
    public function setSameSite(bool $sameSite): void
288
    {
289
        $this->sameSite = $sameSite;
290
    }
291
292
293
    /**
294
     * 发送文本
295
     *
296
     * @return string
297
     */
298
299
    public function __toString()
300
    {
301
        $cookie = sprintf('%s=%s', $this->name, $this->value);
302
303
        if ($this->expire !== 0) {
304
            $time = $this->fullTime ? $this->expire : time() + $this->expire;
305
            $dateTime = DateTime::createFromFormat('U', $time, new DateTimeZone('GMT'));
306
            $cookie .= '; Expires=' . str_replace('+0000', '', $dateTime->format('D, d M Y H:i:s T'));
307
        }
308
309
        if ($this->domain !== null) {
310
            $cookie .= '; Domain=' . $this->domain;
311
        }
312
313
        if ($this->path) {
314
            $cookie .= '; Path=' . $this->path;
315
        }
316
317
        if ($this->secure) {
318
            $cookie .= '; Secure';
319
        }
320
321
        if ($this->httpOnly) {
322
            $cookie .= '; HttpOnly';
323
        }
324
325
        if ($this->sameSite) {
326
            $cookie .= '; SameSite=Strict';
327
        }
328
        return $cookie;
329
    }
330
331
    /**
332
     * @return array|mixed
333
     */
334
    public function jsonSerialize()
335
    {
336
        return [
337
            'name' => $this->name,
338
            'value' => $this->value,
339
        ];
340
    }
341
}
342