private function addFieldMapping(ClassMetadataInfo $class, $mapping)
{
if (isset($mapping['name'])) {
$name = $mapping['name'];
} elseif (isset($mapping['fieldName'])) {
$name = $mapping['fieldName'];
} else {
throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
}
$class->mapField($mapping);
// Index this field if either "index", "unique", or "sparse" are set
if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
return;
}
$keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc');
$options = array();
if (isset($mapping['background'])) {
$options['background'] = (bool) $mapping['background'];
}
if (isset($mapping['drop-dups'])) {
$options['dropDups'] = (bool) $mapping['drop-dups'];
}
if (isset($mapping['index-name'])) {
$options['name'] = (string) $mapping['index-name'];
}
if (isset($mapping['safe'])) {
$options['safe'] = (bool) $mapping['safe'];
}
if (isset($mapping['sparse'])) {
$options['sparse'] = (bool) $mapping['sparse'];
}
if (isset($mapping['unique'])) {
$options['unique'] = (bool) $mapping['unique'];
}
$class->addIndex($keys, $options);
}