public function getMetadata(Metadata $documentMetadata, StructureMetadata $structure) { $classMetadata = $this->factory->createClassMetadata($documentMetadata->getClass()); $class = $documentMetadata->getReflectionClass(); $indexMeta = $this->factory->createIndexMetadata(); $indexMeta->setIdField($this->factory->createMetadataField('uuid')); $indexMeta->setLocaleField($this->factory->createMetadataField('locale')); $indexName = 'page'; $categoryName = 'page'; // See if the mapping overrides the default index and category name foreach ($this->mapping as $className => $mapping) { if ($documentMetadata->getAlias() !== $className && $class->name !== $className && false === $class->isSubclassOf($className)) { continue; } $indexName = $mapping['index']; $categoryName = $mapping['category']; } $indexMeta->setCategoryName($categoryName); $indexMeta->setIndexName($indexName); foreach ($structure->getProperties() as $property) { if ($property instanceof BlockMetadata) { $propertyMapping = new ComplexMetadata(); foreach ($property->getComponents() as $component) { foreach ($component->getChildren() as $componentProperty) { if (false === $componentProperty->hasTag('sulu.search.field')) { continue; } $tag = $componentProperty->getTag('sulu.search.field'); $tagAttributes = $tag['attributes']; if (!isset($tagAttributes['index']) || $tagAttributes['index'] !== 'false') { $propertyMapping->addFieldMapping($property->getName() . '.' . $componentProperty->getName(), ['type' => isset($tagAttributes['type']) ? $tagAttributes['type'] : 'string', 'field' => $this->factory->createMetadataProperty('[' . $componentProperty->getName() . ']'), 'aggregate' => true, 'indexed' => false]); } } } $indexMeta->addFieldMapping($property->getName(), ['type' => 'complex', 'mapping' => $propertyMapping, 'field' => $this->getContentField($property)]); } else { $this->mapProperty($property, $indexMeta); } } if ($class->isSubclassOf(ResourceSegmentBehavior::class)) { $indexMeta->setUrlField($this->factory->createMetadataField('resourceSegment')); } if (!$indexMeta->getTitleField()) { if ($class->isSubclassOf(TitleBehavior::class)) { $indexMeta->setTitleField($this->factory->createMetadataProperty('title')); $indexMeta->addFieldMapping('title', ['type' => 'string', 'field' => $this->factory->createMetadataField('title'), 'aggregate' => true, 'indexed' => false]); } } if ($class->isSubclassOf(WebspaceBehavior::class)) { // index the webspace $indexMeta->addFieldMapping('webspace_key', ['type' => 'string', 'field' => $this->factory->createMetadataProperty('webspaceName')]); } if ($class->isSubclassOf(WorkflowStageBehavior::class)) { $indexMeta->addFieldMapping('state', ['type' => 'string', 'field' => $this->factory->createMetadataExpression('object.getWorkflowStage() == 1 ? "test" : "published"')]); } $indexMeta->addFieldMapping(self::FIELD_STRUCTURE_TYPE, ['type' => 'string', 'stored' => true, 'indexed' => false, 'field' => $this->factory->createMetadataProperty('structureType')]); $classMetadata->addIndexMetadata('_default', $indexMeta); return $classMetadata; }
public function testLoadMetadata() { $this->metadata->getReflectionClass()->willReturn(new \ReflectionClass($this->document->reveal())); $this->metadata->addFieldMapping('redirectType', Argument::any())->shouldBeCalled(); $this->metadata->addFieldMapping('redirectExternal', Argument::any())->shouldBeCalled(); $this->metadata->addFieldMapping('redirectTarget', Argument::any())->shouldBeCalled(); $this->subscriber->handleMetadataLoad($this->event->reveal()); }
/** * @param array $mapping * * @return Metadata */ private function getMetadata($mapping) { $metadata = new Metadata(); $metadata->setAlias($mapping['alias']); $metadata->setPhpcrType($mapping['phpcr_type']); $metadata->setClass($mapping['class']); return $metadata; }
/** * @param Metadata $metadata * * @return object */ private function instantiateFromMetadata(Metadata $metadata) { $class = $metadata->getClass(); if (!class_exists($class)) { throw new \RuntimeException(sprintf('Document class "%s" does not exist', $class)); } $document = new $class(); return $document; }
/** * @return Metadata */ private function getUnknownMetadata() { $metadata = new Metadata(); $metadata->setAlias(null); $metadata->setPhpcrType(null); $metadata->setClass(UnknownDocument::class); return $metadata; }