public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server));
$result = current($cursor->toArray());
if (!isset($result->value)) {
return null;
}
/* Prior to 3.0, findAndModify returns an empty document instead of null
* when an upsert is performed and the pre-modified document was
* requested.
*/
if ($this->options['upsert'] && !$this->options['new'] && isset($result->lastErrorObject->updatedExisting) && !$result->lastErrorObject->updatedExisting) {
return null;
}
if (!is_object($result->value)) {
throw new UnexpectedValueException('findAndModify command did not return a "value" document');
}
return $result->value;
}