PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/jsondb.php

https://gitlab.com/mlnkv/simple-rest-api
PHP | 228 lines | 172 code | 56 blank | 0 comment | 31 complexity | 186445b393969f5f79bc2ff551e47b46 MD5 | raw file
  1. <?php
  2. class Jsondb {
  3. private $name = null;
  4. private $path = null;
  5. private $settings = null;
  6. private $items = null;
  7. private $where = null;
  8. public function __construct($name) {
  9. $this->name = $name;
  10. $this->path = STORAGE . $this->name . '.json';
  11. }
  12. public function saveEntry($entry) {
  13. $this->_readFile();
  14. $entry['_uid'] = uniqid();
  15. $this->items[] = $entry;
  16. if ($this->_writeFile()) {
  17. return $entry;
  18. }
  19. return null;
  20. }
  21. public function find($where = null) {
  22. $this->where = $where;
  23. return $this;
  24. }
  25. public function update($data) {
  26. $this->_readFile();
  27. if ($this->where) {
  28. if (is_array($this->where)) {
  29. foreach ($this->where as $where_key => $where_val) {
  30. foreach ($this->items as $item_key => $item_value) {
  31. if (isset($this->items[$item_key][$where_key]) &&
  32. $this->items[$item_key][$where_key] == $where_val) {
  33. foreach ($data as $data_key => $data_value) {
  34. $this->items[$item_key][$data_key] = $data_value;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. if (is_callable($this->where)) {
  41. foreach (array_filter($this->items, $this->where) as $item_key => $item_value) {
  42. foreach ($data as $data_key => $data_value) {
  43. $this->items[$item_key][$data_key] = $data_value;
  44. }
  45. }
  46. }
  47. if (is_string($this->where)) {
  48. foreach ($this->items as $item_key => $item_value) {
  49. if ($this->items[$item_key]['_uid'] == $this->where) {
  50. foreach ($data as $data_key => $data_value) {
  51. $this->items[$item_key][$data_key] = $data_value;
  52. }
  53. }
  54. }
  55. }
  56. } else {
  57. foreach ($this->items as $item_key => $item_value) {
  58. foreach ($data as $data_key => $data_value) {
  59. $this->items[$item_key][$data_key] = $data_value;
  60. }
  61. }
  62. }
  63. $this->_writeFile();
  64. return $this;
  65. }
  66. public function delete() {
  67. $this->_readFile();
  68. if ($this->where) {
  69. if (is_array($this->where)) {
  70. foreach ($this->where as $where_key => $where_val) {
  71. foreach ($this->items as $item_key => $item_value) {
  72. if (isset($this->items[$item_key][$where_key]) &&
  73. $this->items[$item_key][$where_key] == $where_val) {
  74. unset($this->items[$item_key]);
  75. }
  76. }
  77. }
  78. }
  79. if (is_callable($this->where)) {
  80. foreach (array_filter($this->items, $this->where) as $item_key => $item_value) {
  81. unset($this->items[$item_key]);
  82. }
  83. }
  84. if (is_string($this->where)) {
  85. foreach ($this->items as $item_key => $item_value) {
  86. if ($this->items[$item_key]['_uid'] == $this->where) {
  87. unset($this->items[$item_key]);
  88. }
  89. }
  90. }
  91. $this->items = array_values($this->items);
  92. } else {
  93. $this->items = [];
  94. }
  95. $this->_writeFile();
  96. return $this;
  97. }
  98. public function toArray() {
  99. $this->_readFile();
  100. if ($this->where) {
  101. if (is_array($this->where)) {
  102. foreach ($this->where as $key => $val) {
  103. $this->items = array_filter($this->items, function($item) use ($key, $val) {
  104. return isset($item[$key]) && $item[$key] == $val;
  105. });
  106. }
  107. }
  108. if (is_callable($this->where)) {
  109. $this->items = array_filter($this->items, $this->where);
  110. }
  111. if (is_string($this->where)) {
  112. $this->items = array_filter($this->items, function($item) {
  113. return $item['_uid'] == $this->where;
  114. });
  115. }
  116. }
  117. return array_values($this->items);
  118. }
  119. public function settings($key = null, $value = null) {
  120. $this->_readFile();
  121. if ($key === null) {
  122. return $this->settings;
  123. }
  124. if ($value === null) {
  125. return isset($this->settings[$key]) ? $this->settings[$key] : null;
  126. }
  127. $this->settings[$key] = $value;
  128. $this->_writeFile();
  129. return true;
  130. }
  131. private function _readFile() {
  132. $json = file_get_contents($this->path);
  133. $data = json_decode($json, true);
  134. $this->settings = $data['settings'];
  135. $this->items = $data['items'];
  136. }
  137. private function _writeFile() {
  138. $this->settings['updated'] = date('Y-m-d H:i:s');
  139. $data = [
  140. 'settings' => $this->settings,
  141. 'items' => $this->items
  142. ];
  143. $json = json_encode($data);
  144. $fp = fopen($this->path, 'r+');
  145. if (flock($fp, LOCK_EX)) {
  146. ftruncate($fp, 0);
  147. fwrite($fp, $json);
  148. fflush($fp);
  149. flock($fp, LOCK_UN);
  150. } else {
  151. return false;
  152. }
  153. fclose($fp);
  154. return true;
  155. }
  156. static function exists($name) {
  157. return file_exists(STORAGE . $name . '.json');
  158. }
  159. static function create($name) {
  160. if (static::exists($name)) return false;
  161. $data = [
  162. 'settings' => [
  163. 'created' => date('Y-m-d H:i:s'),
  164. 'updated' => date('Y-m-d H:i:s')
  165. ],
  166. 'items' => []
  167. ];
  168. $json = json_encode($data);
  169. file_put_contents(STORAGE . $name . '.json', $json);
  170. }
  171. }
  172. ?>