<?php
namespace Yesf\Http;
use ArrayAccess;
class Session implements ArrayAccess {
private $id;
private $sess;
public static function generateId() {
return bin2hex(random_bytes(10));
}
public function __construct($id, $sess = null) {
$this->id = $id;
if (!empty($sess)) {
$this->sess = unserialize($sess);
} else {
$this->sess = [];
}
}
public function id() {
return $this->id;
}
public function has($offset) {
return isset($this->sess[$offset]);
}
public function get($offset) {
return isset($this->sess[$offset]) ? $this->sess[$offset] : null;
}
public function set($offset, $value) {
$this->sess[$offset] = $value;
}
public function delete($offset) {
unset($this->sess[$offset]);
}
public function clear() {
$this->sess = [];
}
public function encode() {
return serialize($this->sess);
}
public function offsetExists($offset) {
return $this->has($offset);
}
public function offsetGet($offset) {
return $this->get($offset);
}
public function offsetSet($offset, $value) {
$this->set($offset, $value);
}
public function offsetUnset($offset) {
$this->delete($offset);
}
}