GraphAware\Neo4j\OGM\Repository\BaseRepository::hydrate PHP Method

hydrate() public method

public hydrate ( GraphAware\Common\Result\Record $record, $andCheckAssociations = true, $identifier = 'n', $className = null, $andAddLazyLoad = false, $considerAllLazy = false )
$record GraphAware\Common\Result\Record
    public function hydrate(Record $record, $andCheckAssociations = true, $identifier = 'n', $className = null, $andAddLazyLoad = false, $considerAllLazy = false)
    {
        $classN = null !== $className ? $className : $this->className;
        $baseInstance = $this->hydrateNode($record->get($identifier), $classN);
        $cm = $this->entityManager->getClassMetadataFor($classN);
        if ($andCheckAssociations) {
            foreach ($this->classMetadata->getSimpleRelationships(false) as $key => $association) {
                $relId = sprintf('%s_%s', strtolower($association->getPropertyName()), strtolower($association->getType()));
                $relKey = $association->isCollection() ? sprintf('rel_%s', $relId) : $association->getPropertyName();
                if ($record->hasValue($relKey) && null !== $record->get($relKey)) {
                    if ($association->isCollection()) {
                        $association->initializeCollection($baseInstance);
                        foreach ($record->get($relKey) as $v) {
                            $nodeToUse = $association->getDirection() === 'OUTGOING' ? $v['end'] : $v['start'];
                            if ($association->getDirection() === 'BOTH') {
                                $baseId = $record->nodeValue($identifier)->identity();
                                $nodeToUse = $v['end']->identity() === $baseId ? $v['start'] : $v['end'];
                            }
                            $v2 = $this->hydrateNode($nodeToUse, $this->getTargetFullClassName($association->getTargetEntity()), true);
                            $association->addToCollection($baseInstance, $v2);
                            $this->entityManager->getUnitOfWork()->addManaged($v2);
                            $this->entityManager->getUnitOfWork()->addManagedRelationshipReference($baseInstance, $v2, $association->getPropertyName(), $association);
                            $this->setInversedAssociation($baseInstance, $v2, $association->getPropertyName());
                        }
                    } else {
                        $hydrator = $this->getHydrator($this->getTargetFullClassName($association->getTargetEntity()));
                        $relO = $hydrator->hydrateNode($record->get($relKey), $association->getTargetEntity(), true);
                        $association->setValue($baseInstance, $relO);
                        $this->entityManager->getUnitOfWork()->addManagedRelationshipReference($baseInstance, $relO, $association->getPropertyName(), $association);
                        $this->setInversedAssociation($baseInstance, $relO, $relKey);
                    }
                } else {
                    if ($andAddLazyLoad && $association->isCollection() && $association->isLazy()) {
                        $lazy = new LazyRelationshipCollection($this->entityManager, $baseInstance, $association->getTargetEntity(), $association);
                        $association->setValue($baseInstance, $lazy);
                    }
                }
            }
            foreach ($this->classMetadata->getRelationshipEntities() as $key => $relationshipEntity) {
                $class = $this->getTargetFullClassName($relationshipEntity->getRelationshipEntityClass());
                /** @var RelationshipEntityMetadata $reMetadata */
                $reMetadata = $this->entityManager->getRelationshipEntityMetadata($class);
                $recordKey = sprintf('rel_%s_%s', strtolower($relationshipEntity->getPropertyName()), strtolower($reMetadata->getType()));
                if (!$record->hasValue($recordKey) || null === $record->get($recordKey) || empty($record->get($recordKey))) {
                    continue;
                }
                $startNodeMetadata = $this->entityManager->getClassMetadataFor($reMetadata->getStartNode());
                $endNodeMetadata = $this->entityManager->getClassMetadataFor($reMetadata->getEndNode());
                if ($relationshipEntity->isCollection()) {
                    $v = new \GraphAware\Neo4j\OGM\Common\Collection();
                    if (!is_array($record->get($recordKey))) {
                        throw new \LogicException('Expected array record value');
                    }
                    foreach ($record->get($recordKey) as $reMap) {
                        $oo2 = $this->hydrateRelationshipEntity($reMetadata, $reMap, $startNodeMetadata, $endNodeMetadata, $baseInstance, $relationshipEntity);
                        $v->add($oo2);
                    }
                    $relationshipEntity->setValue($baseInstance, $v);
                } else {
                    $reMap = $record->get($recordKey);
                    if (!empty($reMap)) {
                        $reMap = $record->get($recordKey);
                        $relationshipEntity->setValue($baseInstance, $this->hydrateRelationshipEntity($reMetadata, $reMap[0], $startNodeMetadata, $endNodeMetadata, $baseInstance, $relationshipEntity));
                    }
                }
            }
            $lazyDone = [];
            foreach ($this->classMetadata->getLazyRelationships(true) as $relationship) {
                if (!$relationship->isRelationshipEntity()) {
                    $lazyDone[] = $relationship->getPropertyName();
                    $lazyCollection = new LazyRelationshipCollection($this->entityManager, $baseInstance, $relationship->getTargetEntity(), $relationship);
                    $relationship->setValue($baseInstance, $lazyCollection);
                    continue;
                }
                if ($relationship->isRelationshipEntity()) {
                    if ($relationship->isCollection()) {
                        $lazyCollection = new LazyRelationshipCollection($this->entityManager, $baseInstance, $relationship->getRelationshipEntityClass(), $relationship);
                        $relationship->setValue($baseInstance, $lazyCollection);
                    } else {
                    }
                }
            }
            if ($considerAllLazy) {
                foreach ($this->classMetadata->getSimpleRelationships() as $relationship) {
                    if ($relationship->isCollection()) {
                        if (!$relationship->isRelationshipEntity()) {
                            $lazyCollection = new LazyRelationshipCollection($this->entityManager, $baseInstance, $relationship->getTargetEntity(), $relationship);
                            $relationship->setValue($baseInstance, $lazyCollection);
                            continue;
                        }
                        if ($relationship->isRelationshipEntity()) {
                            $lazyCollection = new LazyRelationshipCollection($this->entityManager, $baseInstance, $relationship->getRelationshipEntityClass(), $relationship);
                            $relationship->setValue($baseInstance, $lazyCollection);
                        }
                    }
                }
            }
        }
        return $baseInstance;
    }