Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "documentFormat" option #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions rdb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion rdb/Datum/ObjectDatum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer an explicit set of allowed values. How about?

  • documentFormat not set at all
  • documentFormat == 'array'
  • documentFormat == 'object'

So the else branch should verify that documentFormat is either not isset, or that its value is 'object'. Otherwise it should throw a RqlDriverError.

Does that sound reasonable to you?

$native = new \ArrayObject();
}
foreach ($this->getValue() as $key => $val) {
$native[$key] = $val->toNative($opts);
}
Expand Down
1 change: 0 additions & 1 deletion rdb/DatumConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public function canEncodeAsJson($v)
}

return false;

}

public function wrapImplicitVar(Query $q)
Expand Down
47 changes: 47 additions & 0 deletions tests/Functional/DocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace r\Tests\Functional;

use r\Tests\TestCase;

class DocumentTest extends TestCase
{
public function setUp()
{
$this->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);
}
}