1: <?php
2:
3: 4: 5: 6: 7:
8:
9:
10:
11: 12: 13: 14: 15: 16:
17: class NSessionSection extends NObject implements IteratorAggregate, ArrayAccess
18: {
19:
20: private $session;
21:
22:
23: private $name;
24:
25:
26: private $data;
27:
28:
29: private $meta = FALSE;
30:
31:
32: public $warnOnUndefined = FALSE;
33:
34:
35: 36: 37:
38: public function __construct(NSession $session, $name)
39: {
40: if (!is_string($name)) {
41: throw new InvalidArgumentException("Session namespace must be a string, " . gettype($name) ." given.");
42: }
43:
44: $this->session = $session;
45: $this->name = $name;
46: }
47:
48:
49: 50: 51:
52: private function start()
53: {
54: if ($this->meta === FALSE) {
55: $this->session->start();
56: $this->data = & $_SESSION['__NF']['DATA'][$this->name];
57: $this->meta = & $_SESSION['__NF']['META'][$this->name];
58: }
59: }
60:
61:
62: 63: 64: 65:
66: public function getIterator()
67: {
68: $this->start();
69: if (isset($this->data)) {
70: return new ArrayIterator($this->data);
71: } else {
72: return new ArrayIterator;
73: }
74: }
75:
76:
77: 78: 79: 80: 81: 82:
83: public function __set($name, $value)
84: {
85: $this->start();
86: $this->data[$name] = $value;
87: if (is_object($value)) {
88: $this->meta[$name]['V'] = NClassReflection::from($value)->getAnnotation('serializationVersion');
89: }
90: }
91:
92:
93: 94: 95: 96: 97:
98: public function &__get($name)
99: {
100: $this->start();
101: if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
102: trigger_error("The variable '$name' does not exist in session section", E_USER_NOTICE);
103: }
104:
105: return $this->data[$name];
106: }
107:
108:
109: 110: 111: 112: 113:
114: public function __isset($name)
115: {
116: if ($this->session->exists()) {
117: $this->start();
118: }
119: return isset($this->data[$name]);
120: }
121:
122:
123: 124: 125: 126: 127:
128: public function __unset($name)
129: {
130: $this->start();
131: unset($this->data[$name], $this->meta[$name]);
132: }
133:
134:
135: 136: 137: 138: 139: 140:
141: public function offsetSet($name, $value)
142: {
143: $this->__set($name, $value);
144: }
145:
146:
147: 148: 149: 150: 151:
152: public function offsetGet($name)
153: {
154: return $this->__get($name);
155: }
156:
157:
158: 159: 160: 161: 162:
163: public function offsetExists($name)
164: {
165: return $this->__isset($name);
166: }
167:
168:
169: 170: 171: 172: 173:
174: public function offsetUnset($name)
175: {
176: $this->__unset($name);
177: }
178:
179:
180: 181: 182: 183: 184: 185:
186: public function setExpiration($time, $variables = NULL)
187: {
188: $this->start();
189: if (empty($time)) {
190: $time = NULL;
191: $whenBrowserIsClosed = TRUE;
192: } else {
193: $time = NDateTime53::from($time)->format('U');
194: $max = ini_get('session.gc_maxlifetime');
195: if ($max !== 0 && ($time - time() > $max + 3)) {
196: trigger_error("The expiration time is greater than the session expiration $max seconds", E_USER_NOTICE);
197: }
198: $whenBrowserIsClosed = FALSE;
199: }
200:
201: if ($variables === NULL) {
202: $this->meta['']['T'] = $time;
203: $this->meta['']['B'] = $whenBrowserIsClosed;
204:
205: } elseif (is_array($variables)) {
206: foreach ($variables as $variable) {
207: $this->meta[$variable]['T'] = $time;
208: $this->meta[$variable]['B'] = $whenBrowserIsClosed;
209: }
210:
211: } else {
212: $this->meta[$variables]['T'] = $time;
213: $this->meta[$variables]['B'] = $whenBrowserIsClosed;
214: }
215: return $this;
216: }
217:
218:
219: 220: 221: 222: 223:
224: public function removeExpiration($variables = NULL)
225: {
226: $this->start();
227: if ($variables === NULL) {
228:
229: unset($this->meta['']['T'], $this->meta['']['B']);
230:
231: } elseif (is_array($variables)) {
232:
233: foreach ($variables as $variable) {
234: unset($this->meta[$variable]['T'], $this->meta[$variable]['B']);
235: }
236: } else {
237: unset($this->meta[$variables]['T'], $this->meta[$variables]['B']);
238: }
239: }
240:
241:
242: 243: 244: 245:
246: public function remove()
247: {
248: $this->start();
249: $this->data = NULL;
250: $this->meta = NULL;
251: }
252:
253: }
254: