/**
* Set shard key for this Document.
*
* @param array $keys Array of document keys.
* @param array $options Array of sharding options.
*
* @throws MappingException
*/
public function setShardKey(array $keys, array $options = array())
{
if ($this->inheritanceType === self::INHERITANCE_TYPE_SINGLE_COLLECTION && !is_null($this->shardKey)) {
throw MappingException::shardKeyInSingleCollInheritanceSubclass($this->getName());
}
if ($this->isEmbeddedDocument) {
throw MappingException::embeddedDocumentCantHaveShardKey($this->getName());
}
foreach (array_keys($keys) as $field) {
if (!isset($this->fieldMappings[$field])) {
continue;
}
if (in_array($this->fieldMappings[$field]['type'], ['many', 'collection'])) {
throw MappingException::noMultiKeyShardKeys($this->getName(), $field);
}
if ($this->fieldMappings[$field]['strategy'] !== static::STORAGE_STRATEGY_SET) {
throw MappingException::onlySetStrategyAllowedInShardKey($this->getName(), $field);
}
}
$this->shardKey = array('keys' => array_map(function ($value) {
if ($value == 1 || $value == -1) {
return (int) $value;
}
if (is_string($value)) {
$lower = strtolower($value);
if ($lower === 'asc') {
return 1;
} elseif ($lower === 'desc') {
return -1;
}
}
return $value;
}, $keys), 'options' => $options);
}