Skip to content

Commit 6b56a5d

Browse files
committed
Add a FETCH_CLASS mode
Results can now be fetched as Object by mapping an user's defined class.
1 parent 85af7ba commit 6b56a5d

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/JSONDB/QueryResult.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class QueryResult implements \Iterator, \SeekableIterator, \Countable, \Serializ
6464
*/
6565
private $fetchMode;
6666

67+
/**
68+
* Class name used for FETCH_CLASS method
69+
* @var string
70+
*/
71+
private $className;
72+
6773
/**
6874
* JSONDB instance
6975
* @var JSONDB
@@ -110,7 +116,7 @@ public function queryString()
110116

111117
/**
112118
* Returns the current result
113-
* @return array|QueryResultObject
119+
* @return array|QueryResultObject|object
114120
* @throws Exception
115121
*/
116122
public function current()
@@ -124,6 +130,20 @@ public function current()
124130
case JSONDB::FETCH_OBJECT:
125131
return new QueryResultObject($return);
126132

133+
case JSONDB::FETCH_CLASS:
134+
if (!class_exists($this->className)) {
135+
throw new Exception("JSONDB Query Result Error: Can't fetch for data. Trying to use JSONDB::FETCH_CLASS mode with class \"{$this->className}\" but the class doesn't exist or not found.");
136+
}
137+
$mapper = new $this->className();
138+
$availableVars = get_class_vars($this->className);
139+
foreach ((array)$return as $item => $value) {
140+
if (!array_key_exists($item, $availableVars)) {
141+
throw new Exception("JSONDB Query Result Error: Can't fetch for data. Using JSONDB::FETCH_CLASS mode with class \"{$this->className}\" but the property \"{$item}\" doesn't exist or not public.");
142+
}
143+
$mapper->$item = $value;
144+
}
145+
return $mapper;
146+
127147
default:
128148
throw new Exception('JSONDB Query Result Error: Fetch mode not supported.');
129149
}
@@ -260,14 +280,15 @@ public function offsetUnset($offset)
260280

261281
/**
262282
* Fetch for results
263-
* @param int $mode The fetch mode
283+
* @param int $mode The fetch mode
284+
* @param string $className The class name (for JSONDB::FETCH_CLASS)
264285
* @return array|QueryResultObject|bool
265286
* @throws Exception
266287
*/
267-
public function fetch($mode = NULL)
288+
public function fetch($mode = NULL, $className = NULL)
268289
{
269290
if (NULL !== $mode) {
270-
$this->setFetchMode($mode);
291+
$this->setFetchMode($mode, $className);
271292
}
272293

273294
if ($this->database->queryIsExecuted()) {
@@ -284,12 +305,14 @@ public function fetch($mode = NULL)
284305

285306
/**
286307
* Changes the fetch mode
287-
* @param int $mode
308+
* @param int $mode
309+
* @param string $className
288310
* @return QueryResult
289311
*/
290-
public function setFetchMode($mode = JSONDB::FETCH_ARRAY)
312+
public function setFetchMode($mode = JSONDB::FETCH_ARRAY, $className = NULL)
291313
{
292314
$this->fetchMode = $mode;
315+
$this->className = $className;
293316
return $this;
294317
}
295318

0 commit comments

Comments
 (0)