session_db.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * @class SessionDB
  4. * Fake Database. Stores records in $_SESSION
  5. */
  6. class SessionDB {
  7. public function __construct() {
  8. if (!isset($_SESSION['pk'])) {
  9. $this->reset();
  10. }
  11. }
  12. // fake a database pk
  13. public function pk() {
  14. return $_SESSION['pk']++;
  15. }
  16. // fake a resultset
  17. public function rs() {
  18. return $_SESSION['rs'];
  19. }
  20. public function insert($rec) {
  21. array_push($_SESSION['rs'], $rec);
  22. }
  23. public function update($idx, $attributes) {
  24. $_SESSION['rs'][$idx] = $attributes;
  25. }
  26. public function destroy($idx) {
  27. return array_shift(array_splice($_SESSION['rs'], $idx, 1));
  28. }
  29. public function reset() {
  30. $_SESSION['pk'] = 10; // <-- start fake pks at 10
  31. $_SESSION['rs'] = getData(); // <-- populate $_SESSION with data.
  32. }
  33. }
  34. // Sample data.
  35. function getData() {
  36. return array(
  37. array('id' => 1, 'first' => "Fred", 'last' => 'Flintstone', 'email' => 'fred@flintstone.com'),
  38. array('id' => 2, 'first' => "Wilma", 'last' => 'Flintstone', 'email' => 'wilma@flintstone.com'),
  39. array('id' => 3, 'first' => "Pebbles", 'last' => 'Flintstone', 'email' => 'pebbles@flintstone.com'),
  40. array('id' => 4, 'first' => "Barney", 'last' => 'Rubble', 'email' => 'barney@rubble.com'),
  41. array('id' => 5, 'first' => "Betty", 'last' => 'Rubble', 'email' => 'betty@rubble.com'),
  42. array('id' => 6, 'first' => "BamBam", 'last' => 'Rubble', 'email' => 'bambam@rubble.com')
  43. );
  44. }