1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* sessionware (https://github.com/juliangut/sessionware). |
5
|
|
|
* PSR7 compatible session management. |
6
|
|
|
* |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @link https://github.com/juliangut/sessionware |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Jgut\Sessionware; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Data collection. |
18
|
|
|
*/ |
19
|
|
|
class Collection |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Collection data. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $data = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Collection constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $initialData |
32
|
|
|
* |
33
|
|
|
* @throws \InvalidArgumentException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $initialData = []) |
36
|
|
|
{ |
37
|
|
|
foreach ($initialData as $key => $value) { |
38
|
|
|
$this->set($key, $value); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Collection data existence. |
44
|
|
|
* |
45
|
|
|
* @param string $key |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function has(string $key) : bool |
50
|
|
|
{ |
51
|
|
|
return array_key_exists($key, $this->data); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all data. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getAll() : array |
60
|
|
|
{ |
61
|
|
|
return $this->data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Retrieve collection data. |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* @param mixed|null $default |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function get(string $key, $default = null) |
73
|
|
|
{ |
74
|
|
|
return array_key_exists($key, $this->data) ? $this->data[$key] : $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set collection data. |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @param mixed $value |
82
|
|
|
* |
83
|
|
|
* @throws \InvalidArgumentException |
84
|
|
|
*/ |
85
|
|
|
public function set(string $key, $value) |
86
|
|
|
{ |
87
|
|
|
$this->verifyScalarValue($value); |
88
|
|
|
|
89
|
|
|
$this->data[$key] = $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Verify only scalar values allowed. |
94
|
|
|
* |
95
|
|
|
* @param string|int|float|bool|array $value |
96
|
|
|
* |
97
|
|
|
* @throws \InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
final protected function verifyScalarValue($value) |
100
|
|
|
{ |
101
|
|
|
if (is_array($value)) { |
102
|
|
|
foreach ($value as $val) { |
103
|
|
|
$this->verifyScalarValue($val); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!is_scalar($value)) { |
108
|
|
|
throw new \InvalidArgumentException(sprintf('Session values must be scalars, %s given', gettype($value))); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Remove collection data. |
114
|
|
|
* |
115
|
|
|
* @param string $key |
116
|
|
|
*/ |
117
|
|
|
public function remove(string $key) |
118
|
|
|
{ |
119
|
|
|
if (array_key_exists($key, $this->data)) { |
120
|
|
|
unset($this->data[$key]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Remove all collection data. |
126
|
|
|
*/ |
127
|
|
|
public function clear() |
128
|
|
|
{ |
129
|
|
|
$this->data = []; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|