/** * Map a field. * * @param array $mapping The mapping information. * @return void */ public function mapField(array $mapping) { $mapping = parent::mapField($mapping); if ($this->reflClass->hasProperty($mapping['fieldName'])) { $reflProp = $this->reflClass->getProperty($mapping['fieldName']); $reflProp->setAccessible(true); $this->reflFields[$mapping['fieldName']] = $reflProp; } }
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, \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 completeIdGeneratorMapping(ClassMetadataInfo $class) { $idGenOptions = $class->generatorOptions; switch ($class->generatorType) { case ClassMetadata::GENERATOR_TYPE_AUTO: $class->setIdGenerator(new \CosmoW\ODM\Riak\Id\AutoGenerator($class)); break; case ClassMetadata::GENERATOR_TYPE_INCREMENT: $incrementGenerator = new \CosmoW\ODM\Riak\Id\IncrementGenerator($class); if (isset($idGenOptions['key'])) { $incrementGenerator->setKey($idGenOptions['key']); } if (isset($idGenOptions['collection'])) { $incrementGenerator->setCollection($idGenOptions['collection']); } $class->setIdGenerator($incrementGenerator); break; case ClassMetadata::GENERATOR_TYPE_UUID: $uuidGenerator = new \CosmoW\ODM\Riak\Id\UuidGenerator($class); isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); $class->setIdGenerator($uuidGenerator); break; case ClassMetadata::GENERATOR_TYPE_ALNUM: $alnumGenerator = new \CosmoW\ODM\Riak\Id\AlnumGenerator($class); if (isset($idGenOptions['pad'])) { $alnumGenerator->setPad($idGenOptions['pad']); } if (isset($idGenOptions['chars'])) { $alnumGenerator->setChars($idGenOptions['chars']); } elseif (isset($idGenOptions['awkwardSafe'])) { $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); } $class->setIdGenerator($alnumGenerator); break; case ClassMetadata::GENERATOR_TYPE_CUSTOM: if (empty($idGenOptions['class'])) { throw MappingException::missingIdGeneratorClass($class->name); } $customGenerator = new $idGenOptions['class'](); unset($idGenOptions['class']); if (!$customGenerator instanceof \CosmoW\ODM\Riak\Id\AbstractIdGenerator) { throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); } $methods = get_class_methods($customGenerator); foreach ($idGenOptions as $name => $value) { $method = 'set' . ucfirst($name); if (!in_array($method, $methods)) { throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); } $customGenerator->{$method}($value); } $class->setIdGenerator($customGenerator); break; case ClassMetadata::GENERATOR_TYPE_NONE: break; default: throw new MappingException("Unknown generator type: " . $class->generatorType); } }
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); }