|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Session; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Iterator; |
|
22
|
|
|
use Limoncello\Application\Contracts\Session\SessionFunctionsInterface; |
|
23
|
|
|
use Limoncello\Contracts\Session\SessionInterface; |
|
24
|
|
|
use function assert; |
|
25
|
|
|
use function call_user_func; |
|
26
|
|
|
use function is_bool; |
|
27
|
|
|
use function is_int; |
|
28
|
|
|
use function is_string; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @package Limoncello\Application |
|
32
|
|
|
*/ |
|
33
|
|
|
class Session implements SessionInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
2 |
|
* @var SessionFunctionsInterface |
|
37
|
|
|
*/ |
|
38
|
2 |
|
private $functions; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param SessionFunctionsInterface $functions |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(SessionFunctionsInterface $functions) |
|
44
|
1 |
|
{ |
|
45
|
|
|
$this->functions = $functions; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
1 |
|
*/ |
|
51
|
|
|
public function getIterator() |
|
52
|
|
|
{ |
|
53
|
|
|
$iterator = call_user_func($this->getFunctions()->getIteratorCallable()); |
|
54
|
|
|
|
|
55
|
|
|
assert($iterator instanceof Iterator); |
|
56
|
1 |
|
|
|
57
|
|
|
return $iterator; |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
1 |
|
*/ |
|
63
|
|
|
public function offsetExists($key) |
|
64
|
1 |
|
{ |
|
65
|
|
|
assert(is_string($key) || is_int($key)); |
|
66
|
|
|
|
|
67
|
|
|
$exists = call_user_func($this->getFunctions()->getHasCallable(), $key); |
|
68
|
|
|
|
|
69
|
|
|
assert(is_bool($exists)); |
|
70
|
1 |
|
|
|
71
|
|
|
return $exists; |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
1 |
|
*/ |
|
77
|
|
|
public function offsetGet($key) |
|
78
|
|
|
{ |
|
79
|
|
|
assert(is_string($key) || is_int($key)); |
|
80
|
|
|
|
|
81
|
|
|
$value = call_user_func($this->getFunctions()->getRetrieveCallable(), $key); |
|
82
|
1 |
|
|
|
83
|
|
|
return $value; |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function offsetSet($key, $value) |
|
90
|
|
|
{ |
|
91
|
|
|
assert(is_string($key) || is_int($key)); |
|
92
|
1 |
|
|
|
93
|
|
|
call_user_func($this->getFunctions()->getPutCallable(), $key, $value); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function offsetUnset($key) |
|
100
|
|
|
{ |
|
101
|
|
|
assert(is_string($key) || is_int($key)); |
|
102
|
1 |
|
|
|
103
|
|
|
call_user_func($this->getFunctions()->getDeleteCallable(), $key); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return SessionFunctionsInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getFunctions(): SessionFunctionsInterface |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->functions; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|