Skip to content

Commit 73e085c

Browse files
committed
1.1
1 parent 38414bc commit 73e085c

File tree

4 files changed

+152
-42
lines changed

4 files changed

+152
-42
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,62 @@ $cookie->set('username', 'sdd');
4242
public function has(string $key): bool;
4343
```
4444

45+
***
46+
4547
```php
4648
public function get(string $key, $default = null): mixed;
4749
```
4850

51+
***
52+
53+
```php
54+
public function pull(string $key, $default = null): mixed;
55+
```
56+
57+
***
58+
4959
```php
5060
public function set(string $key, string|bool|int|float $value, ?int $ttl = null): self;
5161
```
5262

63+
***
64+
5365
```php
54-
public function setArray(string[] $array, ?int $ttl = null): self;
66+
public function setArray(string[] $assoc, ?int $ttl = null): self;
5567
```
5668

69+
***
70+
5771
```php
5872
public function remove(string ...$key): bool;
5973
```
6074

75+
***
76+
6177
```php
62-
public function push(): bool;
78+
public function push(set $key, string|bool|int|float $value, ?int $ttl = null): string|bool|int|float;
6379
```
6480

81+
***
82+
6583
```php
6684
public function all(): array;
6785
```
6886

87+
***
88+
89+
```php
90+
public function flush(): bool;
91+
```
92+
93+
***
94+
6995
```php
7096
public function destroy(): bool;
7197
```
7298

99+
***
100+
73101

74102
## Credits
75103

src/Cookie.php

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.0
10+
* @version 1.1
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -16,8 +16,7 @@
1616
namespace InitPHP\Cookies;
1717

1818
use InitPHP\Cookies\Exception\CookieInvalidArgumentException;
19-
use InitPHP\ParameterBag\ParameterBag;
20-
use InitPHP\ParameterBag\ParameterBagInterface;
19+
use \InitPHP\ParameterBag\{ParameterBag, ParameterBagInterface};
2120

2221
class Cookie implements CookieInterface
2322
{
@@ -59,13 +58,13 @@ public function __construct(string $name, string $salt, array $options = [])
5958

6059
public function __destruct()
6160
{
62-
$this->push();
61+
$this->send();
6362
}
6463

6564
/**
6665
* @inheritDoc
6766
*/
68-
public function push(): bool
67+
public function send(): bool
6968
{
7069
if($this->isChange === FALSE){
7170
return true;
@@ -102,7 +101,13 @@ public function has(string $key): bool
102101
return false;
103102
}
104103
$get = $this->storage->get($key);
105-
return ($get['ttl'] === null || $get['ttl'] < \time());
104+
if(!\is_array($get)){
105+
return false;
106+
}
107+
if(($has = ($get['ttl'] === null || $get['ttl'] > \time())) === FALSE){
108+
$this->remove($key);
109+
}
110+
return $has;
106111
}
107112

108113
/**
@@ -113,13 +118,26 @@ public function get(string $key, $default = null)
113118
if(($get = $this->storage->get($key, null)) === null){
114119
return $default;
115120
}
121+
if(!\is_array($get)){
122+
return $default;
123+
}
116124
if($get['ttl'] !== null && $get['ttl'] > \time()){
117125
$this->remove($key);
118126
return $default;
119127
}
120128
return $get['value'];
121129
}
122130

131+
/**
132+
* @inheritDoc
133+
*/
134+
public function pull(string $key, $default = null)
135+
{
136+
$value = $this->get($key, $default);
137+
$this->remove($key);
138+
return $value;
139+
}
140+
123141
/**
124142
* @inheritDoc
125143
*/
@@ -128,11 +146,8 @@ public function set(string $key, $value, ?int $ttl = null): CookieInterface
128146
if(!\is_string($value) && !\is_bool($value) && !\is_numeric($value)){
129147
throw new CookieInvalidArgumentException('Cookie value can only be string, boolean or numeric.');
130148
}
149+
$ttl = $this->ttlCheck($ttl);
131150
$this->isChange = true;
132-
$ttl = ($ttl !== null) ? (int)\abs($ttl) + \time() : null;
133-
if($ttl === 0){
134-
$ttl = null;
135-
}
136151
$cookie = [
137152
'value' => $value,
138153
'ttl' => $ttl,
@@ -145,17 +160,13 @@ public function set(string $key, $value, ?int $ttl = null): CookieInterface
145160
/**
146161
* @inheritDoc
147162
*/
148-
public function setArray(array $array, ?int $ttl = null): CookieInterface
163+
public function setArray(array $assoc, ?int $ttl = null): CookieInterface
149164
{
150-
$ttl = ($ttl !== null) ? (int)\abs($ttl) + \time() : null;
151-
if($ttl === 0){
152-
$ttl = null;
153-
}
154-
165+
$ttl = $this->ttlCheck($ttl);
155166
$cookies = [];
156-
foreach ($array as $key => $value) {
167+
foreach ($assoc as $key => $value) {
157168
if(!\is_string($key)){
158-
throw new CookieInvalidArgumentException();
169+
throw new CookieInvalidArgumentException("\$assoc must be an associative array.");
159170
}
160171
if(!\is_string($value) && !\is_bool($value) && !\is_numeric($value)){
161172
throw new CookieInvalidArgumentException('Cookie value can only be string, boolean or numeric.');
@@ -170,19 +181,35 @@ public function setArray(array $array, ?int $ttl = null): CookieInterface
170181
return $this;
171182
}
172183

184+
/**
185+
* @inheritDoc
186+
*/
187+
public function push(string $key, $value, ?int $ttl = null)
188+
{
189+
$this->set($key, $value, $ttl);
190+
return $value;
191+
}
192+
173193
/**
174194
* @inheritDoc
175195
*/
176196
public function all(): array
177197
{
178198
$cookies = [];
179199
$now = \time();
180-
foreach ($this->storage->all() as $key => $value) {
181-
if($value['ttl'] !== null && $value['ttl'] > $now){
200+
$removes = []; $all = $this->storage->all();
201+
foreach ($all as $key => $value) {
202+
if($value['ttl'] !== null && $value['ttl'] < $now){
203+
$removes[] = $key;
182204
continue;
183205
}
184206
$cookies[$key] = $value['value'];
185207
}
208+
unset($all);
209+
if(!empty($removes)){
210+
$this->remove(...$removes);
211+
unset($removes);
212+
}
186213
return $cookies;
187214
}
188215

@@ -196,16 +223,29 @@ public function remove(string ...$key): CookieInterface
196223
return $this;
197224
}
198225

226+
/**
227+
* @inheritDoc
228+
*/
229+
public function flush(): bool
230+
{
231+
$this->isChange = true;
232+
$this->storage->clear();
233+
return true;
234+
}
235+
199236
/**
200237
* @inheritDoc
201238
*/
202239
public function destroy(): bool
203240
{
204241
$this->isChange = false;
205-
$this->storage->clear();
206-
return @\setcookie($this->name, '', [
242+
$delete = @\setcookie($this->name, '', [
207243
'expires' => (\time() - 86400)
208244
]);
245+
if($delete !== FALSE){
246+
$this->storage->clear();
247+
}
248+
return $delete;
209249
}
210250

211251
/**
@@ -259,4 +299,16 @@ protected function decode(): array
259299
return \unserialize($data['cookies']);
260300
}
261301

302+
protected function ttlCheck(?int $ttl): ?int
303+
{
304+
if($ttl === null){
305+
return null;
306+
}
307+
$ttl = (int)\abs($ttl);
308+
if($ttl === 0){
309+
throw new CookieInvalidArgumentException("\$ttl can be null or a positive integer.");
310+
}
311+
return $ttl + \time();
312+
}
313+
262314
}

src/CookieInterface.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.0
10+
* @version 1.1
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -21,19 +21,19 @@ interface CookieInterface
2121
{
2222

2323
/**
24-
* Çerezin varlığını kontrol eder.
24+
* Cookie varlığını kontrol eder.
2525
*
26-
* Süresi dolmuş bir çerez sorgulanmak istenirse; false döner ve çerezi kaldırmaya çalışır.
26+
* Süresi dolmuş bir cookie sorgulanmak istenirse; false döner ve cookie kaldırılır.
2727
*
2828
* @param string $key
2929
* @return bool
3030
*/
3131
public function has(string $key): bool;
3232

3333
/**
34-
* Çerezin değerini verir.
34+
* Cookie değerini verir.
3535
*
36-
* Süresi dolmuş ya da olmayan bir çerez istenirse $default döner.
36+
* Süresi dolmuş ya da olmayan bir cookie istenirse $default döner.
3737
*
3838
* @param string $key
3939
* @param mixed $default
@@ -42,10 +42,21 @@ public function has(string $key): bool;
4242
public function get(string $key, $default = null);
4343

4444
/**
45-
* Bir çerezi tanımlar.
45+
* Cookie değerini verir ve siler.
4646
*
47-
* Bu yöntem çerezi doğrudan kullanının tarayıcısna göndermez. Çerezin geçerli değerini değiştirir/tanımlar.
48-
* Yapılan değişiklik CookieInterface::__destruct() yönteminde ya da kendisinden sonraki CookieInterface::push() yöntemi ile tarayıcıya aktarılır.
47+
* CookieInterface::get() yönteminden farklı olarak cookie değeri getirildikten sonra cookie silinir.
48+
*
49+
* @param string $key
50+
* @param mixed $default
51+
* @return mixed
52+
*/
53+
public function pull(string $key, $default = null);
54+
55+
/**
56+
* Bir cookie tanımlar.
57+
*
58+
* Bu yöntem cookie doğrudan kullanının tarayıcısna göndermez. Cookie geçerli değerini değiştirir/tanımlar.
59+
* Yapılan değişiklik CookieInterface::__destruct() yönteminde ya da kendisinden sonraki CookieInterface::send() yöntemi ile tarayıcıya aktarılır.
4960
*
5061
* @param string $key
5162
* @param string|int|float|bool $value
@@ -59,12 +70,22 @@ public function set(string $key, $value, ?int $ttl = null): self;
5970
* İlişkisel bir dizi kullanarak bir cookie tanımlar.
6071
*
6172
*
62-
* @param string[] $array
73+
* @param string[] $assoc
6374
* @param int|null $ttl
6475
* @return $this
6576
* @throws CookieInvalidArgumentException
6677
*/
67-
public function setArray(array $array, ?int $ttl = null): self;
78+
public function setArray(array $assoc, ?int $ttl = null): self;
79+
80+
/**
81+
* Bir Cookie verisi set eder. CookieInterface::set() yönteminden farklı olarak bu yöntem geriye $value döndürür.
82+
*
83+
* @param string $key
84+
* @param string|int|float|bool $value
85+
* @param null|int $ttl
86+
* @return mixed
87+
*/
88+
public function push(string $key, $value, ?int $ttl = null);
6889

6990
/**
7091
* Tüm cookie verisini (süresi dolmayanları) ilişkisel bir dizi olarak verir.
@@ -73,6 +94,17 @@ public function setArray(array $array, ?int $ttl = null): self;
7394
*/
7495
public function all(): array;
7596

97+
/**
98+
* Cookie kaldırır.
99+
*
100+
* Bu yöntemde cookie doğrudan kullanının tarayıcısna gönderilmez. Cookie geçerli script içinde kaldırılır.
101+
* Yapılan değişiklik CookieInterface::__destruct() yönteminde ya da kendisinden sonraki CookieInterface::send() yöntemi ile tarayıcıya aktarılır.
102+
*
103+
* @param string ...$key
104+
* @return $this
105+
*/
106+
public function remove(string ...$key): self;
107+
76108
/**
77109
* Geçerli değişikleri (set,remove) kullanıcının browserına gönderir.
78110
*
@@ -81,18 +113,16 @@ public function all(): array;
81113
* @see setcookie()
82114
* @return bool
83115
*/
84-
public function push(): bool;
116+
public function send(): bool;
85117

86118
/**
87-
* Çerezi kaldırır.
119+
* Cookie kaldırılmadan sadece içeriğini boşaltır.
88120
*
89-
* Bu yöntem çerezi doğrudan kullanının tarayıcısna göndermez. Çerezin geçerli script içinde kaldırır.
90-
* Yapılan değişiklik CookieInterface::__destruct() yönteminde ya da kendisinden sonraki CookieInterface::push() yöntemi ile tarayıcıya aktarılır.
121+
* Yapılan değişiklik CookieInterface::__destruct() yönteminde ya da kendisinden sonraki CookieInterface::send() yöntemi ile tarayıcıya aktarılır.
91122
*
92-
* @param string ...$key
93-
* @return $this
123+
* @return bool
94124
*/
95-
public function remove(string ...$key): self;
125+
public function flush(): bool;
96126

97127
/**
98128
* Tüm cookieleri yok eder.

src/Exception/CookieInvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.0
10+
* @version 1.1
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

0 commit comments

Comments
 (0)