PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Barstool/Adaptor.php

http://github.com/funkatron/barstool
PHP | 220 lines | 85 code | 39 blank | 96 comment | 7 complexity | 76fae4d1506f889964937ef353a5e419 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. */
  5. class Barstool_Adaptor
  6. {
  7. /**
  8. * if this is true, objects in JSON are returned as associative
  9. * arrays. Otherwise they're returned as objects (default)
  10. *
  11. * @var string
  12. */
  13. protected $return_assoc_arrays = false;
  14. /**
  15. * The name of the store, typically the database name
  16. *
  17. * @var string
  18. */
  19. protected $name = 'Lawnchair';
  20. /**
  21. * the table name if we're using an RBDMS store
  22. *
  23. * @var string
  24. */
  25. protected $table = 'field';
  26. function __construct($options) {
  27. $this->init($options);
  28. }
  29. protected function init($options) {
  30. if ($options && isset($options['return_assoc_arrays']) && $options['return_assoc_arrays']) {
  31. $this->return_assoc_arrays = true;
  32. }
  33. }
  34. /**
  35. * whether or not we should return JSON objects as associative arrays
  36. *
  37. * @param boolean $state if true, objects stored will be returned as associative arrays
  38. */
  39. public function setReturnAssocArray($state) {
  40. $this->return_assoc_arrays = (bool)$state;
  41. }
  42. public function save($obj, $callback=null) {
  43. }
  44. public function get($key, $callback=null) {
  45. }
  46. public function exists($key, $callback=nul) {
  47. }
  48. public function all($callback=null) {
  49. }
  50. public function remove($keyOrObj, $callback=null) {
  51. }
  52. public function nuke($callback=null) {
  53. }
  54. public function find($condition, $callback=null) {
  55. $cb = array($this, 'findIterator');
  56. $this->all($cb, $condition, $callback);
  57. }
  58. /**
  59. * an iteration function that calls the callback on each object that meets the conditions.
  60. * Intended to be used from Barstool_Adaptor::find
  61. *
  62. * @param array $objs
  63. * @param callback $condition
  64. * @param callback $callback
  65. * @return void
  66. * @author Ed Finkler
  67. */
  68. protected function findIterator($objs, $condition, $callback) {
  69. $matches = array();
  70. foreach ($objs as $obj) {
  71. array_push($matches, $obj);
  72. if (call_user_func($condition, $obj)) {
  73. call_user_func($callback, $obj);
  74. }
  75. }
  76. }
  77. /**
  78. * Applies a callback to all records in the store
  79. *
  80. * @param callback $callback
  81. * @return void
  82. * @author Ed Finkler
  83. */
  84. public function each($callback) {
  85. $cb = array($this, 'eachIterator');
  86. $this->all($cb, $callback);
  87. }
  88. /**
  89. * a simple iteration function that calls the callback on each object.
  90. * Intended to be used from Barstool_Adaptor::each
  91. *
  92. * @param array $objs
  93. * @param callback $callback
  94. * @return void
  95. * @author Ed Finkler
  96. */
  97. protected function eachIterator($objs, $callback) {
  98. foreach ($objs as $obj) {
  99. call_user_func($callback, $obj);
  100. }
  101. }
  102. /**
  103. * encodes the given value (probably an object) as JSON
  104. *
  105. * @param mixed $obj
  106. * @return string
  107. * @author Ed Finkler
  108. */
  109. protected function serialize($obj) {
  110. return json_encode($obj);
  111. }
  112. /**
  113. * decodes the given JSON string into its PHP value.
  114. *
  115. * @see Barstool_Adaptor::return_assoc_arrays
  116. * @param string $str
  117. * @return mixed
  118. * @author Ed Finkler
  119. */
  120. protected function deserialize($str) {
  121. return json_decode($str, $this->return_assoc_arrays);
  122. }
  123. /**
  124. * from comments for http://php.net/manual/en/function.uniqid.php
  125. *
  126. * @return string
  127. * @author Ed Finkler
  128. */
  129. protected function uuid() {
  130. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  131. // 32 bits for "time_low"
  132. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  133. // 16 bits for "time_mid"
  134. mt_rand(0, 0xffff),
  135. // 16 bits for "time_hi_and_version",
  136. // four most significant bits holds version number 4
  137. mt_rand(0, 0x0fff) | 0x4000,
  138. // 16 bits, 8 bits for "clk_seq_hi_res",
  139. // 8 bits for "clk_seq_low",
  140. // two most significant bits holds zero and one for variant DCE1.1
  141. mt_rand(0, 0x3fff) | 0x8000,
  142. // 48 bits for "node"
  143. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  144. );
  145. }
  146. /**
  147. * returns the current timestamp in milliseconds
  148. *
  149. * @return integer
  150. * @author Ed Finkler
  151. */
  152. protected function now() {
  153. return round(microtime(true)*1000);
  154. }
  155. /**
  156. * reports if an array is associative
  157. *
  158. * @param array $array
  159. * @return boolean
  160. */
  161. protected function isAssoc($array) {
  162. if ( !is_array($array) || empty($array) ) {
  163. return -1;
  164. }
  165. foreach (array_keys($array) as $k => $v) {
  166. if ($k !== $v) {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. /**
  173. * converts an associative array to an object by encoding and decoding json
  174. * @param array $assoc an associative array
  175. * @return stdClass
  176. */
  177. protected function assocToObject($assoc) {
  178. return json_decode(json_encode($assoc));
  179. }
  180. }
  181. ?>