Skip to content

Commit bcd3fe6

Browse files
authored
Merge pull request #32 from UseMuffin/query-jsonserialize
Make Query instance JSON serializable.
2 parents 1dd1abf + 5108b7f commit bcd3fe6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Query.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Cake\Datasource\QueryTrait;
99
use Cake\Utility\Hash;
1010
use IteratorAggregate;
11+
use JsonSerializable;
1112
use Muffin\Webservice\Model\Endpoint;
1213
use Muffin\Webservice\Webservice\WebserviceInterface;
1314

14-
class Query implements QueryInterface, IteratorAggregate
15+
class Query implements IteratorAggregate, JsonSerializable, QueryInterface
1516
{
1617

1718
use QueryTrait;
@@ -520,4 +521,16 @@ public function __debugInfo()
520521
'webservice' => $this->webservice()
521522
];
522523
}
524+
525+
/**
526+
* Executes the query and converts the result set into JSON.
527+
*
528+
* Part of JsonSerializable interface.
529+
*
530+
* @return \Cake\Datasource\ResultSetInterface The data to convert to JSON.
531+
*/
532+
public function jsonSerialize()
533+
{
534+
return $this->all();
535+
}
523536
}

tests/TestCase/QueryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ public function testDebugInfo()
212212
], $this->query->__debugInfo());
213213
}
214214

215+
public function testJsonSerialize()
216+
{
217+
$expected = [
218+
['id' => 1, 'title' => 'Hello World'],
219+
['id' => 2, 'title' => 'New ORM'],
220+
['id' => 3, 'title' => 'Webservices']
221+
];
222+
223+
$this->assertEquals(json_encode($expected), json_encode($this->query));
224+
}
225+
215226
/**
216227
* @inheritDoc
217228
*/

0 commit comments

Comments
 (0)