5
5
use GuzzleHttp \Client ;
6
6
use Neo4j \QueryAPI \Exception \Neo4jException ;
7
7
use Neo4j \QueryAPI \Results \ResultRow ;
8
+ use Neo4j \QueryAPI \Results \ResultSet ;
8
9
use Psr \Http \Client \ClientInterface ;
9
- use Psr \Http \Client \RequestExceptionInterface ;
10
10
use stdClass ;
11
11
12
12
class Transaction
13
13
{
14
-
15
- public function __construct (private ClientInterface $ client , private string $ clusterAffinity , private string $ transactionId )
16
- {
14
+ public function __construct (
15
+ private ClientInterface $ client ,
16
+ private string $ clusterAffinity ,
17
+ private string $ transactionId
18
+ ) {
17
19
}
18
20
19
21
/**
20
- * Create a node in Neo4j with a specified label and properties .
22
+ * Execute a Cypher query within the transaction .
21
23
*
22
24
* @param string $query The Cypher query to be executed.
23
- * @param $parameters
24
- * @return array The response data from Neo4j.
25
+ * @param array $parameters Parameters for the query.
26
+ * @return ResultSet The result rows in ResultSet format.
27
+ * @throws Neo4jException If the response structure is invalid.
25
28
*/
26
- public function run (string $ query , array $ parameters ): array
29
+ public function run (string $ query , array $ parameters ): ResultSet
27
30
{
28
- // Execute the request to the Neo4j server
29
- $ response = $ this ->client ->post ("/db/neo4j/query/v2/tx " , [
30
- 'headers ' => [
31
- 'neo4j-cluster-affinity ' => $ this ->clusterAffinity ,
32
- ],
33
- 'json ' => [
31
+ $ response = $ this ->client ->post ("/db/neo4j/query/v2/tx/ {$ this ->transactionId }" , [
32
+ 'headers ' => [
33
+ 'neo4j-cluster-affinity ' => $ this ->clusterAffinity ,
34
+ ],
35
+ 'json ' => [
36
+ 'statement ' => $ query ,
37
+ 'parameters ' => empty ($ parameters ) ? new stdClass () : $ parameters ,
38
+ ],
39
+ ]);
34
40
35
- ' statement ' => $ query ,
36
- ' parameters ' => empty ( $ parameters ) ? new stdClass () : $ parameters , // Pass the parameters array here
41
+ $ responseBody = $ response -> getBody ()-> getContents ();
42
+ $ data = json_decode ( $ responseBody , true );
37
43
38
- ],
44
+ if (!isset ($ data ['data ' ]['fields ' ], $ data ['data ' ]['values ' ])) {
45
+ throw new Neo4jException ([
46
+ 'message ' => 'Unexpected response structure from Neo4j ' ,
47
+ 'response ' => $ data ,
39
48
]);
40
-
41
- // Decode the response body
42
- $ data = json_decode ($ response ->getBody ()->getContents (), true );
43
-
44
- // Initialize the OGM (Object Graph Mapping) class
45
- $ ogm = new OGM ();
46
-
47
- // Extract keys (field names) and values (actual data)
48
- $ keys = $ data ['results ' ][0 ]['columns ' ];
49
- $ values = $ data ['results ' ][0 ]['data ' ];
50
-
51
- // Process each row of the result and map them using OGM
52
- $ rows = array_map (function ($ resultRow ) use ($ ogm , $ keys ) {
53
- $ data = [];
54
- foreach ($ keys as $ index => $ key ) {
55
- $ fieldData = $ resultRow ['row ' ][$ index ] ?? null ;
56
- $ data [$ key ] = $ ogm ->map ($ fieldData ); // Map the field data to the appropriate object format
57
- }
58
- return new ResultRow ($ data ); // Wrap the mapped data in a ResultRow object
59
- }, $ values );
60
-
61
- return $ rows ; // Return the processed rows as an array of ResultRow objects
62
-
63
-
49
+ }
50
+
51
+ $ keys = $ data ['data ' ]['fields ' ];
52
+ $ values = $ data ['data ' ]['values ' ];
53
+
54
+ if (empty ($ values )) {
55
+ return new ResultSet ([]);
56
+ }
57
+
58
+ $ ogm = new OGM ();
59
+ $ rows = array_map (function ($ resultRow ) use ($ ogm , $ keys ) {
60
+ $ data = [];
61
+ foreach ($ keys as $ index => $ key ) {
62
+ $ fieldData = $ resultRow [$ index ] ?? null ;
63
+ $ data [$ key ] = $ ogm ->map ($ fieldData );
64
+ }
65
+ return new ResultRow ($ data );
66
+ }, $ values );
67
+
68
+ return new ResultSet ($ rows );
64
69
}
65
70
66
-
67
-
68
71
public function commit (): void
69
72
{
70
73
$ this ->client ->post ("/db/neo4j/query/v2/tx/ {$ this ->transactionId }/commit " , [
71
74
'headers ' => [
72
75
'neo4j-cluster-affinity ' => $ this ->clusterAffinity ,
73
- ]
76
+ ],
74
77
]);
75
78
}
76
79
@@ -79,7 +82,7 @@ public function rollback(): void
79
82
$ this ->client ->delete ("/db/neo4j/query/v2/tx/ {$ this ->transactionId }" , [
80
83
'headers ' => [
81
84
'neo4j-cluster-affinity ' => $ this ->clusterAffinity ,
82
- ]
85
+ ],
83
86
]);
84
87
}
85
88
}
0 commit comments