Skip to content

Commit 3c9064a

Browse files
committed
PHPLIB-242: Refactor FindAndModify to use field path syntax in type map
1 parent e090855 commit 3c9064a

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/Operation/FindAndModify.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,14 @@ public function execute(Server $server)
216216
}
217217

218218
$cursor = $server->executeWriteCommand($this->databaseName, new Command($this->createCommandDocument($server)), $this->createOptions());
219-
$result = current($cursor->toArray());
220-
221-
if ( ! isset($result->value)) {
222-
return null;
223-
}
224-
225-
if ( ! is_object($result->value)) {
226-
throw new UnexpectedValueException('findAndModify command did not return a "value" document');
227-
}
228219

229220
if (isset($this->options['typeMap'])) {
230-
return \MongoDB\apply_type_map_to_document($result->value, $this->options['typeMap']);
221+
$cursor->setTypeMap(\MongoDB\create_field_path_type_map($this->options['typeMap'], 'value'));
231222
}
232223

233-
return $result->value;
224+
$result = current($cursor->toArray());
225+
226+
return isset($result->value) ? $result->value : null;
234227
}
235228

236229
public function getCommandDocument(Server $server)

tests/FunctionsTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ public function provideDocumentAndTypeMap()
5858
'z' => new BSONArray([1, 2, 3]),
5959
]),
6060
],
61+
[
62+
[
63+
'x' => 1,
64+
'random' => [
65+
'foo' => 'bar',
66+
],
67+
'value' => [
68+
'bar' => 'baz',
69+
'embedded' => [
70+
'foo' => 'bar',
71+
],
72+
],
73+
],
74+
[
75+
'root' => 'array',
76+
'document' => 'stdClass',
77+
'array' => 'array',
78+
'fieldPaths' => [
79+
'value' => 'array',
80+
],
81+
],
82+
[
83+
'x' => 1,
84+
'random' => (object) [
85+
'foo' => 'bar',
86+
],
87+
'value' => [
88+
'bar' => 'baz',
89+
'embedded' => (object) [
90+
'foo' => 'bar',
91+
],
92+
],
93+
],
94+
]
6195
];
6296
}
6397

0 commit comments

Comments
 (0)