-
Notifications
You must be signed in to change notification settings - Fork 89
/
basket.php
239 lines (211 loc) · 4.62 KB
/
basket.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/*
Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
This file is part of the Fat-Free Framework (http://fatfreeframework.com).
This is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or later.
Fat-Free Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
*/
//! Session-based pseudo-mapper
class Basket extends Magic {
//@{ Error messages
const
E_Field='Undefined field %s';
//@}
protected
//! Session key
$key,
//! Current item identifier
$id,
//! Current item contents
$item=[];
/**
* Return TRUE if field is defined
* @return bool
* @param $key string
**/
function exists($key) {
return array_key_exists($key,$this->item);
}
/**
* Assign value to field
* @return scalar|FALSE
* @param $key string
* @param $val scalar
**/
function set($key,$val) {
return ($key=='_id')?FALSE:($this->item[$key]=$val);
}
/**
* Retrieve value of field
* @return scalar|FALSE
* @param $key string
**/
function &get($key) {
if ($key=='_id')
return $this->id;
if (array_key_exists($key,$this->item))
return $this->item[$key];
user_error(sprintf(self::E_Field,$key),E_USER_ERROR);
return FALSE;
}
/**
* Delete field
* @return NULL
* @param $key string
**/
function clear($key) {
unset($this->item[$key]);
}
/**
* Return items that match key/value pair;
* If no key/value pair specified, return all items
* @return array
* @param $key string
* @param $val mixed
**/
function find($key=NULL,$val=NULL) {
$out=[];
if (isset($_SESSION[$this->key])) {
foreach ($_SESSION[$this->key] as $id=>$item)
if (!isset($key) ||
array_key_exists($key,$item) && $item[$key]==$val ||
$key=='_id' && $id==$val) {
$obj=clone($this);
$obj->id=$id;
$obj->item=$item;
$out[]=$obj;
}
}
return $out;
}
/**
* Return first item that matches key/value pair
* @return object|FALSE
* @param $key string
* @param $val mixed
**/
function findone($key,$val) {
return ($data=$this->find($key,$val))?$data[0]:FALSE;
}
/**
* Map current item to matching key/value pair
* @return array
* @param $key string
* @param $val mixed
**/
function load($key,$val) {
if ($found=$this->find($key,$val)) {
$this->id=$found[0]->id;
return $this->item=$found[0]->item;
}
$this->reset();
return [];
}
/**
* Return TRUE if current item is empty/undefined
* @return bool
**/
function dry() {
return !$this->item;
}
/**
* Return number of items in basket
* @return int
**/
function count() {
return isset($_SESSION[$this->key])?count($_SESSION[$this->key]):0;
}
/**
* Save current item
* @return array
**/
function save() {
if (!$this->id)
$this->id=uniqid('',TRUE);
$_SESSION[$this->key][$this->id]=$this->item;
return $this->item;
}
/**
* Erase item matching key/value pair
* @return bool
* @param $key string
* @param $val mixed
**/
function erase($key,$val) {
$found=$this->find($key,$val);
if ($found && $id=$found[0]->id) {
unset($_SESSION[$this->key][$id]);
if ($id==$this->id)
$this->reset();
return TRUE;
}
return FALSE;
}
/**
* Reset cursor
* @return NULL
**/
function reset() {
$this->id=NULL;
$this->item=[];
}
/**
* Empty basket
* @return NULL
**/
function drop() {
unset($_SESSION[$this->key]);
}
/**
* Hydrate item using hive array variable
* @return NULL
* @param $var array|string
**/
function copyfrom($var) {
if (is_string($var))
$var=\Base::instance()->$var;
foreach ($var as $key=>$val)
$this->set($key,$val);
}
/**
* Populate hive array variable with item contents
* @return NULL
* @param $key string
**/
function copyto($key) {
$var=&\Base::instance()->ref($key);
foreach ($this->item as $key=>$field)
$var[$key]=$field;
}
/**
* Check out basket contents
* @return array
**/
function checkout() {
if (isset($_SESSION[$this->key])) {
$out=$_SESSION[$this->key];
unset($_SESSION[$this->key]);
return $out;
}
return [];
}
/**
* Instantiate class
* @return void
* @param $key string
**/
function __construct($key='basket') {
$this->key=$key;
if (session_status()!=PHP_SESSION_ACTIVE)
session_start();
Base::instance()->sync('SESSION');
$this->reset();
}
}