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);
if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
return;
}
// Multiple index specifications in one field mapping is ambiguous
if ((isset($mapping['index']) && is_array($mapping['index'])) + (isset($mapping['unique']) && is_array($mapping['unique'])) + (isset($mapping['sparse']) && is_array($mapping['sparse'])) > 1) {
throw new \InvalidArgumentException('Multiple index specifications found among index, unique, and/or sparse fields');
}
// Index this field if either "index", "unique", or "sparse" are set
$keys = array($name => 'asc');
/* The "order" option is only used in the index specification and should
* not be passed along as an index option.
*/
if (isset($mapping['index']['order'])) {
$keys[$name] = $mapping['index']['order'];
unset($mapping['index']['order']);
} elseif (isset($mapping['unique']['order'])) {
$keys[$name] = $mapping['unique']['order'];
unset($mapping['unique']['order']);
} elseif (isset($mapping['sparse']['order'])) {
$keys[$name] = $mapping['sparse']['order'];
unset($mapping['sparse']['order']);
}
/* Initialize $options from any array value among index, unique, and
* sparse. Any boolean values for unique or sparse should be merged into
* the options afterwards to ensure consistent parsing.
*/
$options = array();
$unique = null;
$sparse = null;
if (isset($mapping['index']) && is_array($mapping['index'])) {
$options = $mapping['index'];
}
if (isset($mapping['unique'])) {
if (is_array($mapping['unique'])) {
$options = $mapping['unique'] + array('unique' => true);
} else {
$unique = (bool) $mapping['unique'];
}
}
if (isset($mapping['sparse'])) {
if (is_array($mapping['sparse'])) {
$options = $mapping['sparse'] + array('sparse' => true);
} else {
$sparse = (bool) $mapping['sparse'];
}
}
if (isset($unique)) {
$options['unique'] = $unique;
}
if (isset($sparse)) {
$options['sparse'] = $sparse;
}
$class->addIndex($keys, $options);
}