1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElePHPant\Cookie\Cookie; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ElePHPant\Cookie\Storage\{CookieStorage, CookieStorageInterface}; |
7
|
|
|
use ElePHPant\Cookie\Strategies\Encryption\EncryptionStrategyInterface; |
8
|
|
|
use ElePHPant\Cookie\ValueObjects\{Config, Expiration}; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CookieTrait |
12
|
|
|
* |
13
|
|
|
* Please report bugs on https://github.com/wilderamorim/cookie/issues |
14
|
|
|
* |
15
|
|
|
* @author Wilder Amorim <https://github.com/wilderamorim> |
16
|
|
|
* @link https://www.linkedin.com/in/wilderamorim/ |
17
|
|
|
*/ |
18
|
|
|
trait CookieTrait |
19
|
|
|
{ |
20
|
|
|
/** @var array Stores the configuration array. */ |
21
|
|
|
protected static array $configs = []; |
22
|
|
|
|
23
|
|
|
/** @var bool Indicates if encryption is enabled */ |
24
|
|
|
protected static bool $isEncrypted = false; |
25
|
|
|
|
26
|
|
|
/** @var CookieStorageInterface Storage object for managing cookies. */ |
27
|
|
|
protected static CookieStorageInterface $storage; |
28
|
|
|
|
29
|
|
|
/** @var EncryptionStrategyInterface|null Encryption strategy object. */ |
30
|
|
|
protected static ?EncryptionStrategyInterface $encryptionStrategy; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Process the configuration array. |
34
|
|
|
* |
35
|
|
|
* @param array $configs The configuration array. |
36
|
|
|
*/ |
37
|
|
|
protected static function processConfig(array $configs): void |
38
|
|
|
{ |
39
|
|
|
self::boot($configs); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initialize the trait by bootstrapping the necessary components. |
44
|
|
|
* |
45
|
|
|
* @param array $configs The configuration array. |
46
|
|
|
*/ |
47
|
|
|
private static function boot(array $configs): void |
48
|
|
|
{ |
49
|
|
|
self::$storage = new CookieStorage(); |
50
|
|
|
|
51
|
|
|
self::$configs = (new Config($configs))->toArray(); |
52
|
|
|
self::$configs['expiration'] = new Expiration(self::$configs['expiration'] ?? 'minutes'); |
53
|
|
|
|
54
|
|
|
if (!empty(self::$configs['encryption'])) { |
55
|
|
|
self::$isEncrypted = true; |
56
|
|
|
self::$encryptionStrategy = new self::$configs['encryption'](self::$configs); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |