basket.php
4.21 KB
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
<?php
/*
Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
This file is part of the Fat-Free Framework (http://fatfree.sf.net).
THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Please see the license.txt file for more information.
*/
//! Session-based pseudo-mapper
class Basket {
//@{ Error messages
const
E_Field='Undefined field %s';
//@}
protected
//! Session key
$key,
//! Current item identifier
$id,
//! Current item contents
$item=array();
/**
* 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));
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|FALSE
* @param $key string
* @param $val mixed
**/
function find($key=NULL,$val=NULL) {
if (isset($_SESSION[$this->key])) {
$out=array();
foreach ($_SESSION[$this->key] as $id=>$item)
if (!isset($key) ||
array_key_exists($key,$item) && $item[$key]==$val) {
$obj=clone($this);
$obj->id=$id;
$obj->item=$item;
$out[]=$obj;
}
return $out;
}
return FALSE;
}
/**
* 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 array();
}
/**
* 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(NULL,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=array();
}
/**
* Empty basket
* @return NULL
**/
function drop() {
unset($_SESSION[$this->key]);
}
/**
* Hydrate item using hive array variable
* @return NULL
* @param $key string
**/
function copyfrom($key) {
foreach (\Base::instance()->get($key) as $key=>$val)
$this->item[$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 array();
}
/**
* Instantiate class
* @return void
* @param $key string
**/
function __construct($key='basket') {
$this->key=$key;
@session_start();
Base::instance()->sync('SESSION');
$this->reset();
}
}