private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) { $attributes = $xmlIndex->attributes(); $keys = array(); foreach ($xmlIndex->{'key'} as $key) { $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; } $options = array(); if (isset($attributes['background'])) { $options['background'] = 'true' === (string) $attributes['background']; } if (isset($attributes['drop-dups'])) { $options['dropDups'] = 'true' === (string) $attributes['drop-dups']; } if (isset($attributes['name'])) { $options['name'] = (string) $attributes['name']; } if (isset($attributes['safe'])) { $options['safe'] = 'true' === (string) $attributes['safe']; } if (isset($attributes['sparse'])) { $options['sparse'] = 'true' === (string) $attributes['sparse']; } if (isset($attributes['unique'])) { $options['unique'] = 'true' === (string) $attributes['unique']; } if (isset($xmlIndex->{'option'})) { foreach ($xmlIndex->{'option'} as $option) { $value = (string) $option['value']; if ($value === 'true') { $value = true; } elseif ($value === 'false') { $value = false; } elseif (is_numeric($value)) { $value = preg_match('/^[-]?\\d+$/', $value) ? (int) $value : (double) $value; } $options[(string) $option['name']] = $value; } } $class->addIndex($keys, $options); }
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 Riak 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); }
private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) { $keys = array_merge($keys, $index->keys); $options = array(); $allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); foreach ($allowed as $name) { if (isset($index->{$name})) { $options[$name] = $index->{$name}; } } $options = array_merge($options, $index->options); $class->addIndex($keys, $options); }