protected function ensureMongoId($rawId)
{
if (is_array($rawId)) {
$result = [];
foreach ($rawId as $key => $value) {
$result[$key] = $this->ensureMongoId($value);
}
return $result;
} elseif (is_object($rawId)) {
if ($rawId instanceof ObjectID) {
return $rawId;
} else {
$rawId = (string) $rawId;
}
}
try {
$mongoId = new ObjectID($rawId);
} catch (InvalidArgumentException $e) {
// invalid id format
$mongoId = $rawId;
}
return $mongoId;
}