Skip to content

Commit 82de6ec

Browse files
committed
Ensure operations return documents as objects by default
Unless the operation returns a scalar or custom value (e.g. Count, CreateIndexes), we should explicitly convert documents to stdClass instances to return. This will ultimately be made configurable once PHPLIB-112 is implemented. A manual cast (of arrays to objects) was also necessary until PHPC-318 is implemented.
1 parent 771316b commit 82de6ec

File tree

8 files changed

+52
-31
lines changed

8 files changed

+52
-31
lines changed

src/Operation/Aggregate.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,21 @@ public function execute(Server $server)
122122
return $cursor;
123123
}
124124

125+
$cursor->setTypeMap(array('document' => 'stdClass'));
125126
$result = current($cursor->toArray());
126127

127-
if (empty($result['ok'])) {
128-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
128+
// TODO: Remove this once PHPC-318 is implemented
129+
is_array($result) and $result = (object) $result;
130+
131+
if (empty($result->ok)) {
132+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
129133
}
130134

131-
if ( ! isset($result['result']) || ! is_array($result['result'])) {
135+
if ( ! isset($result->result) || ! is_array($result->result)) {
132136
throw new UnexpectedValueException('aggregate command did not return a "result" array');
133137
}
134138

135-
return new ArrayIterator(array_map(
136-
function (stdClass $document) { return (array) $document; },
137-
$result['result']
138-
));
139+
return new ArrayIterator($result->result);
139140
}
140141

141142
/**

src/Operation/CreateCollection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ public function __construct($databaseName, $collectionName, array $options = arr
106106
public function execute(Server $server)
107107
{
108108
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
109-
$cursor->setTypeMap(array('document' => 'array'));
109+
$cursor->setTypeMap(array('document' => 'stdClass'));
110110
$result = current($cursor->toArray());
111111

112-
if (empty($result['ok'])) {
113-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
112+
// TODO: Remove this once PHPC-318 is implemented
113+
is_array($result) and $result = (object) $result;
114+
115+
if (empty($result->ok)) {
116+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
114117
}
115118

116119
return $result;

src/Operation/CreateIndexes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private function executeCommand(Server $server)
9191
));
9292

9393
$cursor = $server->executeCommand($this->databaseName, $command);
94+
$cursor->setTypeMap(array('document' => 'array'));
9495
$result = current($cursor->toArray());
9596

9697
if (empty($result['ok'])) {

src/Operation/Distinct.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ public function __construct($databaseName, $collectionName, $fieldName, array $f
6262
public function execute(Server $server)
6363
{
6464
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
65-
$cursor->setTypeMap(array('document' => 'array'));
65+
$cursor->setTypeMap(array('document' => 'stdClass'));
6666
$result = current($cursor->toArray());
6767

68-
if (empty($result['ok'])) {
69-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
68+
// TODO: Remove this once PHPC-318 is implemented
69+
is_array($result) and $result = (object) $result;
70+
71+
if (empty($result->ok)) {
72+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
7073
}
7174

72-
if ( ! isset($result['values']) || ! is_array($result['values'])) {
75+
if ( ! isset($result->values) || ! is_array($result->values)) {
7376
throw new UnexpectedValueException('distinct command did not return a "values" array');
7477
}
7578

76-
return $result['values'];
79+
return $result->values;
7780
}
7881

7982
/**

src/Operation/DropCollection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ public function __construct($databaseName, $collectionName)
4141
public function execute(Server $server)
4242
{
4343
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
44-
$cursor->setTypeMap(array('document' => 'array'));
44+
$cursor->setTypeMap(array('document' => 'stdClass'));
4545
$result = current($cursor->toArray());
4646

47-
if (empty($result['ok'])) {
48-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
47+
// TODO: Remove this once PHPC-318 is implemented
48+
is_array($result) and $result = (object) $result;
49+
50+
if (empty($result->ok)) {
51+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
4952
}
5053

5154
return $result;

src/Operation/DropDatabase.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ public function __construct($databaseName)
3939
public function execute(Server $server)
4040
{
4141
$cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1)));
42-
$cursor->setTypeMap(array('document' => 'array'));
42+
$cursor->setTypeMap(array('document' => 'stdClass'));
4343
$result = current($cursor->toArray());
4444

45-
if (empty($result['ok'])) {
46-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
45+
// TODO: Remove this once PHPC-318 is implemented
46+
is_array($result) and $result = (object) $result;
47+
48+
if (empty($result->ok)) {
49+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
4750
}
4851

4952
return $result;

src/Operation/DropIndexes.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ public function execute(Server $server)
5656
);
5757

5858
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
59+
$cursor->setTypeMap(array('document' => 'stdClass'));
5960
$result = current($cursor->toArray());
6061

61-
if (empty($result['ok'])) {
62-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
62+
// TODO: Remove this once PHPC-318 is implemented
63+
is_array($result) and $result = (object) $result;
64+
65+
if (empty($result->ok)) {
66+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
6367
}
6468

6569
return $result;

src/Operation/FindAndModify.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,17 @@ public function __construct($databaseName, $collectionName, array $options)
118118
public function execute(Server $server)
119119
{
120120
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
121-
$cursor->setTypeMap(array('document' => 'array'));
121+
$cursor->setTypeMap(array('document' => 'stdClass'));
122122
$result = current($cursor->toArray());
123123

124-
if (empty($result['ok'])) {
125-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
124+
// TODO: Remove this once PHPC-318 is implemented
125+
is_array($result) and $result = (object) $result;
126+
127+
if (empty($result->ok)) {
128+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
126129
}
127130

128-
if ( ! isset($result['value'])) {
131+
if ( ! isset($result->value)) {
129132
return null;
130133
}
131134

@@ -134,17 +137,17 @@ public function execute(Server $server)
134137
* requested.
135138
*/
136139
if ($this->options['upsert'] && ! $this->options['new'] &&
137-
isset($result['lastErrorObject']->updatedExisting) &&
138-
! $result['lastErrorObject']->updatedExisting) {
140+
isset($result->lastErrorObject->updatedExisting) &&
141+
! $result->lastErrorObject->updatedExisting) {
139142

140143
return null;
141144
}
142145

143-
if ( ! is_array($result['value'])) {
146+
if ( ! is_object($result->value)) {
144147
throw new UnexpectedValueException('findAndModify command did not return a "value" document');
145148
}
146149

147-
return (object) $result['value'];
150+
return $result->value;
148151
}
149152

150153
/**

0 commit comments

Comments
 (0)