public function findAll(array $filters = [])
{
$pagination = $this->getPagination($filters);
$parameters = [];
$label = $this->classMetadata->getLabel();
$query = sprintf('MATCH (n:%s)', $label);
if (null !== $pagination) {
$query .= ' WITH n ORDER BY ';
if (null !== $pagination->getOrderBy()) {
$query .= 'n.' . $pagination->getOrderBy()[0] . ' ' . $pagination->getOrderBy()[1] . ' ';
} else {
$query .= 'id(n) ASC ';
}
$query .= ' SKIP {skip} LIMIT {limit}';
$parameters['skip'] = $pagination->getFirst();
$parameters['limit'] = $pagination->getMax();
}
/** @var RelationshipMetadata[] $associations */
$associations = $this->classMetadata->getRelationships();
$assocReturns = [];
foreach ($associations as $identifier => $association) {
$type = $association->isRelationshipEntity() ? $this->entityManager->getRelationshipEntityMetadata($association->getRelationshipEntityClass())->getType() : $association->getType();
switch ($association->getDirection()) {
case 'INCOMING':
$relStr = '<-[rel_%s:%s]-';
break;
case 'OUTGOING':
$relStr = '-[rel_%s:%s]->';
break;
default:
$relStr = '-[rel_%s:%s]-';
break;
}
$relationshipIdentifier = sprintf('%s_%s', strtolower($association->getPropertyName()), strtolower($type));
$relQueryPart = sprintf($relStr, $relationshipIdentifier, $type);
$query .= PHP_EOL;
$query .= 'OPTIONAL MATCH (n)' . $relQueryPart . '(' . $association->getPropertyName() . ')';
$query .= ' WITH n, ';
$query .= implode(', ', $assocReturns);
if (!empty($assocReturns)) {
$query .= ', ';
}
$relid = $relid = 'rel_' . $relationshipIdentifier;
if ($association->hasOrderBy()) {
$orderProperty = $association->getPropertyName() . '.' . $association->getOrderByPropery();
if ($association->isRelationshipEntity()) {
$reMetadata = $this->entityManager->getRelationshipEntityMetadata($association->getRelationshipEntityClass());
$split = explode('.', $association->getOrderByPropery());
if (count($split) > 1) {
$reName = $split[0];
$v = $split[1];
if ($reMetadata->getStartNodePropertyName() === $reName || $reMetadata->getEndNodePropertyName() === $reName) {
$orderProperty = $association->getPropertyName() . '.' . $v;
}
} else {
if (null !== $reMetadata->getPropertyMetadata($association->getOrderByPropery())) {
$orderProperty = $relid . '.' . $association->getOrderByPropery();
}
}
}
$query .= $relid . ', ' . $association->getPropertyName() . ' ORDER BY ' . $orderProperty . ' ' . $association->getOrder();
$query .= PHP_EOL;
$query .= ' WITH n, ';
$query .= implode(', ', $assocReturns);
if (!empty($assocReturns)) {
$query .= ', ';
}
}
if ($association->isCollection() || $association->isRelationshipEntity()) {
$query .= sprintf(' CASE count(%s) WHEN 0 THEN [] ELSE collect({start:startNode(%s), end:endNode(%s), rel:%s}) END as %s', $relid, $relid, $relid, $relid, $relid);
$assocReturns[] = $relid;
} else {
$query .= $association->getPropertyName();
$assocReturns[] = $association->getPropertyName();
}
}
if (null !== $pagination) {
$query .= ' WITH n';
if (!empty($assocReturns)) {
$query .= ', ' . implode(',', $assocReturns);
}
$query .= ' ORDER BY ';
if (null !== $pagination->getOrderBy()) {
$query .= 'n.' . $pagination->getOrderBy()[0] . ' ' . $pagination->getOrderBy()[1] . ' ';
} else {
$query .= 'id(n) ASC ';
}
$parameters['skip'] = $pagination->getFirst();
$parameters['limit'] = $pagination->getMax();
}
$query .= PHP_EOL;
$query .= 'RETURN n';
if (!empty($assocReturns)) {
$query .= ', ' . implode(', ', $assocReturns);
}
if (isset($filters[self::FILTER_ORDER])) {
foreach ($filters[self::FILTER_ORDER] as $key => $filter) {
if (array_key_exists($key, $this->classMetadata->getPropertiesMetadata())) {
$query .= sprintf(' ORDER BY n.%s %s', $key, $filter);
}
}
}
if (isset($filters[self::FILTER_LIMIT]) && is_numeric($filters[self::FILTER_LIMIT])) {
$query .= ' LIMIT {limit}';
$parameters[self::FILTER_LIMIT] = $filters[self::FILTER_LIMIT];
}
$tag = ['class' => self::class, 'method' => 'findAll', 'arguments' => $filters];
$result = $this->entityManager->getDatabaseDriver()->run($query, $parameters, json_encode($tag));
return $this->hydrateResultSet($result);
}