Skip to content

Commit 2fb73f8

Browse files
committed
correct properties behaviour
1 parent 53bb866 commit 2fb73f8

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/Formatter/Specialised/HttpOGMTranslator.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ private function translateValue($value, HttpMetaInfo $meta): array
105105

106106
if (is_array($value)) {
107107
if ($meta->getCurrentType() === 'path') {
108+
/**
109+
* There are edge cases where multiple paths are wrapped in a list.
110+
*
111+
* @see OGMFormatterIntegrationTest::testPathMultiple for an example
112+
*/
108113
if (is_array($value[0])) {
109114
$tbr = [];
110115
foreach ($value as $path) {
@@ -147,9 +152,9 @@ private function translateObject(object $value, HttpMetaInfo $meta): array
147152

148153
if ($type === 'node') {
149154
$node = $meta->currentNode();
150-
if ($node && (array) $value === (array) $node->properties) {
155+
if ($node && json_encode($value, JSON_THROW_ON_ERROR) === json_encode($node->properties, JSON_THROW_ON_ERROR)) {
151156
$meta = $meta->incrementMeta();
152-
[$map, $meta] = $this->translateCypherMap((array) $node->properties, $meta);
157+
$map = $this->translateProperties($node->properties);
153158

154159
return [new Node((int) $node->id, new CypherList($node->labels), $map), $meta];
155160
}
@@ -158,14 +163,29 @@ private function translateObject(object $value, HttpMetaInfo $meta): array
158163
return $this->translateCypherMap((array) $value, $meta);
159164
}
160165

166+
private function translateProperties(stdClass $properties): CypherMap
167+
{
168+
$tbr = [];
169+
foreach ((array) $properties as $key => $value) {
170+
if ($value instanceof stdClass) {
171+
$tbr[$key] = new CypherMap((array) $value);
172+
} elseif (is_array($value)) {
173+
$tbr[$key] = new CypherList($value);
174+
} else {
175+
$tbr[$key] = $value;
176+
}
177+
}
178+
179+
return new CypherMap($tbr);
180+
}
181+
161182
/**
162183
* @param RelationshipArray $relationship
163184
*/
164185
private function relationship(stdClass $relationship, HttpMetaInfo $meta): array
165186
{
166187
$meta = $meta->incrementMeta();
167-
/** @var array<string, OGMTypes> $map */
168-
[$map, $meta] = $this->translateCypherMap((array) $relationship->properties, $meta);
188+
$map = $this->translateProperties($relationship->properties);
169189

170190
$tbr = new Relationship(
171191
(int) $relationship->id,
@@ -225,7 +245,7 @@ private function path(array $value, HttpMetaInfo $meta): Path
225245
*/
226246
private function translateNode(array $node): Node
227247
{
228-
return new Node($node['id'], new CypherList($node['labels']), $this->translateCypherMap($node['properties']));
248+
return new Node($node['id'], new CypherList($node['labels']), $this->translateProperties($node['properties']));
229249
}
230250

231251
/**

tests/Integration/DriverParityTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\Tests\Integration;
1515

1616
use Laudis\Neo4j\Contracts\FormatterInterface;
17+
use Laudis\Neo4j\Contracts\PointInterface;
1718
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
1819
use Laudis\Neo4j\Tests\Fixtures\MoviesFixture;
1920
use Laudis\Neo4j\Types\ArrayList;
@@ -64,7 +65,12 @@ public function testComplex(string $alias): void
6465
{
6566
$results = $this->getClient()->run(<<<'CYPHER'
6667
MATCH (n:Person)-[r:ACTED_IN]->(m), p = () - [] -> () - [] -> ()
67-
RETURN n, p, {movie: m, roles: r.roles} AS actInfo, m
68+
SET m.point = point({latitude:12, longitude: 56, height: 1000})
69+
RETURN n,
70+
p,
71+
{movie: m, roles: r.roles} AS actInfo,
72+
m,
73+
point({latitude:13, longitude: 56, height: 1000}) as point
6874
LIMIT 1
6975
CYPHER, [], $alias);
7076

@@ -77,8 +83,13 @@ public function testComplex(string $alias): void
7783

7884
self::assertInstanceOf(ArrayList::class, $actorInfo->get('roles'));
7985
self::assertInstanceOf(Node::class, $actorInfo->get('movie'));
86+
// this can be a cyphermap in HTTP protocol
87+
$point = $actorInfo->getAsNode('movie')->getProperty('point');
88+
self::assertTrue($point instanceof PointInterface || $point instanceof CypherMap);
89+
self::assertIsObject($actorInfo->getAsNode('movie')->getProperty('point'));
8090
self::assertInstanceOf(Path::class, $result->get('p'));
8191
self::assertInstanceOf(Node::class, $result->get('m'));
92+
self::assertInstanceOf(PointInterface::class, $result->get('point'));
8293
}
8394
}
8495
}

0 commit comments

Comments
 (0)