Automatic pass to convert with php-cs-fixer
This commit is contained in:
256
src/Dbjson.php
256
src/Dbjson.php
@@ -1,43 +1,56 @@
|
||||
<?php
|
||||
|
||||
/** DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
* @license BSD
|
||||
*/
|
||||
/**
|
||||
* DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
* @license BSD
|
||||
*/
|
||||
|
||||
namespace Domframework;
|
||||
|
||||
/** DBJSON : a NoSQL DB in JSON
|
||||
* Documentation
|
||||
* - A filter is an array containing the fields and the values to found
|
||||
* array ("key"=>"val") <== Look for the key equal val
|
||||
* array ("key=>array ("val", "<=")) <== Look for the key lighter or equal
|
||||
* than val
|
||||
* array () <== Look for all the documents (no
|
||||
* filter)
|
||||
* array ("key"=>"val", "key2"=>"val2") <== Look for two parameters
|
||||
* - A document is an array containing the fields and the values to store
|
||||
* array ("key"=>"val)
|
||||
*
|
||||
* - The field named _id is the document key
|
||||
*/
|
||||
/**
|
||||
* DBJSON : a NoSQL DB in JSON
|
||||
* Documentation
|
||||
* - A filter is an array containing the fields and the values to found
|
||||
* array ("key"=>"val") <== Look for the key equal val
|
||||
* array ("key=>array ("val", "<=")) <== Look for the key lighter or equal
|
||||
* than val
|
||||
* array () <== Look for all the documents (no
|
||||
* filter)
|
||||
* array ("key"=>"val", "key2"=>"val2") <== Look for two parameters
|
||||
* - A document is an array containing the fields and the values to store
|
||||
* array ("key"=>"val)
|
||||
*
|
||||
* - The field named _id is the document key
|
||||
*/
|
||||
class Dbjson
|
||||
{
|
||||
/** The DSN of the connection */
|
||||
/**
|
||||
* The DSN of the connection
|
||||
*/
|
||||
private $dsn = "";
|
||||
/** The database file */
|
||||
/**
|
||||
* The database file
|
||||
*/
|
||||
private $dbfile = "";
|
||||
/** The lock file */
|
||||
/**
|
||||
* The lock file
|
||||
*/
|
||||
private $dbfileLock = "";
|
||||
/** The last Insert Id */
|
||||
/**
|
||||
* The last Insert Id
|
||||
*/
|
||||
private $lastInsertId = 0;
|
||||
/** The database content */
|
||||
/**
|
||||
* The database content
|
||||
*/
|
||||
private $db;
|
||||
|
||||
/** The constructor
|
||||
* @param string $dsn The DSN of the connection
|
||||
*/
|
||||
/**
|
||||
* The constructor
|
||||
* @param string $dsn The DSN of the connection
|
||||
*/
|
||||
public function __construct($dsn)
|
||||
{
|
||||
if (! function_exists("openssl_random_pseudo_bytes")) {
|
||||
@@ -99,18 +112,19 @@ class Dbjson
|
||||
$this->dbfile = $this->dbfile;
|
||||
}
|
||||
|
||||
/** Store one document in database
|
||||
* @param string $collection The collection name
|
||||
* @param array $document The document to insert
|
||||
* @return integer return The number of document inserted in the database
|
||||
*/
|
||||
/**
|
||||
* Store one document in database
|
||||
* @param string $collection The collection name
|
||||
* @param array $document The document to insert
|
||||
* @return integer return The number of document inserted in the database
|
||||
*/
|
||||
public function insertOne($collection, $document)
|
||||
{
|
||||
$uniqueKey = $this->uniqueKey();
|
||||
$this->lockEX();
|
||||
$this->db = $this->readDB();
|
||||
$this->db[$collection]["content"][$uniqueKey] = array_merge(
|
||||
array("_id" => $uniqueKey),
|
||||
["_id" => $uniqueKey],
|
||||
$document
|
||||
);
|
||||
$this->writeDB();
|
||||
@@ -118,11 +132,12 @@ class Dbjson
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** Store multiple documents in database
|
||||
* @param string $collection The collection name
|
||||
* @param array $documents array(array ())
|
||||
* @return integer The number of documents inserted in the database
|
||||
*/
|
||||
/**
|
||||
* Store multiple documents in database
|
||||
* @param string $collection The collection name
|
||||
* @param array $documents array(array ())
|
||||
* @return integer The number of documents inserted in the database
|
||||
*/
|
||||
public function insertMany($collection, $documents)
|
||||
{
|
||||
foreach ($documents as $document) {
|
||||
@@ -135,7 +150,7 @@ class Dbjson
|
||||
foreach ($documents as $document) {
|
||||
$uniqueKey = $this->uniqueKey();
|
||||
$this->db[$collection]["content"][$uniqueKey] = array_merge(
|
||||
array("_id" => $uniqueKey),
|
||||
["_id" => $uniqueKey],
|
||||
$document
|
||||
);
|
||||
}
|
||||
@@ -145,20 +160,21 @@ class Dbjson
|
||||
return count($documents);
|
||||
}
|
||||
|
||||
/** Look at the documents matching $filter (all by default).
|
||||
* Then return only the $fields (all by default).
|
||||
* The field _id is always returned
|
||||
* Return $limit maximum documents (no limit by default)
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @param array|string $fields The fields to display (* for all, empty array
|
||||
* for none)
|
||||
* @param integer $limit The number of documents to display
|
||||
* @return array The documents matching the parameters
|
||||
*/
|
||||
/**
|
||||
* Look at the documents matching $filter (all by default).
|
||||
* Then return only the $fields (all by default).
|
||||
* The field _id is always returned
|
||||
* Return $limit maximum documents (no limit by default)
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @param array|string $fields The fields to display (* for all, empty array
|
||||
* for none)
|
||||
* @param integer $limit The number of documents to display
|
||||
* @return array The documents matching the parameters
|
||||
*/
|
||||
public function find(
|
||||
$collection,
|
||||
$filter = array(),
|
||||
$filter = [],
|
||||
$fields = "*",
|
||||
$limit = null
|
||||
) {
|
||||
@@ -166,14 +182,14 @@ class Dbjson
|
||||
$this->db = $this->readDB();
|
||||
// Get the keys of the documents based on the filter
|
||||
$keys = $this->filter($collection, $filter);
|
||||
$res = array();
|
||||
$res = [];
|
||||
foreach ($keys as $key) {
|
||||
// Limit the fields
|
||||
$tmp = array();
|
||||
$tmp = [];
|
||||
if ($fields === "*") {
|
||||
$tmp = $this->db[$collection]["content"][$key];
|
||||
} elseif (is_array($fields)) {
|
||||
if (! in_array("_id", $fields)) {
|
||||
if (! in_array("_id", $fields, true)) {
|
||||
array_unshift($fields, "_id");
|
||||
}
|
||||
foreach ($fields as $field) {
|
||||
@@ -200,20 +216,21 @@ class Dbjson
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Update some existing documents. Do not change the _id keys
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @param array $document The data to update
|
||||
* @return integer The number of modified documents
|
||||
* To unset a field, add in the document array a "_unset"=>array("field)"
|
||||
*/
|
||||
/**
|
||||
* Update some existing documents. Do not change the _id keys
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @param array $document The data to update
|
||||
* @return integer The number of modified documents
|
||||
* To unset a field, add in the document array a "_unset"=>array("field)"
|
||||
*/
|
||||
public function update($collection, $filter, $document)
|
||||
{
|
||||
$this->lockEX();
|
||||
$this->db = $this->readDB();
|
||||
// Get the keys of the documents based on the filter
|
||||
$keys = $this->filter($collection, $filter);
|
||||
$unset = array();
|
||||
$unset = [];
|
||||
if (array_key_exists("_unset", $document)) {
|
||||
$unset = $document["_unset"];
|
||||
unset($document["_unset"]);
|
||||
@@ -253,12 +270,13 @@ class Dbjson
|
||||
return count($keys);
|
||||
}
|
||||
|
||||
/** Replace some existing documents. Do not change the _id keys
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @param array $document The data to update
|
||||
* @return integer The number of modified documents
|
||||
*/
|
||||
/**
|
||||
* Replace some existing documents. Do not change the _id keys
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @param array $document The data to update
|
||||
* @return integer The number of modified documents
|
||||
*/
|
||||
public function replace($collection, $filter, $document)
|
||||
{
|
||||
$this->lockEX();
|
||||
@@ -267,7 +285,7 @@ class Dbjson
|
||||
$keys = $this->filter($collection, $filter);
|
||||
foreach ($keys as $key) {
|
||||
$tmp = $this->db[$collection]["content"][$key];
|
||||
$replace = array();
|
||||
$replace = [];
|
||||
$replace["_id"] = $tmp["_id"];
|
||||
$replace = array_merge($replace, $document);
|
||||
$this->db[$collection]["content"][$key] = $replace;
|
||||
@@ -278,11 +296,12 @@ class Dbjson
|
||||
return count($keys);
|
||||
}
|
||||
|
||||
/** Delete the first document matching the filter
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to found the documents
|
||||
* @return integer The number of deleted documents
|
||||
*/
|
||||
/**
|
||||
* Delete the first document matching the filter
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to found the documents
|
||||
* @return integer The number of deleted documents
|
||||
*/
|
||||
public function deleteOne($collection, $filter)
|
||||
{
|
||||
$this->lockEX();
|
||||
@@ -301,11 +320,12 @@ class Dbjson
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** Delete all the documents matching the filter
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @return integer The number of deleted documents
|
||||
*/
|
||||
/**
|
||||
* Delete all the documents matching the filter
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* @return integer The number of deleted documents
|
||||
*/
|
||||
public function deleteMany($collection, $filter)
|
||||
{
|
||||
$this->lockEX();
|
||||
@@ -321,33 +341,34 @@ class Dbjson
|
||||
return count($keys);
|
||||
}
|
||||
|
||||
/** Look for the keys corresponding to the filter in the collection
|
||||
* Don't manage the locks !
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* - A filter is an array containing the fields and the values to found
|
||||
* array () <== Look for all the documents (no
|
||||
* filter)
|
||||
* array ("key"=>"val") <== Look for the key equal val
|
||||
* array ("key=>array ("val", "<=")) <== Look for the key lighter or
|
||||
* equal than val
|
||||
* array ("key"=>"val", "key2"=>"val2") <== Look for two parameters
|
||||
* array ("key"=>array ("val", "=="),
|
||||
* "key2"=>array ("val2", "==")) <== Look for two complex parameters
|
||||
* Here is the comparison types available : ==,
|
||||
* @return array the keys matching the filter
|
||||
*/
|
||||
/**
|
||||
* Look for the keys corresponding to the filter in the collection
|
||||
* Don't manage the locks !
|
||||
* @param string $collection The collection name
|
||||
* @param array $filter The filter to apply to found the documents
|
||||
* - A filter is an array containing the fields and the values to found
|
||||
* array () <== Look for all the documents (no
|
||||
* filter)
|
||||
* array ("key"=>"val") <== Look for the key equal val
|
||||
* array ("key=>array ("val", "<=")) <== Look for the key lighter or
|
||||
* equal than val
|
||||
* array ("key"=>"val", "key2"=>"val2") <== Look for two parameters
|
||||
* array ("key"=>array ("val", "=="),
|
||||
* "key2"=>array ("val2", "==")) <== Look for two complex parameters
|
||||
* Here is the comparison types available : ==,
|
||||
* @return array the keys matching the filter
|
||||
*/
|
||||
public function filter($collection, $filter)
|
||||
{
|
||||
if ($this->db === null) {
|
||||
$this->db = $this->readDB();
|
||||
}
|
||||
$keys = array();
|
||||
$keys = [];
|
||||
if (! array_key_exists($collection, $this->db)) {
|
||||
$this->db[$collection]["content"] = array();
|
||||
$this->db[$collection]["content"] = [];
|
||||
}
|
||||
foreach ($this->db[$collection]["content"] as $key => $document) {
|
||||
if ($filter === array()) {
|
||||
if ($filter === []) {
|
||||
$keys[] = $key;
|
||||
continue;
|
||||
}
|
||||
@@ -390,7 +411,7 @@ class Dbjson
|
||||
$matchFilter = true;
|
||||
} elseif (
|
||||
strtolower($fvals[1]) === "in_array" &&
|
||||
in_array($fvals[0], $document[$fkey])
|
||||
in_array($fvals[0], $document[$fkey], true)
|
||||
) {
|
||||
$matchFilter = true;
|
||||
} else {
|
||||
@@ -418,9 +439,10 @@ class Dbjson
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/** Generate a unique key
|
||||
* @return string the Unique key generated
|
||||
*/
|
||||
/**
|
||||
* Generate a unique key
|
||||
* @return string the Unique key generated
|
||||
*/
|
||||
private function uniqueKey()
|
||||
{
|
||||
$data = openssl_random_pseudo_bytes(16);
|
||||
@@ -429,7 +451,9 @@ class Dbjson
|
||||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||
}
|
||||
|
||||
/** Exclusive lock the database file */
|
||||
/**
|
||||
* Exclusive lock the database file
|
||||
*/
|
||||
private function lockEX()
|
||||
{
|
||||
$this->dbfileLock = fopen($this->dbfile, "rt");
|
||||
@@ -438,7 +462,9 @@ class Dbjson
|
||||
}
|
||||
}
|
||||
|
||||
/** Shared lock the database file */
|
||||
/**
|
||||
* Shared lock the database file
|
||||
*/
|
||||
private function lockSH()
|
||||
{
|
||||
$this->dbfileLock = fopen($this->dbfile, "rt");
|
||||
@@ -447,7 +473,9 @@ class Dbjson
|
||||
}
|
||||
}
|
||||
|
||||
/** Unlock the database file */
|
||||
/**
|
||||
* Unlock the database file
|
||||
*/
|
||||
private function lockUN()
|
||||
{
|
||||
if ($this->dbfileLock !== null) {
|
||||
@@ -457,22 +485,24 @@ class Dbjson
|
||||
}
|
||||
}
|
||||
|
||||
/** Read the dbfile and return an array containing the data. This function
|
||||
* don't do locks !
|
||||
* @return array The database content from the dbfile
|
||||
*/
|
||||
/**
|
||||
* Read the dbfile and return an array containing the data. This function
|
||||
* don't do locks !
|
||||
* @return array The database content from the dbfile
|
||||
*/
|
||||
private function readDB()
|
||||
{
|
||||
$res = json_decode(file_get_contents($this->dbfile), true);
|
||||
if ($res === null) {
|
||||
$res = array();
|
||||
$res = [];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Write the dbfile with the provided data. This function don't do locks !
|
||||
* @return bool True if the recording is OK, false if there is a problem
|
||||
*/
|
||||
/**
|
||||
* Write the dbfile with the provided data. This function don't do locks !
|
||||
* @return bool True if the recording is OK, false if there is a problem
|
||||
*/
|
||||
private function writeDB()
|
||||
{
|
||||
return !! file_put_contents($this->dbfile, json_encode($this->db));
|
||||
|
||||
Reference in New Issue
Block a user