basket.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $obj=clone($this);
  80. $obj->id=$id;
  81. $obj->item=$item;
  82. $out[]=$obj;
  83. }
  84. }
  85. return $out;
  86. }
  87. /**
  88. * Return first item that matches key/value pair
  89. * @return object|FALSE
  90. * @param $key string
  91. * @param $val mixed
  92. **/
  93. function findone($key,$val) {
  94. return ($data=$this->find($key,$val))?$data[0]:FALSE;
  95. }
  96. /**
  97. * Map current item to matching key/value pair
  98. * @return array
  99. * @param $key string
  100. * @param $val mixed
  101. **/
  102. function load($key,$val) {
  103. if ($found=$this->find($key,$val)) {
  104. $this->id=$found[0]->id;
  105. return $this->item=$found[0]->item;
  106. }
  107. $this->reset();
  108. return [];
  109. }
  110. /**
  111. * Return TRUE if current item is empty/undefined
  112. * @return bool
  113. **/
  114. function dry() {
  115. return !$this->item;
  116. }
  117. /**
  118. * Return number of items in basket
  119. * @return int
  120. **/
  121. function count() {
  122. return isset($_SESSION[$this->key])?count($_SESSION[$this->key]):0;
  123. }
  124. /**
  125. * Save current item
  126. * @return array
  127. **/
  128. function save() {
  129. if (!$this->id)
  130. $this->id=uniqid(NULL,TRUE);
  131. $_SESSION[$this->key][$this->id]=$this->item;
  132. return $this->item;
  133. }
  134. /**
  135. * Erase item matching key/value pair
  136. * @return bool
  137. * @param $key string
  138. * @param $val mixed
  139. **/
  140. function erase($key,$val) {
  141. $found=$this->find($key,$val);
  142. if ($found && $id=$found[0]->id) {
  143. unset($_SESSION[$this->key][$id]);
  144. if ($id==$this->id)
  145. $this->reset();
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. /**
  151. * Reset cursor
  152. * @return NULL
  153. **/
  154. function reset() {
  155. $this->id=NULL;
  156. $this->item=[];
  157. }
  158. /**
  159. * Empty basket
  160. * @return NULL
  161. **/
  162. function drop() {
  163. unset($_SESSION[$this->key]);
  164. }
  165. /**
  166. * Hydrate item using hive array variable
  167. * @return NULL
  168. * @param $var array|string
  169. **/
  170. function copyfrom($var) {
  171. if (is_string($var))
  172. $var=\Base::instance()->$var;
  173. foreach ($var as $key=>$val)
  174. $this->set($key,$val);
  175. }
  176. /**
  177. * Populate hive array variable with item contents
  178. * @return NULL
  179. * @param $key string
  180. **/
  181. function copyto($key) {
  182. $var=&\Base::instance()->ref($key);
  183. foreach ($this->item as $key=>$field)
  184. $var[$key]=$field;
  185. }
  186. /**
  187. * Check out basket contents
  188. * @return array
  189. **/
  190. function checkout() {
  191. if (isset($_SESSION[$this->key])) {
  192. $out=$_SESSION[$this->key];
  193. unset($_SESSION[$this->key]);
  194. return $out;
  195. }
  196. return [];
  197. }
  198. /**
  199. * Instantiate class
  200. * @return void
  201. * @param $key string
  202. **/
  203. function __construct($key='basket') {
  204. $this->key=$key;
  205. if (session_status()!=PHP_SESSION_ACTIVE)
  206. session_start();
  207. Base::instance()->sync('SESSION');
  208. $this->reset();
  209. }
  210. }