PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/public/atomic-core/fllat.php

https://bitbucket.org/mdahlke/visual-countdown
PHP | 545 lines | 442 code | 17 blank | 86 comment | 31 complexity | 38ae13829f4d740e74afb258b219608f MD5 | raw file
  1. <?php
  2. /**
  3. * Fllat: A flat file database system. Driven by PHP.
  4. * Stores data in JSON. SQL based data fetching.
  5. *
  6. * PHP version 5.4
  7. *
  8. * @author Alfred Xing <xing@lfred.info>
  9. * @copyright 2013 Alfred Xing
  10. * @license LICENSE.md MIT License
  11. * @version 0.1
  12. *
  13. */
  14. require "vendor/prequel.php";
  15. class Fllat
  16. {
  17. /**
  18. * Create a database
  19. *
  20. * @param string $name name of the database
  21. * @param string $path directory of the database file
  22. */
  23. function __construct($name, $path = "db")
  24. {
  25. $this -> name = $name;
  26. $this -> path = $path;
  27. $this -> go($path . "/" . $this -> name . '.dat');
  28. }
  29. /**
  30. * Initialize database for work
  31. *
  32. * @param string $file relative path of database
  33. *
  34. * @return string existence status of database
  35. */
  36. function go($file)
  37. {
  38. if (file_exists($file)) {
  39. $this -> file = realpath($file);
  40. return "Database '".$this -> name."' already exists.";
  41. } else {
  42. file_put_contents($file, "");
  43. $this -> file = realpath($file);
  44. return "Database '".$this -> name."' successfully created.";
  45. }
  46. }
  47. /**
  48. * Delete database
  49. *
  50. * @return string
  51. */
  52. function del()
  53. {
  54. if (file_exists($this -> file)) {
  55. unlink($this -> file);
  56. return "Database '".$this -> name."' successfully deleted.";
  57. } else {
  58. return "Database '".$this -> name."' doesn't exist.";
  59. }
  60. }
  61. /**
  62. * Rewrite data
  63. *
  64. * @param array $data
  65. */
  66. function rw($data)
  67. {
  68. file_put_contents($this -> file, json_encode($data, JSON_PRETTY_PRINT));
  69. return $data;
  70. }
  71. /**
  72. * Appends data to database
  73. *
  74. * @param array $data
  75. */
  76. function add($data)
  77. {
  78. $_old = file_get_contents($this -> file);
  79. if ($_old) {
  80. $_db = json_decode(file_get_contents($this -> file), true);
  81. } else {
  82. $_db = array();
  83. };
  84. $_db[] = $data;
  85. return $this -> rw($_db);
  86. }
  87. /**
  88. * Remove a row from the database
  89. *
  90. * @param integer $index the index of the row to remove
  91. */
  92. function rm($index) {
  93. $_old = file_get_contents($this -> file);
  94. if ($_old) {
  95. $_db = json_decode(file_get_contents($this -> file), true);
  96. } else {
  97. $_db = array();
  98. };
  99. array_splice($_db, $index, 1);
  100. return $this -> rw($_db);
  101. }
  102. /**
  103. * Updates a row in the database
  104. *
  105. * @param integer $index the index of the row to update
  106. * @param array $data
  107. */
  108. function update($index, $data) {
  109. $_old = file_get_contents($this -> file);
  110. if ($_old) {
  111. $_db = json_decode(file_get_contents($this -> file), true);
  112. } else {
  113. $_db = array();
  114. };
  115. $_db[$index] = array_merge($_db[$index], $data);
  116. return $this -> rw($_db);
  117. }
  118. /**
  119. * Returns the index of a row where key matches value
  120. *
  121. * @param string $key
  122. * @param string $val
  123. *
  124. * @return integer
  125. */
  126. function index($key, $val)
  127. {
  128. $_old = file_get_contents($this -> file);
  129. if ($_old) {
  130. $_db = json_decode($_old, true);
  131. foreach ($_db as $index => $row) {
  132. if ($row[$key] === $val) {
  133. return $index;
  134. break;
  135. }
  136. }
  137. } else {
  138. return ;
  139. }
  140. }
  141. /**
  142. * Change the value of a key
  143. *
  144. * @param string $col
  145. * @param string $to
  146. * @param string $key
  147. * @param string $val
  148. *
  149. * @return array
  150. */
  151. function to($col, $to, $key, $val)
  152. {
  153. $_old = file_get_contents($this -> file);
  154. if ($_old) {
  155. $_result = array();
  156. $_db = json_decode($_old, true);
  157. foreach ($_db as $index => $row) {
  158. if ($row[$key] === $val) {
  159. $_db[$index][$col] = $to;
  160. $_result = 1;
  161. };
  162. }
  163. return $this -> rw($_db);
  164. } else {
  165. return ;
  166. }
  167. }
  168. /**
  169. * Get the row where the value matches that of the key and return the value of the other key
  170. *
  171. * @param string $col
  172. * @param string $key
  173. * @param string $val
  174. *
  175. * @return array
  176. */
  177. function get($col, $key, $val)
  178. {
  179. $_old = file_get_contents($this -> file);
  180. if ($_old) {
  181. $_db = json_decode($_old, true);
  182. foreach ($_db as $index => $row) {
  183. if ($row[$key] === $val && $row[$col]) {
  184. return $row[$col];
  185. break;
  186. }
  187. }
  188. } else {
  189. return ;
  190. }
  191. }
  192. /**
  193. * Get a set of columns for all rows
  194. *
  195. * @param array $cols the list of columns to get, empty for all
  196. *
  197. * @return array
  198. */
  199. function select($cols = array())
  200. {
  201. $_old = file_get_contents($this -> file);
  202. if ($_old) {
  203. $_db = json_decode($_old, true);
  204. $_result = array();
  205. $_values = array();
  206. if ($cols === array()) {
  207. foreach ($_db as $index => $row) {
  208. foreach (array_keys($row) as $c) {
  209. $_values[$c] = $row[$c];
  210. };
  211. if ($_values)
  212. $_result[$index] = $_values;
  213. $_values = array();
  214. }
  215. } else {
  216. foreach ($_db as $index => $row) {
  217. foreach ((array) $cols as $c) {
  218. if ($row[$c])
  219. $_values[$c] = $row[$c];
  220. };
  221. if ($_values)
  222. $_result[$index] = $_values;
  223. $_values = array();
  224. }
  225. }
  226. return $_result;
  227. } else {
  228. return ;
  229. }
  230. }
  231. /**
  232. * Get the row where the value matches that of the key and return the value of the other key
  233. *
  234. * @param array $cols
  235. * @param string $key
  236. * @param string $val
  237. *
  238. * @return array
  239. */
  240. function where($cols, $key, $val)
  241. {
  242. $_old = file_get_contents($this -> file);
  243. if ($_old) {
  244. $_db = json_decode($_old, true);
  245. $_result = array();
  246. $_values = array();
  247. if ($cols === array()) {
  248. foreach ($_db as $index => $row) {
  249. if ($row[$key] === $val) {
  250. foreach (array_keys($row) as $c) {
  251. $_values[$c] = $row[$c];
  252. };
  253. $_result[$index] = $_values;
  254. $_values = array();
  255. }
  256. }
  257. } else {
  258. foreach ($_db as $index => $row) {
  259. if ($row[$key] === $val) {
  260. foreach ((array) $cols as $c) {
  261. $_values[$c] = $row[$c];
  262. };
  263. $_result[$index] = $_values;
  264. $_values = array();
  265. };
  266. }
  267. }
  268. return $_result;
  269. } else {
  270. return ;
  271. }
  272. }
  273. /**
  274. * Get columns from rows in which the key's value is part of the inputted array of values
  275. *
  276. * @param array $cols the columns to return
  277. * @param string $key the column to look for the value
  278. * @param array $val an array of values to be accepted
  279. *
  280. * @return array
  281. */
  282. function in($cols, $key, $val)
  283. {
  284. $_old = file_get_contents($this -> file);
  285. if ($_old) {
  286. $_db = json_decode($_old, true);
  287. $_result = array();
  288. $_values = array();
  289. if ($cols === array()) {
  290. foreach ($_db as $index => $row) {
  291. if (in_array($row[$key], $val)) {
  292. foreach (array_keys($row) as $c) {
  293. $_values[$c] = $row[$c];
  294. };
  295. $_result[$index] = $_values;
  296. $_values = array();
  297. }
  298. }
  299. } else {
  300. foreach ($_db as $index => $row) {
  301. if (in_array($row[$key], $val)) {
  302. foreach ((array) $cols as $c) {
  303. $_values[$c] = $row[$c];
  304. };
  305. $_result[$index] = $_values;
  306. $_values = array();
  307. };
  308. }
  309. }
  310. return $_result;
  311. } else {
  312. return ;
  313. }
  314. }
  315. /**
  316. * Matches keys and values based on a regular expression
  317. *
  318. * @param array $cols the columns to return; an empty array returns all columns
  319. * @param string $key the column whose value to match
  320. * @param string $regex the regular expression to match
  321. *
  322. * @return array
  323. */
  324. function like($cols, $key, $regex)
  325. {
  326. $_old = file_get_contents($this -> file);
  327. if ($_old) {
  328. $_db = json_decode($_old, true);
  329. $_result = array();
  330. $_values = array();
  331. if ($cols === array()) {
  332. foreach ($_db as $index => $row) {
  333. if (preg_match($regex, $row[$key])) {
  334. foreach (array_keys($row) as $c) {
  335. $_values[$c] = $row[$c];
  336. };
  337. $_result[$index] = $_values;
  338. $_values = array();
  339. }
  340. }
  341. } else {
  342. foreach ($_db as $index => $row) {
  343. if (preg_match($regex, $row[$key])) {
  344. foreach ((array) $cols as $c) {
  345. $_values[$c] = $row[$c];
  346. };
  347. $_result[$index] = $_values;
  348. $_values = array();
  349. };
  350. }
  351. }
  352. return $_result;
  353. } else {
  354. return ;
  355. }
  356. }
  357. /**
  358. * Merges two databases and gets rid of duplicates
  359. *
  360. * @param array $cols the columns to merge
  361. * @param Fllat $second the second database to merge
  362. *
  363. * @return array the merged array
  364. */
  365. function union($cols, $second)
  366. {
  367. return array_map(
  368. "unserialize", array_unique(
  369. array_map(
  370. "serialize", array_merge(
  371. $this
  372. -> select($cols),
  373. $second
  374. -> select($cols)
  375. )
  376. )
  377. )
  378. );
  379. }
  380. /**
  381. * Matches and merges columns between databases
  382. *
  383. * @param string $method the method to join (inner, left, right, full)
  384. * @param array $cols the columns to select
  385. * @param Fllat $second the second database to consider
  386. * @param array $match a key-value pair: left column to match => right column
  387. *
  388. * @return array joined array
  389. */
  390. function join($method, $cols, $second, $match)
  391. {
  392. $_left = file_get_contents($this -> file);
  393. $_right = file_get_contents($second -> file);
  394. if ($_left && $_right) {
  395. $_left = json_decode($_left, true);
  396. $_right = json_decode($_right, true);
  397. $_result = array();
  398. $_values = array();
  399. if ($method === "inner") {
  400. foreach ($_left as $lrow) {
  401. foreach ($_right as $rrow) {
  402. if ($lrow[array_keys($match)[0]] === $rrow[array_values($match)[0]]) {
  403. $_result[] = array_merge($lrow, $rrow);
  404. }
  405. }
  406. }
  407. } elseif ($method === "left") {
  408. foreach ($_left as $lrow) {
  409. foreach ($_right as $rrow) {
  410. if ($lrow[array_keys($match)[0]] === $rrow[array_values($match)[0]]) {
  411. $_values = array_merge($lrow, $rrow);
  412. break;
  413. } else {
  414. $_values = $lrow;
  415. }
  416. }
  417. $_result[] = $_values;
  418. $_values = array();
  419. }
  420. } elseif ($method === "right") {
  421. foreach ($_left as $lrow) {
  422. foreach ($_right as $rrow) {
  423. if ($lrow[array_keys($match)[0]] === $rrow[array_values($match)[0]]) {
  424. $_values = array_merge($lrow, $rrow);
  425. break;
  426. } else {
  427. $_values = $rrow;
  428. }
  429. }
  430. $_result[] = $_values;
  431. $_values = array();
  432. }
  433. } elseif ($method === "full") {
  434. $_result = array_map(
  435. "unserialize", array_unique(
  436. array_map(
  437. "serialize", array_merge(
  438. $this
  439. -> join("left", $cols, $second, $match),
  440. $this
  441. -> join("right", $cols, $second, $match)
  442. )
  443. )
  444. )
  445. );
  446. }
  447. return $GLOBALS["prequel"] -> select($cols, $_result);
  448. } else {
  449. return ;
  450. }
  451. }
  452. /**
  453. * Checks whether the given key/value pair exists
  454. *
  455. * @param string $key the key
  456. * @param string $val the value
  457. *
  458. * @return boolean whether the pair exists
  459. */
  460. function exists($key, $val)
  461. {
  462. $_old = file_get_contents($this -> file);
  463. if ($_old) {
  464. $_db = json_decode($_old, true);
  465. $_result = false;
  466. foreach ($_db as $index => $row) {
  467. if ($row[$key] === $val) {
  468. $_result = true;
  469. }
  470. }
  471. return $_result;
  472. } else {
  473. return false;
  474. }
  475. }
  476. /**
  477. * Counts the number of items per column or for all columns
  478. *
  479. * @param string $col the column name to count. No input counts all columns.
  480. *
  481. * @return int the number of rows containing that column.
  482. */
  483. function count($col = "")
  484. {
  485. if ($col === "") {
  486. $query = array();
  487. } else {
  488. $query = (array) $col;
  489. }
  490. return count($this -> select($query));
  491. }
  492. /**
  493. * Gets the first item of a column
  494. *
  495. * @param string $col the column to look at
  496. *
  497. * @return mixed the first item in the column
  498. */
  499. function first($col)
  500. {
  501. return $this -> select((array) $col)[0][$col];
  502. }
  503. /**
  504. * Gets the last item in a column
  505. *
  506. * @param string $col the name of the column to look at
  507. *
  508. * @return mixed the last item in the column
  509. */
  510. function last($col)
  511. {
  512. $_values = $this -> select((array) $col);
  513. return end($_values)[$col];
  514. }
  515. }
  516. ?>