Skip to content

Commit

Permalink
Merge pull request #4 from RobertGrubb/updates
Browse files Browse the repository at this point in the history
[Fixes] Fix delete and get by id when does not exist
  • Loading branch information
RobertGrubb authored Jun 11, 2020
2 parents 0bad821 + c7005ea commit 92e9c7c
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 37 deletions.
8 changes: 8 additions & 0 deletions examples/todo/database/todo/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"title": "An example",
"id": "5ed559fc3669f",
"createdAt": 1591040508,
"updatedAt": 1591040508
}
]
28 changes: 28 additions & 0 deletions examples/todo/filerdb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../src/FilerDB.php';

try {
// Instantiate Database
$filerdb = new FilerDB\Instance([

// Required
'root' => __DIR__ . '/database',

// Optional configurations
'includeTimestamps' => true,

// Specify database
'database' => 'todo',

// Configs
'createRootIfNotExist' => true,
'createDatabaseIfNotExist' => true,
'createCollectionIfNotExist' => true
]);

return $filerdb;
} catch (Exception $e) {
return false;
}
50 changes: 50 additions & 0 deletions examples/todo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// Include a file that returns the filerdb instance.
$filerdb = require __DIR__ . '/filerdb.php';

if (!$filerdb) die('FilerDB was unable to instantiate.');

// Current action
$action = (isset($_GET['a']) ? $_GET['a'] : false);

if ($action === 'submit') {
$todo = (isset($_POST['todo']) ? $_POST['todo'] : false);

if ($todo !== false) {
$filerdb->collection('items')->insert([
'title' => $todo
]);
}

header('Location: index.php');
exit;

} elseif ($action === 'remove') {
$id = (isset($_GET['id']) ? $_GET['id'] : false);

if ($id !== false) {
$filerdb->collection('items')->id($id)->delete();
}

header('Location: index.php');
exit;
}

// Retrieve all todo items
$items = $filerdb->collection('items')->all();

?>

<h1>Todo Items</h1>

<form action="index.php?a=submit" method="POST">
<input type="text" name="todo" />
<button type="submit">Add</button>
</form>

<ul>
<?php foreach ($items as $item): ?>
<li><?= $item->title; ?>&nbsp;&nbsp;<a href="index.php?a=remove&id=<?= $item->id; ?>">[X]</a></li>
<?php endforeach; ?>
</ul>
4 changes: 2 additions & 2 deletions example/backup.php → examples/usage/backup.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

require '../vendor/autoload.php';
require_once __DIR__ . '/../src/FilerDB.php';
require_once __DIR__ , '/../../vendor/autoload.php';
require_once __DIR__ . '/../../src/FilerDB.php';

try {
// Instantiate Database
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions example/dev.php → examples/usage/dev.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

require '../vendor/autoload.php';
require_once __DIR__ . '/../src/FilerDB.php';
require_once __DIR__ , '/../../vendor/autoload.php';
require_once __DIR__ . '/../../src/FilerDB.php';

try {
// Instantiate Database
Expand Down
4 changes: 2 additions & 2 deletions example/test.php → examples/usage/test.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

require '../vendor/autoload.php';
require_once __DIR__ . '/../src/FilerDB.php';
require_once __DIR__ , '/../../vendor/autoload.php';
require_once __DIR__ . '/../../src/FilerDB.php';

use FilerDB\Instance;

Expand Down
2 changes: 1 addition & 1 deletion src/FilerDB/Core/Helpers/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function byId ($documents, $id = null) {
}

// If no index was set, return false.
if (!$index) return false;
if ($index === false) return false;

// Return object with data.
return (object) [
Expand Down
49 changes: 28 additions & 21 deletions src/FilerDB/Core/Libraries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ public function __construct ($config = null, $database, $collection) {
public function id ($id) {
$documents = $this->documents;
$data = Document::byId($documents, $id);
if ($data === false) return false;

if ($data === false) {
$this->documents = false;
$this->response = false;
return $this;
}

$this->documents = $documents[$data->index];
$this->response = $this->documents;
return $this;
Expand All @@ -113,13 +119,17 @@ public function id ($id) {
*/
public function get ($fields = false) {

/**
* If the columns parameter is provided,
* and is an array.
*/
if (is_array($fields)) {
if (count($fields) >= 1) {
return $this->pickFieldsFromData($this->response, $fields);
// Make sure there is a document(s) to iterate through.
if ($this->response !== false) {

/**
* If the columns parameter is provided,
* and is an array.
*/
if (is_array($fields)) {
if (count($fields) >= 1) {
return $this->pickFieldsFromData($this->response, $fields);
}
}
}

Expand Down Expand Up @@ -489,24 +499,21 @@ public function delete () {
$documentsToDelete = $this->documents;
$originalDocuments = $this->getDocuments();

/**
* Fail safe, we do not want to delete all records
* with this method.
*
* Warn them to use ->empty() instead.
*/
if (count($documentsToDelete) === count($originalDocuments)) {
throw new FilerDBException("Please use ->empty() to delete all records");
return false;
}

/**
* Filter out records that match the documents
* to be deleted.
*/
foreach ($documentsToDelete as $deleteDoc) {
if (is_array($documentsToDelete)) {
foreach ($documentsToDelete as $deleteDoc) {
foreach ($originalDocuments as $key => $origDoc) {
if ($origDoc->id === $deleteDoc->id) {
unset($originalDocuments[$key]);
}
}
}
} else {
foreach ($originalDocuments as $key => $origDoc) {
if ($origDoc->id === $deleteDoc->id) {
if ($origDoc->id === $documentsToDelete->id) {
unset($originalDocuments[$key]);
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/FilerDB/Core/Libraries/Databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,4 @@ private function retrieveDatabases() {
$this->databases = $result;
}

/**
* Builds the path for the database in the file system.
* @return string $path
*/
private function path ($database) {
$path = $this->config->root . DIRECTORY_SEPARATOR . $database . DIRECTORY_SEPARATOR;
return $path;
}

}

0 comments on commit 92e9c7c

Please sign in to comment.