public function testLoadInformation_FromFullClassname() { $doctrineConfiguration = $this->setupDoctrine('FS\\SolrBundle\\Tests\\Doctrine\\Mapper'); $factory = new MetaInformationFactory(); $factory->setDoctrineConfiguration($doctrineConfiguration); $entityClassname = get_class(new ValidTestEntity()); $informations = $factory->loadInformation($entityClassname); $expected = $entityClassname; $this->assertEquals($expected, $informations->getClassName(), 'class from fullclassname not discovered'); }
/** * @param \ArrayAccess $document * @param object $sourceTargetEntity * * @return object * * @throws \InvalidArgumentException if $sourceTargetEntity is null */ public function toEntity(\ArrayAccess $document, $sourceTargetEntity) { if (null === $sourceTargetEntity) { throw new \InvalidArgumentException('$sourceTargetEntity should not be null'); } $metaInformationFactory = new MetaInformationFactory(); $metaInformation = $metaInformationFactory->loadInformation($sourceTargetEntity); $hydratedDocument = $this->indexHydrator->hydrate($document, $metaInformation); if ($this->hydrationMode == HydrationModes::HYDRATE_INDEX) { return $hydratedDocument; } $metaInformation->setEntity($hydratedDocument); if ($this->hydrationMode == HydrationModes::HYDRATE_DOCTRINE) { return $this->doctrineHydrator->hydrate($document, $metaInformation); } }
/** * @param object $entity */ public function updateDocument($entity) { $metaInformations = $this->metaInformationFactory->loadInformation($entity); $doc = $this->toDocument($metaInformations); $this->eventManager->handle(EventManager::UPDATE, new Event($this->solrClient, $metaInformations)); $this->addDocumentToIndex($doc); return true; }
/** * @param \ArrayAccess $document * @param object $sourceTargetEntity * * @return object * * @throws \InvalidArgumentException if $sourceTargetEntity is null */ public function toEntity(\ArrayAccess $document, $sourceTargetEntity) { if (null === $sourceTargetEntity) { throw new \InvalidArgumentException('$sourceTargetEntity should not be null'); } $metaInformation = $this->metaInformationFactory->loadInformation($sourceTargetEntity); if ($metaInformation->isDoctrineEntity() === false && $this->hydrationMode == HydrationModes::HYDRATE_DOCTRINE) { throw new \RuntimeException(sprintf('Please check your config. Given entity is not a Doctrine entity, but Doctrine hydration is enabled. Use setHydrationMode(HydrationModes::HYDRATE_DOCTRINE) to fix this.')); } $hydratedDocument = $this->indexHydrator->hydrate($document, $metaInformation); if ($this->hydrationMode == HydrationModes::HYDRATE_INDEX) { return $hydratedDocument; } $metaInformation->setEntity($hydratedDocument); if ($this->hydrationMode == HydrationModes::HYDRATE_DOCTRINE) { return $this->doctrineHydrator->hydrate($document, $metaInformation); } }
/** * @param object $entity * * @return bool */ public function updateDocument($entity) { $metaInformations = $this->metaInformationFactory->loadInformation($entity); if (!$this->addToIndex($metaInformations, $entity)) { return; } $doc = $this->toDocument($metaInformations); $event = new Event($this->solrClientCore, $metaInformations); $this->eventManager->dispatch(Events::PRE_UPDATE, $event); $this->addDocumentToIndex($doc, $metaInformations, $event); $this->eventManager->dispatch(Events::POST_UPDATE, $event); return true; }
/** * @param Field $field * * @return array */ private function mapCollection(Field $field) { /** @var Collection $value */ $value = $field->getValue(); $getter = $field->getGetterName(); if (!empty($getter)) { $values = array(); foreach ($value as $relatedObj) { $values[] = $relatedObj->{$getter}(); } return $values; } $collection = array(); foreach ($value as $object) { $metaInformation = $this->metaInformationFactory->loadInformation($object); $field = array(); $document = $this->createDocument($metaInformation); foreach ($document as $fieldName => $value) { $field[$fieldName] = $value; } $collection[] = $field; } return $collection; }
/** * @return MetaInformationFactory */ private function setupMetaInformationFactory() { $ormConfiguration = new Configuration(); $ormConfiguration->addEntityNamespace('FSTest:ValidTestEntity', 'FS\\SolrBundle\\Tests\\Doctrine\\Mapper'); $ormConfiguration->addEntityNamespace('FSTest:EntityCore0', 'FS\\SolrBundle\\Tests\\Doctrine\\Mapper'); $ormConfiguration->addEntityNamespace('FSTest:EntityCore1', 'FS\\SolrBundle\\Tests\\Doctrine\\Mapper'); $knowNamespaces = new KnownNamespaceAliases(); $knowNamespaces->addEntityNamespaces($ormConfiguration); $classnameResolver = new ClassnameResolver($knowNamespaces); $reader = new AnnotationReader(new \Doctrine\Common\Annotations\AnnotationReader()); $metaFactory = new MetaInformationFactory($reader); $metaFactory->setClassnameResolver($classnameResolver); return $metaFactory; }