public static function prepareToStore($value)
{
// if array - try to prepare every value
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = self::prepareToStore($v);
}
return $value;
}
// if scalar - return it
if (!is_object($value)) {
return $value;
}
// if internal mongo types - pass it as is
if (in_array(get_class($value), array('MongoId', 'MongoCode', 'MongoDate', 'MongoRegex', 'MongoBinData', 'MongoInt32', 'MongoInt64', 'MongoDBRef', 'MongoMinKey', 'MongoMaxKey', 'MongoTimestamp'))) {
return $value;
}
// do not convert geo-json to array
if ($value instanceof \GeoJson\Geometry\Geometry) {
return $value->jsonSerialize();
}
// structure
if ($value instanceof Structure) {
// validate structure
if (!$value->isValid()) {
$exception = new InvalidDocumentException('Embedded document not valid');
$exception->setDocument($value);
throw $exception;
}
// get array from structure
return $value->toArray();
}
// other objects convert to array
return (array) $value;
}