Overview

Namespaces

  • Baguette
    • Application
    • Response
    • Serializer
    • Session
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 
<?php

namespace Baguette\Session;

/**
 * Session class based on Array
 *
 * @author    USAMI Kenta <tadsan@zonu.me>
 * @copyright 2016 Baguette HQ
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
 * @link      http://php.net/manual/book.session.php
 */
class ArraySession implements SessionInterface
{
    /** @var array */
    private $session;
    /** @var string */
    private $id = '';
    /** @var string */
    private $name;

    const SESSID_CHARS = '-ABCDEFGHIJKLMNOPQRSQUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    public function __construct(array $options = []) {
        $this->name = isset($options['name']) ? $options['name'] : session_name();
    }

    /**
     * @return bool
     */
    public function start()
    {
        $this->session = [];
        $this->id = self::genid();

        return true;
    }

    /**
     * @param  string $id
     * @return string id
     */
    public function id($id = null)
    {
        if ($id !== null) {
            $this->id = $id;
        }

        return $this->id;
    }

    /**
     * @param  bool $_delete_old_session
     * @return bool
     */
    public function regenerateId($_delete_old_session = false)
    {
        $this->id = self::genid();

        return true;
    }

    /**
     * @param  string $key
     * @param  array  $options
     * @return mixed  $value
     */
    public function get($key, array $options = [])
    {
        if (array_key_exists($key, $this->session)) { return $this->session[$key]; }
        if (array_key_exists('default', $options)) { return $options['default']; }

        throw new \OutOfRangeException($key);
    }

    /**
     * @param string $key
     * @param mixed  $value
     */
    public function set($key, $value)
    {
        $this->session[$key] = $value;
    }

    /**
     * @return array
     */
    public function getAll()
    {
        return $this->session;
    }

    /**
     * @param  string $name
     * @return string
     */
    public function name($name = null)
    {
        if ($name !== null) {
            return $this->name;
        }

        $old = $this->name;
        $this->name = $name;

        return $old;
    }

    /**
     * @return bool
     */
    public function destroy()
    {
        $this->session = [];

        return true;
    }

    /**
     * @return string
     */
    private function genid()
    {
        return (new \RandomLib\Factory)
            ->getGenerator(new \SecurityLib\Strength(\SecurityLib\Strength::MEDIUM))
            ->generateString(26, self::SESSID_CHARS);
    }
}
ξ ^ω^)ξ < Baguette Not Framework API documentation generated by ApiGen