Passed
Pull Request — develop (#1)
by nguereza
02:57
created

HttpCookie::__construct()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 22
nc 9
nop 1
dl 0
loc 24
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant PHP
7
 * Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 * Copyright (c) 2011 - 2017 rehyved.com
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file HttpCookie.php
35
 *
36
 *  The Http Cookie class
37
 *
38
 *  @package    Platine\Framework\Http\Client
39
 *  @author Platine Developers team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   https://www.platine-php.com
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Framework\Http\Client;
50
51
/**
52
 * @class HttpCookie
53
 * @package Platine\Framework\Http\Client
54
 */
55
class HttpCookie
56
{
57
    /**
58
     * The name of cookie
59
     * @var string
60
     */
61
    protected string $name;
62
63
    /**
64
     * The value of cookie
65
     * @var string
66
     */
67
    protected string $value;
68
69
    /**
70
     * The cookie expires time
71
     * @var int
72
     */
73
    protected int $expires = 0;
74
75
    /**
76
     * The cookie max age
77
     * @var int
78
     */
79
    protected int $maxAge = 0;
80
81
    /**
82
     * The cookie domain
83
     * @var string|null
84
     */
85
    protected ?string $domain = null;
86
87
    /**
88
     * The cookie path
89
     * @var string
90
     */
91
    protected string $path = '/';
92
93
    /**
94
     * Whether the cookie is secure or not
95
     * @var bool
96
     */
97
    protected bool $secure = false;
98
99
    /**
100
     * Whether the cookie is for HTTP or not
101
     * @var bool
102
     */
103
    protected bool $httpOnly = true;
104
105
    /**
106
     * The value of the same site
107
     *
108
     * @var string|null
109
     */
110
    protected ?string $sameSite = null;
111
112
    /**
113
     * Create new instance
114
     * @param string $cookieString
115
     */
116
    public function __construct(string $cookieString)
117
    {
118
        $parts = array_map('trim', explode(';', $cookieString));
119
        foreach ($parts as $partStr) {
120
            $part = array_map('trim', explode('=', $partStr));
121
            $name = $part[0];
122
            $value = $part[1] ?? '';
123
            if (stripos($name, 'Expires') !== false) {
124
                $this->expires = strtotime($value);
125
            } elseif (stripos($name, 'Max-Age') !== false) {
126
                $this->maxAge = intval($value);
127
            } elseif (stripos($name, 'Domain') !== false) {
128
                $this->domain = $value;
129
            } elseif (stripos($name, 'Path') !== false) {
130
                $this->path = $value;
131
            } elseif (stripos($name, 'Secure') !== false) {
132
                $this->secure = true;
133
            } elseif (stripos($name, 'HttpOnly') !== false) {
134
                $this->httpOnly = true;
135
            } elseif (stripos($name, 'SameSite') !== false) {
136
                $this->sameSite = $value;
137
            } else {
138
                $this->name = $name;
139
                $this->value = $value;
140
            }
141
        }
142
    }
143
144
    /**
145
     *
146
     * @return string
147
     */
148
    public function getName(): string
149
    {
150
        return $this->name;
151
    }
152
153
    /**
154
     *
155
     * @return string
156
     */
157
    public function getValue(): string
158
    {
159
        return $this->value;
160
    }
161
162
    /**
163
     *
164
     * @return int
165
     */
166
    public function getExpires(): int
167
    {
168
        return $this->expires;
169
    }
170
171
    /**
172
     *
173
     * @return int
174
     */
175
    public function getMaxAge(): int
176
    {
177
        return $this->maxAge;
178
    }
179
180
    /**
181
     *
182
     * @return string|null
183
     */
184
    public function getDomain(): ?string
185
    {
186
        return $this->domain;
187
    }
188
189
    /**
190
     *
191
     * @return string
192
     */
193
    public function getPath(): string
194
    {
195
        return $this->path;
196
    }
197
198
    /**
199
     *
200
     * @return bool
201
     */
202
    public function isSecure(): bool
203
    {
204
        return $this->secure;
205
    }
206
207
    /**
208
     *
209
     * @return bool
210
     */
211
    public function isHttpOnly(): bool
212
    {
213
        return $this->httpOnly;
214
    }
215
216
    /**
217
     *
218
     * @return string|null
219
     */
220
    public function getSameSite(): ?string
221
    {
222
        return $this->sameSite;
223
    }
224
}
225