Overview

Namespaces

  • None
  • Wei
    • Validator

Classes

  • Wei\Validator\All
  • Wei\Validator\AllOf
  • Wei\Validator\Alnum
  • Wei\Validator\Alpha
  • Wei\Validator\BaseValidator
  • Wei\Validator\Between
  • Wei\Validator\Blank
  • Wei\Validator\Callback
  • Wei\Validator\CharLength
  • Wei\Validator\Chinese
  • Wei\Validator\Color
  • Wei\Validator\Contains
  • Wei\Validator\CreditCard
  • Wei\Validator\Date
  • Wei\Validator\DateTime
  • Wei\Validator\Decimal
  • Wei\Validator\Digit
  • Wei\Validator\Dir
  • Wei\Validator\DivisibleBy
  • Wei\Validator\DoubleByte
  • Wei\Validator\Email
  • Wei\Validator\EndsWith
  • Wei\Validator\EqualTo
  • Wei\Validator\Exists
  • Wei\Validator\FieldExists
  • Wei\Validator\File
  • Wei\Validator\GreaterThan
  • Wei\Validator\GreaterThanOrEqual
  • Wei\Validator\IdCardCn
  • Wei\Validator\IdCardHk
  • Wei\Validator\IdCardMo
  • Wei\Validator\IdCardTw
  • Wei\Validator\IdenticalTo
  • Wei\Validator\Image
  • Wei\Validator\In
  • Wei\Validator\Ip
  • Wei\Validator\Length
  • Wei\Validator\LessThan
  • Wei\Validator\LessThanOrEqual
  • Wei\Validator\Lowercase
  • Wei\Validator\Luhn
  • Wei\Validator\MaxLength
  • Wei\Validator\MinLength
  • Wei\Validator\MobileCn
  • Wei\Validator\NaturalNumber
  • Wei\Validator\NoneOf
  • Wei\Validator\Null
  • Wei\Validator\Number
  • Wei\Validator\OneOf
  • Wei\Validator\Password
  • Wei\Validator\Phone
  • Wei\Validator\PhoneCn
  • Wei\Validator\PlateNumberCn
  • Wei\Validator\PositiveInteger
  • Wei\Validator\PostcodeCn
  • Wei\Validator\Present
  • Wei\Validator\QQ
  • Wei\Validator\RecordExists
  • Wei\Validator\Regex
  • Wei\Validator\Required
  • Wei\Validator\SomeOf
  • Wei\Validator\StartsWith
  • Wei\Validator\Time
  • Wei\Validator\Tld
  • Wei\Validator\Type
  • Wei\Validator\Uppercase
  • Wei\Validator\Url
  • Wei\Validator\Uuid
  • Overview
  • Namespace
  • Function
  1: <?php
  2: /**
  3:  * Wei Framework
  4:  *
  5:  * @copyright   Copyright (c) 2008-2015 Twin Huang
  6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
  7:  */
  8: 
  9: namespace Wei;
 10: 
 11: /**
 12:  * A service that handles session data ($_SESSION)
 13:  *
 14:  * @author      Twin Huang <twinhuang@qq.com>
 15:  */
 16: class Session extends Base implements \ArrayAccess, \Countable, \IteratorAggregate
 17: {
 18:     /**
 19:      * The namespace to store session data
 20:      *
 21:      * @var string|false
 22:      */
 23:     protected $namespace = false;
 24: 
 25:     /**
 26:      * The session data
 27:      *
 28:      * @var array
 29:      */
 30:     protected $data = array();
 31: 
 32:     /**
 33:      * The session configuration options
 34:      *
 35:      * @var array
 36:      * @link http://php.net/manual/en/session.configuration.php
 37:      */
 38:     protected $inis = array();
 39: 
 40:     /**
 41:      * Constructor
 42:      *
 43:      * @param array $options
 44:      */
 45:     public function __construct(array $options = array())
 46:     {
 47:         parent::__construct($options);
 48:         $this->start();
 49:     }
 50: 
 51:     /**
 52:      * Start session
 53:      *
 54:      * @throws \RuntimeException When header has been sent
 55:      */
 56:     public function start()
 57:     {
 58:         if (!session_id()) {
 59:             $file = $line = null;
 60:             if (headers_sent($file, $line)) {
 61:                 throw new \RuntimeException(sprintf('Unable to start session, output started at %s:%s', $file, $line));
 62:             }
 63:             session_start();
 64:         }
 65: 
 66:         if ($this->namespace) {
 67:             if (!isset($_SESSION[$this->namespace])) {
 68:                 $_SESSION[$this->namespace] = array();
 69:             }
 70:             $this->data = &$_SESSION[$this->namespace];
 71:         } else {
 72:             $this->data = &$_SESSION;
 73:         }
 74:     }
 75: 
 76:     /**
 77:      * Get or set session
 78:      *
 79:      * @param  string $key The name of cookie
 80:      * @param  mixed $value The value of cookie
 81:      * @return mixed
 82:      */
 83:     public function __invoke($key, $value = null)
 84:     {
 85:         if (1 == func_num_args()) {
 86:             return $this->get($key);
 87:         } else {
 88:             return $this->set($key, $value);
 89:         }
 90:     }
 91: 
 92:     /**
 93:      * Returns session value
 94:      *
 95:      * @param  string $key    The name of session
 96:      * @param  mixed  $default The default parameter value if the session does not exist
 97:      * @return mixed
 98:      */
 99:     public function get($key, $default = null)
100:     {
101:         return isset($this->data[$key]) ? $this->data[$key] : $default;
102:     }
103: 
104:     /**
105:      * Set session data
106:      *
107:      * @param string|array $name The session name or A key-value array
108:      * @param mixed $value The session value
109:      * @return $this
110:      */
111:     public function set($name, $value = null)
112:     {
113:         if (!is_array($name)) {
114:             $this->data[$name] = $value;
115:         } else {
116:             foreach ($name as $key => $value) {
117:                 $this->data[$key] = $value;
118:             }
119:         }
120:         return $this;
121:     }
122: 
123:     /**
124:      * Remove session data by specified name
125:      *
126:      * @param string $name The name of session
127:      * @return $this
128:      */
129:     public function remove($name)
130:     {
131:         unset($this->data[$name]);
132:         return $this;
133:     }
134: 
135:     /**
136:      * Check if the session is exists
137:      *
138:      * @param string $name
139:      * @return bool
140:      */
141:     public function exists($name)
142:     {
143:         return array_key_exists($name, $this->data);
144:     }
145: 
146:     /**
147:      * Clear session data
148:      *
149:      * @return $this
150:      */
151:     public function clear()
152:     {
153:         $this->data = array();
154:         return $this;
155:     }
156: 
157:     /**
158:      * Destroy all session data
159:      *
160:      * @return $this
161:      */
162:     public function destroy()
163:     {
164:         if (session_id()) {
165:             session_destroy();
166:         }
167:         return $this->clear();
168:     }
169: 
170:     /**
171:      * Returns session data as array
172:      *
173:      * @return array
174:      */
175:     public function toArray()
176:     {
177:         return $this->data;
178:     }
179: 
180:     /**
181:      * Set session configuration options
182:      *
183:      * @param array $inis
184:      * @return $this
185:      */
186:     public function setInis($inis)
187:     {
188:         foreach ($inis as $name => $value) {
189:             ini_set('session.' . $name, $value);
190:         }
191:         $this->inis = $inis + $this->inis;
192:         return $this;
193:     }
194: 
195:     /**
196:      * Check if the offset exists
197:      *
198:      * @param  string $offset
199:      * @return bool
200:      */
201:     public function offsetExists($offset)
202:     {
203:         return array_key_exists($offset, $this->data);
204:     }
205: 
206:     /**
207:      * Get the offset value
208:      *
209:      * @param  string $offset
210:      * @return mixed
211:      */
212:     public function &offsetGet($offset)
213:     {
214:         return $this->data[$offset];
215:     }
216: 
217:     /**
218:      * Set the offset value
219:      *
220:      * @param string $offset
221:      * @param mixed $value
222:      */
223:     public function offsetSet($offset, $value)
224:     {
225:         $this->data[$offset] = $value;
226:     }
227: 
228:     /**
229:      * Unset the offset
230:      *
231:      * @param string $offset
232:      */
233:     public function offsetUnset($offset)
234:     {
235:         unset($this->data[$offset]);
236:     }
237: 
238:     /**
239:      * Return the length of data
240:      *
241:      * @return int the length of data
242:      */
243:     public function count()
244:     {
245:         return count($this->data);
246:     }
247: 
248:     /**
249:      * Retrieve an array iterator
250:      *
251:      * @return \ArrayIterator
252:      */
253:     public function getIterator()
254:     {
255:         return new \ArrayIterator($this->data);
256:     }
257: }
258: 
Wei Framework API documentation generated by ApiGen