public function loadMetadataForClass($className, ClassMetadata $class)
{
/** @var $class \Doctrine\ODM\PHPCR\Mapping\ClassMetadata */
try {
$xmlRoot = $this->getElement($className);
} catch (DoctrineMappingException $e) {
// Convert Exception type for consistency with other drivers
throw new MappingException($e->getMessage(), $e->getCode(), $e);
}
if (!$xmlRoot) {
return;
}
if (isset($xmlRoot['repository-class'])) {
$class->setCustomRepositoryClassName((string) $xmlRoot['repository-class']);
}
if (isset($xmlRoot['translator'])) {
$class->setTranslator((string) $xmlRoot['translator']);
}
if (isset($xmlRoot['versionable']) && $xmlRoot['versionable'] !== 'false') {
$class->setVersioned(strtolower($xmlRoot['versionable']));
}
if (isset($xmlRoot['referenceable']) && $xmlRoot['referenceable'] !== 'false') {
$class->setReferenceable((bool) $xmlRoot['referenceable']);
}
if (isset($xmlRoot['uniqueNodeType']) && $xmlRoot['uniqueNodeType'] !== 'false') {
$class->setUniqueNodeType((bool) $xmlRoot['uniqueNodeType']);
}
if (isset($xmlRoot['is-leaf'])) {
if (!in_array($value = $xmlRoot['is-leaf'], array('true', 'false'))) {
throw new MappingException(sprintf('Value of is-leaf must be "true" or "false", got "%s" for class "%s"', $value, $className));
}
$class->setIsLeaf($value == 'true' ? true : false);
}
if (isset($xmlRoot->mixins)) {
$mixins = array();
foreach ($xmlRoot->mixins->mixin as $mixin) {
$attributes = $mixin->attributes();
if (!isset($attributes['type'])) {
throw new MappingException('<mixin> missing mandatory type attribute');
}
$mixins[] = (string) $attributes['type'];
}
$class->setMixins($mixins);
$attributes = $xmlRoot->mixins->attributes();
if (isset($attributes['inherit'])) {
$class->setInheritMixins($attributes['inherit']);
}
}
if (isset($xmlRoot['node-type'])) {
$class->setNodeType((string) $xmlRoot['node-type']);
}
if ($xmlRoot->getName() === 'mapped-superclass') {
$class->isMappedSuperclass = true;
}
if (isset($xmlRoot->field)) {
foreach ($xmlRoot->field as $field) {
$mapping = array();
$attributes = $field->attributes();
foreach ($attributes as $key => $value) {
$mapping[$key] = (string) $value;
// convert bool fields
if (in_array($key, array('id', 'multivalue', 'assoc', 'translated', 'nullable'))) {
$mapping[$key] = 'true' === $mapping[$key] ? true : false;
}
}
if (!isset($mapping['name'])) {
throw new MappingException(sprintf('Missing name attribute for field of %s', $className));
}
$mapping['fieldName'] = $mapping['name'];
unset($mapping['name']);
$class->mapField($mapping);
}
}
if (isset($xmlRoot->id)) {
$mapping = array('fieldName' => (string) $xmlRoot->id->attributes()->name, 'id' => true);
if (isset($xmlRoot->id->generator) && isset($xmlRoot->id->generator->attributes()->strategy)) {
$mapping['strategy'] = (string) $xmlRoot->id->generator->attributes()->strategy;
}
$class->mapId($mapping);
}
if (isset($xmlRoot->node)) {
$class->mapNode(array('fieldName' => (string) $xmlRoot->node->attributes()->name));
}
if (isset($xmlRoot->nodename)) {
$class->mapNodename(array('fieldName' => (string) $xmlRoot->nodename->attributes()->name));
}
if (isset($xmlRoot->{'parent-document'})) {
$mapping = array('fieldName' => (string) $xmlRoot->{'parent-document'}->attributes()->name, 'cascade' => isset($xmlRoot->{'parent-document'}->cascade) ? $this->getCascadeMode($xmlRoot->{'parent-document'}->cascade) : 0);
$class->mapParentDocument($mapping);
}
if (isset($xmlRoot->child)) {
foreach ($xmlRoot->child as $child) {
$attributes = $child->attributes();
$mapping = array('fieldName' => (string) $attributes->name, 'cascade' => isset($child->cascade) ? $this->getCascadeMode($child->cascade) : 0);
if (isset($attributes['node-name'])) {
$mapping['nodeName'] = (string) $attributes->{'node-name'};
}
$class->mapChild($mapping);
}
}
if (isset($xmlRoot->children)) {
foreach ($xmlRoot->children as $children) {
$attributes = $children->attributes();
$mapping = array('fieldName' => (string) $attributes->name, 'cascade' => isset($children->cascade) ? $this->getCascadeMode($children->cascade) : 0, 'filter' => isset($attributes['filter']) ? (array) $attributes->filter : null, 'fetchDepth' => isset($attributes['fetch-depth']) ? (int) $attributes->{'fetch-depth'} : -1, 'ignoreUntranslated' => !empty($attributes['ignore-untranslated']));
$class->mapChildren($mapping);
}
}
if (isset($xmlRoot->{'reference-many'})) {
foreach ($xmlRoot->{'reference-many'} as $reference) {
$attributes = $reference->attributes();
$reference['cascade'] = isset($reference->cascade) ? $this->getCascadeMode($reference->cascade) : 0;
$reference['fieldName'] = (string) $attributes->name ?: null;
$this->addReferenceMapping($class, $reference, 'many');
}
}
if (isset($xmlRoot->{'reference-one'})) {
foreach ($xmlRoot->{'reference-one'} as $reference) {
$attributes = $reference->attributes();
$reference['cascade'] = isset($reference->cascade) ? $this->getCascadeMode($reference->cascade) : 0;
$reference['fieldName'] = (string) $attributes->name ?: null;
$this->addReferenceMapping($class, $reference, 'one');
}
}
if (isset($xmlRoot->locale)) {
$class->mapLocale(array('fieldName' => (string) $xmlRoot->locale->attributes()->name));
}
if (isset($xmlRoot->depth)) {
$class->mapDepth(array('fieldName' => (string) $xmlRoot->depth->attributes()->name));
}
if (isset($xmlRoot->{'mixed-referrers'})) {
foreach ($xmlRoot->{'mixed-referrers'} as $mixedReferrers) {
$attributes = $mixedReferrers->attributes();
$mapping = array('fieldName' => (string) $attributes->name, 'referenceType' => isset($attributes['reference-type']) ? strtolower((string) $attributes->{'reference-type'}) : null);
$class->mapMixedReferrers($mapping);
}
}
if (isset($xmlRoot->referrers)) {
foreach ($xmlRoot->referrers as $referrers) {
$attributes = $referrers->attributes();
if (!isset($attributes['referenced-by'])) {
throw new MappingException("{$className} is missing the referenced-by attribute for the referrer field " . $attributes->name);
}
if (!isset($attributes['referring-document'])) {
throw new MappingException("{$className} is missing the referring-document attribute for the referrer field " . $attributes->name);
}
// referenceType is determined from the referencedBy field of referringDocument
$mapping = array('fieldName' => (string) $attributes->name, 'cascade' => isset($referrers->cascade) ? $this->getCascadeMode($referrers->cascade) : 0, 'referencedBy' => (string) $attributes->{'referenced-by'}, 'referringDocument' => (string) $attributes->{'referring-document'});
$class->mapReferrers($mapping);
}
}
if (isset($xmlRoot->{'version-name'})) {
$class->mapVersionName(array('fieldName' => (string) $xmlRoot->{'version-name'}->attributes()->name));
}
if (isset($xmlRoot->{'version-created'})) {
$class->mapVersionCreated(array('fieldName' => (string) $xmlRoot->{'version-created'}->attributes()->name));
}
if (isset($xmlRoot->{'lifecycle-callbacks'})) {
foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
$class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\\ODM\\PHPCR\\Event::' . (string) $lifecycleCallback['type']));
}
}
if (isset($xmlRoot->uuid)) {
$mapping = array();
$attributes = $xmlRoot->uuid->attributes();
foreach ($attributes as $key => $value) {
$mapping[$key] = (string) $value;
}
if (!array_key_exists('name', $mapping)) {
throw new MappingException(sprintf('Missing name attribute for field of %s', $className));
}
$mapping['uuid'] = true;
$mapping['fieldName'] = $mapping['name'];
$class->mapField($mapping);
}
if (isset($xmlRoot->{'child-class'})) {
$childClasses = array();
foreach ($xmlRoot->{'child-class'} as $requiredClass) {
$childClasses[] = (string) $requiredClass['name'];
}
$class->setChildClasses($childClasses);
}
$class->validateClassMapping();
}