/** * Take the results for the given query an build a collection of appropriate entities * * @param array $results * @param Query $query * * @return \SplObjectStorage */ public function hydrate(array $results, Query $query) { $entities = new \SplObjectStorage(); $variables = $query->getVariables(); foreach ($results['rows'] as $variable => $values) { $class = $this->map->getClass($variables[$variable]); $metadata = $this->registry->getMetadata($class); foreach ($values as $value) { $entity = $this->createEntity($metadata, $value); if ($metadata instanceof NodeMetadata) { foreach ($results['nodes'] as $id => $node) { $labels = $metadata->getLabels(); if (count($labels) !== count(array_intersect($labels, $node['labels']))) { continue; } $idProp = $metadata->getId()->getProperty(); if ($value[$idProp] !== $node['properties'][$idProp]) { continue; } $this->entities->addInfo($entity, ['realId' => (int) $node['id']]); break; } } else { foreach ($results['relationships'] as $id => $rel) { if ($metadata->getType() !== $rel['type']) { continue; } $idProp = $metadata->getId()->getProperty(); if ($value[$idProp] !== $rel['properties'][$idProp]) { continue; } $this->entities->addInfo($entity, ['realId' => (int) $rel['id'], 'startNode' => (int) $rel['startNode'], 'endNode' => (int) $rel['endNode']]); break; } } $entities->attach($entity); } } $entities->rewind(); return $entities; }
/** * Clean query parameters by converting via the types defined by the properties * * @param Query $query * * @return array */ protected function cleanParameters(Query $query) { $params = $query->getParameters(); $references = $query->getReferences(); $variables = $query->getVariables(); foreach ($params as $key => &$values) { if (!isset($references[$key])) { continue; } foreach ($values as $k => &$value) { if (!isset($references[$key][$k])) { continue; } list($var, $prop) = explode('.', $references[$key][$k]); $class = $this->identityMap->getClass($variables[$var]); $metadata = $this->metadataRegistry->getMetadata($class); if (!$metadata->hasProperty($prop)) { continue; } $prop = $metadata->getProperty($prop); if ($prop->isNullable() && $value === null) { unset($values[$k]); continue; } $value = Types::getType($prop->getType())->convertToDatabaseValue($value, $prop); } } return $params; }