Skip to content

Commit 75762f4

Browse files
authored
Merge pull request #3 from na2axl/adding_class_map
Add FETCH_CLASS mode for class mapping
2 parents 13afb8c + 04ae16e commit 75762f4

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

src/JSONDB/JSONDB.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class JSONDB
8484
*/
8585
const FETCH_OBJECT = 5;
8686

87+
/**
88+
* Define if we fetch results with class mapping
89+
* @const int
90+
*/
91+
const FETCH_CLASS = 6;
92+
8793
/**
8894
* The current JSONDB instance
8995
* @var JSONDB

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

tests/JSONDBTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class JSONDBTest extends PHPUnit_Framework_TestCase
66
{
7-
87
/**
98
* @var \JSONDB\JSONDB
109
*/
@@ -181,4 +180,54 @@ public function testExceptionIsRaisedForDuplicatePKUKOnReplace() {
181180
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
182181
self::$database->query('__phpunit_test_table_pk.replace(2)');
183182
}
183+
184+
/**
185+
* @expectedException \JSONDB\Exception
186+
*/
187+
public function testExceptionIsRaisedForFetchClassMode() {
188+
$r = new \JSONDB\QueryResult(array(array('testVar1' => 'foo', 'testVar2' => 'bar')), self::$database);
189+
$r->setFetchMode(\JSONDB\JSONDB::FETCH_CLASS, 'FakeClass');
190+
$r->fetch();
191+
}
192+
193+
/**
194+
* @expectedException \JSONDB\Exception
195+
*/
196+
public function testExceptionIsRaisedForFetchClassMode2() {
197+
$r = new \JSONDB\QueryResult(array(array('testVar1' => 'foo', 'testVar3' => 'bar')), self::$database);
198+
$r->setFetchMode(\JSONDB\JSONDB::FETCH_CLASS, 'TestClass');
199+
$r->fetch();
200+
}
201+
202+
/**
203+
* @expectedException \JSONDB\Exception
204+
*/
205+
public function testExceptionIsRaisedForFetchClassMode3() {
206+
$r = new \JSONDB\QueryResult(array(array('testVar1' => 'foo', 'testVar4' => 'bar')), self::$database);
207+
$r->setFetchMode(\JSONDB\JSONDB::FETCH_CLASS, 'TestClass');
208+
$r->fetch();
209+
}
210+
211+
public function testFetchClassMode() {
212+
$r = new \JSONDB\QueryResult(array(array('testVar1' => 'foo', 'testVar2' => 'bar')), self::$database);
213+
$r->setFetchMode(\JSONDB\JSONDB::FETCH_CLASS, 'TestClass');
214+
$value = $r->current();
215+
$this->assertInstanceOf('TestClass', $value);
216+
}
217+
218+
public function testFetchClassMode2() {
219+
$r = new \JSONDB\QueryResult(array(array('testVar1' => 'foo', 'testVar2' => 'bar')), self::$database);
220+
$r->setFetchMode(\JSONDB\JSONDB::FETCH_CLASS, 'TestClass');
221+
$value = $r->current();
222+
$expected = new TestClass();
223+
$expected->testVar1 = 'foo';
224+
$expected->testVar2 = 'bar';
225+
$this->assertEquals($expected, $value);
226+
}
227+
}
228+
229+
class TestClass {
230+
public $testVar1;
231+
public $testVar2;
232+
private $testVar3;
184233
}

0 commit comments

Comments
 (0)