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 the HTTP request and response cookies
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: * @property Request $request A service that handles the HTTP request data
16: * @property Response $response A service that handles the HTTP response data
17: */
18: class Cookie extends Base implements \ArrayAccess, \IteratorAggregate
19: {
20: /**
21: * The cookie data
22: *
23: * @var array
24: */
25: protected $data = array();
26:
27: /**
28: * Constructor
29: *
30: * @param array $options
31: */
32: public function __construct(array $options = array())
33: {
34: parent::__construct($options);
35:
36: $this->data = &$this->request->getParameterReference('cookie');
37: }
38:
39: /**
40: * Get request cookie or set response cookie
41: *
42: * @param string $key the name of cookie
43: * @param mixed $value the value of cookie
44: * @param array $options options for set cookie
45: * @return mixed
46: */
47: public function __invoke($key, $value = null, $options = array())
48: {
49: if (1 == func_num_args()) {
50: return $this->get($key);
51: } else {
52: return $this->set($key, $value, $options);
53: }
54: }
55:
56: /**
57: * Get request cookie
58: *
59: * @param string $key
60: * @param mixed $default default value
61: * @return mixed
62: */
63: public function get($key, $default = null)
64: {
65: return isset($this->data[$key]) ? $this->data[$key] : $default;
66: }
67:
68: /**
69: * Set response cookie
70: *
71: * @param string $key The name of cookie
72: * @param mixed $value The value of cookie
73: * @param array $options
74: * @return $this
75: */
76: public function set($key, $value = null, array $options = array())
77: {
78: if (isset($options['expires']) && 0 > $options['expires'] && isset($this->data[$key])) {
79: unset($this->data[$key]);
80: } else {
81: $this->data[$key] = $value;
82: }
83:
84: $this->response->setCookie($key, $value, $options);
85:
86: return $this;
87: }
88:
89: /**
90: * Remove response cookie
91: *
92: * @param string $key the name of cookie
93: * @return $this
94: */
95: public function remove($key)
96: {
97: unset($this->data[$key]);
98: $this->response->removeCookie($key);
99: return $this;
100: }
101:
102: /**
103: * Clear all cookie data
104: *
105: * @return $this
106: */
107: public function clear()
108: {
109: $this->data = array();
110: return $this;
111: }
112:
113: /**
114: * Returns all cookie data
115: *
116: * @return array
117: */
118: public function toArray()
119: {
120: return $this->data;
121: }
122:
123: /**
124: * Check if the offset exists
125: *
126: * @param string $offset
127: * @return bool
128: */
129: public function offsetExists($offset)
130: {
131: return array_key_exists($offset, $this->data);
132: }
133:
134: /**
135: * Get the offset value
136: *
137: * @param string $offset
138: * @return mixed
139: */
140: public function &offsetGet($offset)
141: {
142: return $this->data[$offset];
143: }
144:
145: /**
146: * Set the offset value
147: *
148: * @param string $offset
149: * @param mixed $value
150: * @return mixed
151: */
152: public function offsetSet($offset, $value)
153: {
154: return $this->data[$offset] = $value;
155: }
156:
157: /**
158: * Unset the offset
159: *
160: * @param string $offset
161: * @return $this
162: */
163: public function offsetUnset($offset)
164: {
165: return $this->remove($offset);
166: }
167:
168: /**
169: * Retrieve an array iterator
170: *
171: * @return \ArrayIterator
172: */
173: public function getIterator()
174: {
175: return new \ArrayIterator($this->data);
176: }
177: }
178: