basket.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /*
  3. Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  5. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. PURPOSE.
  9. Please see the license.txt file for more information.
  10. */
  11. //! Session-based pseudo-mapper
  12. class Basket {
  13. //@{ Error messages
  14. const
  15. E_Field='Undefined field %s';
  16. //@}
  17. protected
  18. //! Session key
  19. $key,
  20. //! Current item identifier
  21. $id,
  22. //! Current item contents
  23. $item=array();
  24. /**
  25. * Return TRUE if field is defined
  26. * @return bool
  27. * @param $key string
  28. **/
  29. function exists($key) {
  30. return array_key_exists($key,$this->item);
  31. }
  32. /**
  33. * Assign value to field
  34. * @return scalar|FALSE
  35. * @param $key string
  36. * @param $val scalar
  37. **/
  38. function set($key,$val) {
  39. return ($key=='_id')?FALSE:($this->item[$key]=$val);
  40. }
  41. /**
  42. * Retrieve value of field
  43. * @return scalar|FALSE
  44. * @param $key string
  45. **/
  46. function get($key) {
  47. if ($key=='_id')
  48. return $this->id;
  49. if (array_key_exists($key,$this->item))
  50. return $this->item[$key];
  51. user_error(sprintf(self::E_Field,$key));
  52. return FALSE;
  53. }
  54. /**
  55. * Delete field
  56. * @return NULL
  57. * @param $key string
  58. **/
  59. function clear($key) {
  60. unset($this->item[$key]);
  61. }
  62. /**
  63. * Return items that match key/value pair;
  64. * If no key/value pair specified, return all items
  65. * @return array|FALSE
  66. * @param $key string
  67. * @param $val mixed
  68. **/
  69. function find($key=NULL,$val=NULL) {
  70. if (isset($_SESSION[$this->key])) {
  71. $out=array();
  72. foreach ($_SESSION[$this->key] as $id=>$item)
  73. if (!isset($key) ||
  74. array_key_exists($key,$item) && $item[$key]==$val) {
  75. $obj=clone($this);
  76. $obj->id=$id;
  77. $obj->item=$item;
  78. $out[]=$obj;
  79. }
  80. return $out;
  81. }
  82. return FALSE;
  83. }
  84. /**
  85. * Return first item that matches key/value pair
  86. * @return object|FALSE
  87. * @param $key string
  88. * @param $val mixed
  89. **/
  90. function findone($key,$val) {
  91. return ($data=$this->find($key,$val))?$data[0]:FALSE;
  92. }
  93. /**
  94. * Map current item to matching key/value pair
  95. * @return array
  96. * @param $key string
  97. * @param $val mixed
  98. **/
  99. function load($key,$val) {
  100. if ($found=$this->find($key,$val)) {
  101. $this->id=$found[0]->id;
  102. return $this->item=$found[0]->item;
  103. }
  104. $this->reset();
  105. return array();
  106. }
  107. /**
  108. * Return TRUE if current item is empty/undefined
  109. * @return bool
  110. **/
  111. function dry() {
  112. return !$this->item;
  113. }
  114. /**
  115. * Return number of items in basket
  116. * @return int
  117. **/
  118. function count() {
  119. return isset($_SESSION[$this->key])?count($_SESSION[$this->key]):0;
  120. }
  121. /**
  122. * Save current item
  123. * @return array
  124. **/
  125. function save() {
  126. if (!$this->id)
  127. $this->id=uniqid(NULL,TRUE);
  128. $_SESSION[$this->key][$this->id]=$this->item;
  129. return $this->item;
  130. }
  131. /**
  132. * Erase item matching key/value pair
  133. * @return bool
  134. * @param $key string
  135. * @param $val mixed
  136. **/
  137. function erase($key,$val) {
  138. $found=$this->find($key,$val);
  139. if ($found && $id=$found[0]->id) {
  140. unset($_SESSION[$this->key][$id]);
  141. if ($id==$this->id)
  142. $this->reset();
  143. return TRUE;
  144. }
  145. return FALSE;
  146. }
  147. /**
  148. * Reset cursor
  149. * @return NULL
  150. **/
  151. function reset() {
  152. $this->id=NULL;
  153. $this->item=array();
  154. }
  155. /**
  156. * Empty basket
  157. * @return NULL
  158. **/
  159. function drop() {
  160. unset($_SESSION[$this->key]);
  161. }
  162. /**
  163. * Hydrate item using hive array variable
  164. * @return NULL
  165. * @param $key string
  166. **/
  167. function copyfrom($key) {
  168. foreach (\Base::instance()->get($key) as $key=>$val)
  169. $this->item[$key]=$val;
  170. }
  171. /**
  172. * Populate hive array variable with item contents
  173. * @return NULL
  174. * @param $key string
  175. **/
  176. function copyto($key) {
  177. $var=&\Base::instance()->ref($key);
  178. foreach ($this->item as $key=>$field)
  179. $var[$key]=$field;
  180. }
  181. /**
  182. * Check out basket contents
  183. * @return array
  184. **/
  185. function checkout() {
  186. if (isset($_SESSION[$this->key])) {
  187. $out=$_SESSION[$this->key];
  188. unset($_SESSION[$this->key]);
  189. return $out;
  190. }
  191. return array();
  192. }
  193. /**
  194. * Instantiate class
  195. * @return void
  196. * @param $key string
  197. **/
  198. function __construct($key='basket') {
  199. $this->key=$key;
  200. @session_start();
  201. Base::instance()->sync('SESSION');
  202. $this->reset();
  203. }
  204. }