Skip to content

Commit

Permalink
Merge pull request #2 from RobertGrubb/updates
Browse files Browse the repository at this point in the history
Structural Updates
  • Loading branch information
RobertGrubb authored May 30, 2020
2 parents cfa4081 + c7716ad commit 0bad821
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 130 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Install via composer:
use FilerDB\Instance;
// Instantiate Database
$filerdb = new Instance([ 'path' => __DIR__ . '/database/' ]);
$filerdb = new Instance([ 'root' => __DIR__ . '/database/' ]);
```

### Configuration
Expand All @@ -25,16 +25,28 @@ $filerdb = new Instance([ 'path' => __DIR__ . '/database/' ]);
[
/**
* This is the main path for FilerDB.
* This is the root path for FilerDB.
*/
'path' => __DIR__ . '/database',
'root' => false,
/**
* If the database path does not exist, try
* If the root path does not exist, try
* and create it.
*/
'createRootIfNotExist' => false,
/**
* If the database does not exist, try
* and create it.
*/
'createDatabaseIfNotExist' => false,
/**
* If the collection does not exist, attempt
* to create it.
*/
'createCollectionIfNotExist' => false,
/**
* If the insert and update logic handles
* the createdAt and updatedAt timestamps
Expand Down
12 changes: 10 additions & 2 deletions example/database/test/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"id": "5ed1b2957c6d9"
},
{
"username": "etari3",
"username": "test234",
"email": "[email protected]",
"location": {
"country": "US",
Expand All @@ -25,5 +25,13 @@
"state": "KY"
},
"id": "5ed1b2957c6d93"
},
{
"id": "5ed1b2957c6d92",
"username": "test234"
},
{
"id": "5ed1b2957c6d92",
"username": "test234"
}
]
]
33 changes: 13 additions & 20 deletions example/dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,24 @@
$filerdb = new FilerDB\Instance([

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

// Optional configurations
'includeTimestamps' => false,
'createDatabaseIfNotExist' => true

// Specify database
'database' => 'woot',

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

$filerdb->collection('foo')->insert([
'testing' => true
]);

/**
* Example of limiting to 1 response, and
* offsetting it.
*
* limit(1, 1)
*
* @param Limiter
* @param Offset
*
* Offset is the array key of the response.
*/
$data = $filerdb
->database('test')
->collection('users')
->id('5ed1b2957c6d92')
->get(['username']);

print_r($data);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
58 changes: 58 additions & 0 deletions src/FilerDB/Core/Helpers/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace FilerDB\Core\Helpers;

use FilerDB\Core\Exceptions\FilerDBException;

class Document {

/**
* Check documents to see if the id exists.
* @param array
* @param string
* @return boolean
*/
public static function exists ($documents, $id) {
if (self::byId($documents, $id) === false) return false;
return true;
}

/**
* Iterates through an array of documents, and finds
* the object that has a specific id. It will then
* return an object with the index, and the data
* of that document.
* @param array $documents
* @param string $id
* @return mixed
*/
public static function byId ($documents, $id = null) {
if (is_null($id)) return false;

// Set the index to false by default
$index = false;

// Set the doc to false by default.
$doc = false;

// Iterate through documents, continue if no match.
foreach ($documents as $i => $document) {

if ($document->id === $id) {
$index = $i;
$doc = $document;
} else {
continue;
}
}

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

// Return object with data.
return (object) [
'index' => $index,
'document' => $doc
];
}
}
78 changes: 25 additions & 53 deletions src/FilerDB/Core/Libraries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use FilerDB\Core\Utilities\Timestamp;
use FilerDB\Core\Utilities\Dot;

// Libraries
use FilerDB\Core\Libraries\Document;
// Helpers
use FilerDB\Core\Helpers\Document;

class Collection {

Expand Down Expand Up @@ -47,6 +47,12 @@ class Collection {
*/
private $response = [];

/**
* Holds the path for the collection in the filesystem.
* @var string
*/
private $collectionPath = null;

/**
* Class constructor
*/
Expand All @@ -65,12 +71,20 @@ public function __construct ($config = null, $database, $collection) {
// Set the current database
$this->database = $database;

// Retrieve the current database.
// Retrieve the current collection.
$this->collection = $collection;

// Build the collection path
$this->collectionPath = FileSystem::collectionPath(
$this->config->root,
$this->database,
$this->collection
);

// Holder for documents that should be returned
$this->documents = $this->getDocuments();

// Holder for response that is returned
$this->response = $this->documents;
}

Expand All @@ -85,7 +99,7 @@ public function __construct ($config = null, $database, $collection) {
*/
public function id ($id) {
$documents = $this->documents;
$data = $this->documentById($id, $documents);
$data = Document::byId($documents, $id);
if ($data === false) return false;
$this->documents = $documents[$data->index];
$this->response = $this->documents;
Expand Down Expand Up @@ -344,7 +358,7 @@ public function insert ($data) {
$id = (isset($insertData->id) ? $insertData->id : uniqid());

// If the id already set?
if ($this->documentById($id, $documents) !== false)
if (Document::exists($documents, $id))
throw new FilerDBException("Document with id:$id already exists");

$insertData->id = $id;
Expand All @@ -365,7 +379,7 @@ public function insert ($data) {
$json = json_encode($documents, JSON_PRETTY_PRINT);

// Attempt to write file
$inserted = FileSystem::writeFile($this->path(), $json);
$inserted = FileSystem::writeFile($this->collectionPath, $json);

// If not inserted, throw an error.
if (!$inserted)
Expand All @@ -388,7 +402,7 @@ public function update ($data) {
// we can assume it's a single document that is being
// updated.
if (!is_array($documentsToUpdate)) {
$docInfo = $this->documentById($this->documents->id, $originalDocuments);
$docInfo = Document::byId($originalDocuments, $this->documents->id);
$key = $docInfo->index;

// Update all of the keys
Expand Down Expand Up @@ -435,7 +449,7 @@ public function update ($data) {
$json = json_encode($originalDocuments, JSON_PRETTY_PRINT);

// Attempt to write file
$updated = FileSystem::writeFile($this->path(), $json);
$updated = FileSystem::writeFile($this->collectionPath, $json);

// If not deleted, throw an error.
if (!$updated)
Expand All @@ -459,7 +473,7 @@ public function empty () {
$json = json_encode($documents, JSON_PRETTY_PRINT);

// Attempt to write file
$emptied = FileSystem::writeFile($this->path(), $json);
$emptied = FileSystem::writeFile($this->collectionPath, $json);

// If not deleted, throw an error.
if (!$emptied)
Expand Down Expand Up @@ -505,7 +519,7 @@ public function delete () {
$json = json_encode($originalDocuments, JSON_PRETTY_PRINT);

// Attempt to write file
$deleted = FileSystem::writeFile($this->path(), $json);
$deleted = FileSystem::writeFile($this->collectionPath, $json);

// If not deleted, throw an error.
if (!$deleted)
Expand Down Expand Up @@ -608,42 +622,13 @@ private function pickFieldsFromData ($data, $fields) {
return $data;
}

/**
* Find a document by it's id in an array
* of documents.
*
* Returns the index and the document data in
* object format.
*/
private function documentById ($id, $documents) {
$index = false;
$doc = false;

foreach ($documents as $i => $document) {

if ($document->id === $id) {
$index = $i;
$doc = $document;
} else {
continue;
}
}

if (!$index) return false;

return (object) [
'index' => $index,
'document' => $doc
];
}

/**
* Get all documents in object format.
* If the file can not be decoded, throw
* an error because the data is malformed.
*/
private function getDocuments () {
$contents = file_get_contents ($this->path());
$contents = file_get_contents ($this->collectionPath);

try {
$contents = json_decode($contents);
Expand All @@ -653,17 +638,4 @@ private function getDocuments () {

return $contents;
}

/**
* Returns a path for the current collection.
*/
private function path () {
$path = $this->config->DATABASE_PATH .
DIRECTORY_SEPARATOR .
$this->database .
DIRECTORY_SEPARATOR .
$this->collection . '.json';

return $path;
}
}
Loading

0 comments on commit 0bad821

Please sign in to comment.