basket.php 4.6 KB

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