1: <?php
2:
3: /*
4:
5: Copyright (c) 2009-2015 F3::Factory/Bong Cosca, All rights reserved.
6:
7: This file is part of the Fat-Free Framework (http://fatfreeframework.com).
8:
9: This is free software: you can redistribute it and/or modify it under the
10: terms of the GNU General Public License as published by the Free Software
11: Foundation, either version 3 of the License, or later.
12:
13: Fat-Free Framework is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: General Public License for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
20:
21: */
22:
23: //! Session-based pseudo-mapper
24: class Basket extends Magic {
25:
26: //@{ Error messages
27: const
28: E_Field='Undefined field %s';
29: //@}
30:
31: protected
32: //! Session key
33: $key,
34: //! Current item identifier
35: $id,
36: //! Current item contents
37: $item=array();
38:
39: /**
40: * Return TRUE if field is defined
41: * @return bool
42: * @param $key string
43: **/
44: function exists($key) {
45: return array_key_exists($key,$this->item);
46: }
47:
48: /**
49: * Assign value to field
50: * @return scalar|FALSE
51: * @param $key string
52: * @param $val scalar
53: **/
54: function set($key,$val) {
55: return ($key=='_id')?FALSE:($this->item[$key]=$val);
56: }
57:
58: /**
59: * Retrieve value of field
60: * @return scalar|FALSE
61: * @param $key string
62: **/
63: function &get($key) {
64: if ($key=='_id')
65: return $this->id;
66: if (array_key_exists($key,$this->item))
67: return $this->item[$key];
68: user_error(sprintf(self::E_Field,$key),E_USER_ERROR);
69: return FALSE;
70: }
71:
72: /**
73: * Delete field
74: * @return NULL
75: * @param $key string
76: **/
77: function clear($key) {
78: unset($this->item[$key]);
79: }
80:
81: /**
82: * Return items that match key/value pair;
83: * If no key/value pair specified, return all items
84: * @return array
85: * @param $key string
86: * @param $val mixed
87: **/
88: function find($key=NULL,$val=NULL) {
89: $out=array();
90: if (isset($_SESSION[$this->key])) {
91: foreach ($_SESSION[$this->key] as $id=>$item)
92: if (!isset($key) ||
93: array_key_exists($key,$item) && $item[$key]==$val) {
94: $obj=clone($this);
95: $obj->id=$id;
96: $obj->item=$item;
97: $out[]=$obj;
98: }
99: }
100: return $out;
101: }
102:
103: /**
104: * Return first item that matches key/value pair
105: * @return object|FALSE
106: * @param $key string
107: * @param $val mixed
108: **/
109: function findone($key,$val) {
110: return ($data=$this->find($key,$val))?$data[0]:FALSE;
111: }
112:
113: /**
114: * Map current item to matching key/value pair
115: * @return array
116: * @param $key string
117: * @param $val mixed
118: **/
119: function load($key,$val) {
120: if ($found=$this->find($key,$val)) {
121: $this->id=$found[0]->id;
122: return $this->item=$found[0]->item;
123: }
124: $this->reset();
125: return array();
126: }
127:
128: /**
129: * Return TRUE if current item is empty/undefined
130: * @return bool
131: **/
132: function dry() {
133: return !$this->item;
134: }
135:
136: /**
137: * Return number of items in basket
138: * @return int
139: **/
140: function count() {
141: return isset($_SESSION[$this->key])?count($_SESSION[$this->key]):0;
142: }
143:
144: /**
145: * Save current item
146: * @return array
147: **/
148: function save() {
149: if (!$this->id)
150: $this->id=uniqid(NULL,TRUE);
151: $_SESSION[$this->key][$this->id]=$this->item;
152: return $this->item;
153: }
154:
155: /**
156: * Erase item matching key/value pair
157: * @return bool
158: * @param $key string
159: * @param $val mixed
160: **/
161: function erase($key,$val) {
162: $found=$this->find($key,$val);
163: if ($found && $id=$found[0]->id) {
164: unset($_SESSION[$this->key][$id]);
165: if ($id==$this->id)
166: $this->reset();
167: return TRUE;
168: }
169: return FALSE;
170: }
171:
172: /**
173: * Reset cursor
174: * @return NULL
175: **/
176: function reset() {
177: $this->id=NULL;
178: $this->item=array();
179: }
180:
181: /**
182: * Empty basket
183: * @return NULL
184: **/
185: function drop() {
186: unset($_SESSION[$this->key]);
187: }
188:
189: /**
190: * Hydrate item using hive array variable
191: * @return NULL
192: * @param $var array|string
193: **/
194: function copyfrom($var) {
195: if (is_string($var))
196: $var=\Base::instance()->get($var);
197: foreach ($var as $key=>$val)
198: $this->item[$key]=$val;
199: }
200:
201: /**
202: * Populate hive array variable with item contents
203: * @return NULL
204: * @param $key string
205: **/
206: function copyto($key) {
207: $var=&\Base::instance()->ref($key);
208: foreach ($this->item as $key=>$field)
209: $var[$key]=$field;
210: }
211:
212: /**
213: * Check out basket contents
214: * @return array
215: **/
216: function checkout() {
217: if (isset($_SESSION[$this->key])) {
218: $out=$_SESSION[$this->key];
219: unset($_SESSION[$this->key]);
220: return $out;
221: }
222: return array();
223: }
224:
225: /**
226: * Instantiate class
227: * @return void
228: * @param $key string
229: **/
230: function __construct($key='basket') {
231: $this->key=$key;
232: @session_start();
233: Base::instance()->sync('SESSION');
234: $this->reset();
235: }
236:
237: }
238: