|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Session; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Common\Assert; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* SessionContainer |
|
31
|
|
|
* |
|
32
|
|
|
* Implements a session object based on namespaces which should prevent naming conflicts. |
|
33
|
|
|
* @author Celestino Diaz <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class SessionContainer implements \Countable, \IteratorAggregate{ |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
protected $sessionNamespace; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Class constructor. |
|
42
|
|
|
* @param string $sessionNamespace the namespace to use |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function __construct($sessionNamespace) { |
|
45
|
1 |
|
Assert::isString($sessionNamespace); |
|
46
|
1 |
|
$this->sessionNamespace = $sessionNamespace; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Checks if the session property is available. |
|
51
|
|
|
* @param string $property the property to check in the session |
|
52
|
|
|
* @throws \InvalidArgumentException |
|
53
|
|
|
* @return boolean check result |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function contains($property) { |
|
56
|
1 |
|
Assert::isString($property); |
|
57
|
1 |
|
return isset($_SESSION[$this->getNamespace($property)]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns the session property hold content or the default value. |
|
62
|
|
|
* @param string $property the session property to retrieve the content from |
|
63
|
|
|
* @param mixed $defaultValue the default value if the property des not exist |
|
64
|
|
|
* @throws \InvalidArgumentException |
|
65
|
|
|
* @return mixed the property hold content or the default value if the property does not exist |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function get($property, $defaultValue = null) { |
|
68
|
1 |
|
Assert::isString($property); |
|
69
|
|
|
|
|
70
|
1 |
|
if (!$this->contains($property)) { |
|
71
|
1 |
|
return $defaultValue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $_SESSION[$this->getNamespace($property)]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets the session property and assigns the content to it. |
|
79
|
|
|
* @param string $property the property to assign the content to |
|
80
|
|
|
* @param mixed $value the value to store |
|
81
|
|
|
* @throws \InvalidArgumentException |
|
82
|
|
|
* @return \Brickoo\Component\Session\SessionContainer |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function set($property, $value) { |
|
85
|
1 |
|
Assert::isString($property); |
|
86
|
1 |
|
$_SESSION[$this->getNamespace($property)] = $value; |
|
87
|
1 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Removes the session property if available. |
|
92
|
|
|
* @param string $property the property to remove |
|
93
|
|
|
* @throws \InvalidArgumentException |
|
94
|
|
|
* @return \Brickoo\Component\Session\SessionContainer |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function remove($property) { |
|
97
|
1 |
|
Assert::isString($property); |
|
98
|
|
|
|
|
99
|
1 |
|
if ($this->contains($property)) { |
|
100
|
1 |
|
unset($_SESSION[$this->getNamespace($property)]); |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Retrieve an external iterator |
|
108
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
109
|
|
|
* @return \ArrayIterator |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function getIterator() { |
|
112
|
1 |
|
return new \ArrayIterator($_SESSION); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Count entries of the container |
|
117
|
|
|
* @link http://php.net/manual/en/countable.count.php |
|
118
|
|
|
* @return integer count of entries |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function count() { |
|
121
|
1 |
|
return count($_SESSION); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns the property namespace name. |
|
126
|
|
|
* @param string $property the property to modify |
|
127
|
|
|
* @return string the session namespace of the property |
|
128
|
|
|
*/ |
|
129
|
4 |
|
private function getNamespace($property) { |
|
130
|
4 |
|
return $this->sessionNamespace.".".$property; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|