Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/JSONDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@ class JSONDB {
const DESC = 0;
const AND = "AND";
const OR = "OR";

/** @var bool The current Result Mode */
protected $result_mode;

/** @var string Result Mode Object */
const ResultModeObject = 'object';

/** @var string Result Mode Array */
const ResultModeArray = 'array';

/**
* JSONDB constructor.
* @param $dir
* @param int $json_encode_opt
*/
public function __construct( $dir, $json_encode_opt = JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT ) {
$this->dir = $dir;
$this->json_opts[ 'encode' ] = $json_encode_opt;
Expand Down Expand Up @@ -440,6 +454,12 @@ private function _process_order_by( $content ) {
$content = $sorted;
}

if ($this->result_mode !== self::ResultModeArray) {
if ($this->result_mode === self::ResultModeObject) {
return json_decode(json_encode($content), FALSE);
}
}

return $content;
}

Expand Down Expand Up @@ -471,4 +491,21 @@ public function get() {
$this->flush_indexes( true );
return $content;
}


/**
* Change the mode of returning the result
* @param string $result_mode
* @throws \InvalidArgumentException
*/
public function changeResultMode(string $result_mode)
{
if ($result_mode === self::ResultModeArray) {
$this->result_mode = self::ResultModeArray;
} elseif ($result_mode === self::ResultModeObject) {
$this->result_mode = self::ResultModeObject;
} else {
throw new InvalidArgumentException('The requested result mode is not supported');
}
}
}