From df07e4ee27e3437ee3953a9168d11cee896caea7 Mon Sep 17 00:00:00 2001 From: Thom Seddon Date: Sat, 20 Aug 2016 17:24:47 +0100 Subject: [PATCH 1/2] Add "documentFormat" option In a similar way to dateFormat/binaryFormat, this allows the user to specify the type of returned documents via the documentFormat option. Passing 'array' will mean documents are returned as native arrays, otherwise documents will continue to be returned as ArrayObject's. --- rdb/Connection.php | 2 +- rdb/Datum/ObjectDatum.php | 6 +++- tests/Functional/DocumentTest.php | 47 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/Functional/DocumentTest.php diff --git a/rdb/Connection.php b/rdb/Connection.php index dcfdf63..874ff1a 100644 --- a/rdb/Connection.php +++ b/rdb/Connection.php @@ -227,7 +227,7 @@ public function run(Query $query, $options = array(), &$profile = '') // Grab PHP-RQL specific options $toNativeOptions = array(); - foreach (array('binaryFormat', 'timeFormat') as $opt) { + foreach (array('binaryFormat', 'timeFormat', 'documentFormat') as $opt) { if (isset($options) && isset($options[$opt])) { $toNativeOptions[$opt] = $options[$opt]; unset($options[$opt]); diff --git a/rdb/Datum/ObjectDatum.php b/rdb/Datum/ObjectDatum.php index 7e241ad..983948d 100644 --- a/rdb/Datum/ObjectDatum.php +++ b/rdb/Datum/ObjectDatum.php @@ -48,7 +48,11 @@ public function setValue($val) public function toNative($opts) { - $native = new \ArrayObject(); + if (isset($opts['documentFormat']) && $opts['documentFormat'] == 'array') { + $native = array(); + } else { + $native = new \ArrayObject(); + } foreach ($this->getValue() as $key => $val) { $native[$key] = $val->toNative($opts); } diff --git a/tests/Functional/DocumentTest.php b/tests/Functional/DocumentTest.php new file mode 100644 index 0000000..32f966a --- /dev/null +++ b/tests/Functional/DocumentTest.php @@ -0,0 +1,47 @@ +conn = $this->getConnection(); + $this->data = $this->useDataset('Heroes'); + $this->data->populate(); + } + + public function tearDown() + { + $this->data->truncate(); + } + + public function testDocumentDefault() + { + $res = $this->db()->table('marvel') + ->get('Iron Man') + ->run($this->conn); + + $this->assertInstanceOf('ArrayObject', $res); + } + + public function testDocumentArrayObject() + { + $res = $this->db()->table('marvel') + ->get('Iron Man') + ->run($this->conn, array('documentFormat' => 'ArrayObject')); + + $this->assertInstanceOf('ArrayObject', $res); + } + + public function testDocumentNativeArray() + { + $res = $this->db()->table('marvel') + ->get('Iron Man') + ->run($this->conn, array('documentFormat' => 'array')); + + $this->assertInternalType('array', $res); + } +} From ed8368d8cdc0cede5d79405290da2b959fead6ea Mon Sep 17 00:00:00 2001 From: Thom Seddon Date: Sat, 20 Aug 2016 17:36:14 +0100 Subject: [PATCH 2/2] Styling fixes --- rdb/Connection.php | 1 - rdb/DatumConverter.php | 1 - 2 files changed, 2 deletions(-) diff --git a/rdb/Connection.php b/rdb/Connection.php index 874ff1a..be75e1b 100644 --- a/rdb/Connection.php +++ b/rdb/Connection.php @@ -271,7 +271,6 @@ public function run(Query $query, $options = array(), &$profile = '') } else { return $this->createCursorFromResponse($response, $token, $response['n'], $toNativeOptions); } - } public function continueQuery($token) diff --git a/rdb/DatumConverter.php b/rdb/DatumConverter.php index 055e3a9..eb67ada 100644 --- a/rdb/DatumConverter.php +++ b/rdb/DatumConverter.php @@ -158,7 +158,6 @@ public function canEncodeAsJson($v) } return false; - } public function wrapImplicitVar(Query $q)