Skip to content

Commit 39f4458

Browse files
committed
Added response parser and configuration class with their tests (both login and other params)
1 parent 2e4c7eb commit 39f4458

File tree

11 files changed

+443
-211
lines changed

11 files changed

+443
-211
lines changed

src/Configuration.php

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,15 @@
22

33
namespace Neo4j\QueryAPI;
44

5+
use Neo4j\QueryAPI\Objects\Bookmarks;
6+
use Neo4j\QueryAPI\Enums\AccessMode;
7+
58
class Configuration
69
{
7-
private string $baseUrl;
8-
private string $authToken;
9-
private array $defaultHeaders;
10-
1110
public function __construct(
12-
string $baseUrl = 'https://localhost:7474',
13-
string $authToken = '',
14-
array $defaultHeaders = []
15-
) {
16-
$this->baseUrl = $baseUrl;
17-
$this->authToken = $authToken;
18-
$this->defaultHeaders = $defaultHeaders;
19-
}
20-
21-
/**
22-
* Set the base URL of the API.
23-
*
24-
* @param string $baseUrl
25-
* @return self
26-
*/
27-
public function setBaseUrl(string $baseUrl): self
28-
{
29-
$this->baseUrl = $baseUrl;
30-
return $this;
31-
}
32-
33-
/**
34-
* Set the authentication token.
35-
*
36-
* @param string $authToken
37-
* @return self
38-
*/
39-
public function setAuthToken(string $authToken): self
40-
{
41-
$this->authToken = $authToken;
42-
return $this;
43-
}
44-
45-
/**
46-
* Set default headers for API requests.
47-
*
48-
* @param array $headers
49-
* @return self
50-
*/
51-
public function setDefaultHeaders(array $headers): self
52-
{
53-
$this->defaultHeaders = $headers;
54-
return $this;
55-
}
56-
57-
/**
58-
* Get the base URL of the API.
59-
*
60-
* @return string
61-
*/
62-
public function getBaseUrl(): string
63-
{
64-
return $this->baseUrl;
65-
}
66-
67-
/**
68-
* Get the authentication token.
69-
*
70-
* @return string
71-
*/
72-
public function getAuthToken(): string
73-
{
74-
return $this->authToken;
75-
}
76-
77-
/**
78-
* Get the default headers for API requests.
79-
*
80-
* @return array
81-
*/
82-
public function getDefaultHeaders(): array
83-
{
84-
return array_merge($this->defaultHeaders, [
85-
'Authorization' => 'Bearer ' . $this->authToken,
86-
'Content-Type' => 'application/json',
87-
]);
88-
}
11+
public readonly string $database = 'neo4j',
12+
public readonly bool $includeCounters = true,
13+
public readonly Bookmarks $bookmark = new Bookmarks([]),
14+
public readonly AccessMode $accessMode = AccessMode::WRITE,
15+
) {}
8916
}

src/Neo4jQueryAPI.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,78 @@
1010
use Neo4j\QueryAPI\Exception\Neo4jException;
1111
use Psr\Http\Client\RequestExceptionInterface;
1212
use Psr\Http\Message\ResponseInterface;
13+
use Neo4j\QueryAPI\loginConfig;
1314

1415
class Neo4jQueryAPI
1516
{
1617
private Client $client;
18+
private LoginConfig $loginConfig;
1719
private Configuration $config;
1820
private ResponseParser $responseParser;
1921

20-
public function __construct(Configuration $config, ResponseParser $responseParser)
22+
public function __construct(LoginConfig $loginConfig, ResponseParser $responseParser, Configuration $config)
2123
{
22-
$this->config = $config;
24+
$this->loginConfig = $loginConfig;
2325
$this->responseParser = $responseParser;
26+
$this->config = $config;
2427

2528
$this->client = new Client([
26-
'base_uri' => rtrim($this->config->getBaseUrl(), '/'),
27-
'timeout' => 10.0,
28-
'headers' => $this->config->getDefaultHeaders(),
29+
'base_uri' => rtrim($this->loginConfig->baseUrl, '/'),
30+
'timeout' => 10.0,
31+
'headers' => [
32+
'Authorization' => 'Basic ' . $this->loginConfig->authToken,
33+
'Accept' => 'application/vnd.neo4j.query',
34+
],
2935
]);
3036
}
3137

38+
3239
/**
3340
* Static method to create an instance with login details.
3441
*/
35-
public static function login(string $address, string $username, string $password): self
42+
public static function login(): self
3643
{
37-
$authToken = base64_encode("$username:$password");
38-
$config = (new Configuration())
39-
->setBaseUrl($address)
40-
->setAuthToken($authToken);
44+
$loginConfig = loginConfig::fromEnv();
45+
$config = new Configuration();
4146

42-
return new self($config, new ResponseParser(new OGM()));
47+
return new self($loginConfig, new ResponseParser(new OGM()), $config);
4348
}
4449

50+
51+
4552
/**
4653
* Executes a Cypher query.
4754
*
4855
* @throws Neo4jException|RequestExceptionInterface
4956
*/
50-
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null, ?string $impersonatedUser = null, AccessMode $accessMode = AccessMode::WRITE): ResultSet
57+
public function run(string $cypher, array $parameters = []): ResultSet
5158
{
5259
try {
5360
$payload = [
54-
'statement' => $cypher,
55-
'parameters' => empty($parameters) ? new \stdClass() : $parameters,
56-
'includeCounters' => true,
57-
'accessMode' => $accessMode->value,
61+
'statement' => $cypher,
62+
'parameters' => empty($parameters) ? new \stdClass() : $parameters,
63+
'includeCounters' => $this->config->includeCounters,
64+
'accessMode' => $this->config->accessMode->value,
5865
];
5966

60-
if ($bookmark !== null) {
61-
$payload['bookmarks'] = $bookmark->getBookmarks();
67+
if (!empty($this->config->bookmark)) {
68+
$payload['bookmarks'] = $this->config->bookmark;
6269
}
6370

64-
if ($impersonatedUser !== null) {
65-
$payload['impersonatedUser'] = $impersonatedUser;
66-
}
6771

68-
$response = $this->client->post("/db/{$database}/query/v2", ['json' => $payload]);
72+
73+
// if ($impersonatedUser !== null) {
74+
// $payload['impersonatedUser'] = $impersonatedUser;
75+
// }
76+
error_log('Neo4j Payload: ' . json_encode($payload));
77+
78+
$response = $this->client->post("/db/{$this->config->database}/query/v2", ['json' => $payload]);
6979

7080
return $this->responseParser->parseRunQueryResponse($response);
7181
} catch (RequestException $e) {
72-
$this->handleRequestException($e);
82+
error_log('Neo4j Request Failed: ' . $e->getMessage());
83+
84+
$this->handleRequestException($e);
7385
}
7486
}
7587

src/OGM.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@ class OGM
1717
*/
1818
public function map(array $object): mixed
1919
{
20+
if (!isset($object['$type'])) {
21+
if (isset($object['elementId'], $object['labels'], $object['properties'])) {
22+
return $this->mapNode($object); // Handle as a Node
23+
}
24+
throw new \InvalidArgumentException('Unknown object type: ' . json_encode($object));
25+
}
26+
27+
// if (!isset($object['_value'])) {
28+
// throw new \InvalidArgumentException('Missing _value key in object: ' . json_encode($object));
29+
// }
30+
2031
return match ($object['$type']) {
21-
'Integer', 'Float', 'String', 'Boolean', 'Null', 'Duration', 'OffsetDateTime' => $object['_value'],
32+
'Integer', 'Float', 'String', 'Boolean', 'Duration', 'OffsetDateTime' => $object['_value'],
2233
'Array' => $object['_value'],
34+
'Null' => null,
2335
'List' => array_map([$this, 'map'], $object['_value']),
2436
'Node' => $this->mapNode($object['_value']),
2537
'Map' => $this->mapProperties($object['_value']),
2638
'Point' => $this->parseWKT($object['_value']),
2739
'Relationship' => $this->mapRelationship($object['_value']),
2840
'Path' => $this->mapPath($object['_value']),
29-
default => throw new \InvalidArgumentException('Unknown type: ' . $object['$type']),
41+
default => throw new \InvalidArgumentException('Unknown type: ' . $object['$type'] . ' in object: ' . json_encode($object)),
3042
};
3143
}
3244

45+
3346
/**
3447
* Parse Well-Known Text (WKT) format to a Point object.
3548
*

src/Objects/Bookmarks.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

33
namespace Neo4j\QueryAPI\Objects;
4+
use JsonSerializable;
5+
46
/**
57
* @api
68
*/
7-
class Bookmarks implements \Countable
9+
class Bookmarks implements \Countable, JsonSerializable
810
{
911
public function __construct(private array $bookmarks)
1012
{
@@ -27,4 +29,9 @@ public function count(): int
2729
{
2830
return count($this->bookmarks);
2931
}
32+
33+
public function jsonSerialize(): array
34+
{
35+
return $this->bookmarks;
36+
}
3037
}

src/Objects/ProfiledQueryPlan.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
?float $pageCacheHitRatio = 0.0,
3030
?int $time = 0,
3131
?string $operatorType = '',
32-
QueryArguments $arguments
32+
// ?QueryArguments $arguments = null,
3333
) {
3434
$this->dbHits = $dbHits ?? 0;
3535
$this->records = $records ?? 0;
@@ -39,7 +39,7 @@ public function __construct(
3939
$this->pageCacheHitRatio = $pageCacheHitRatio ?? 0.0;
4040
$this->time = $time ?? 0;
4141
$this->operatorType = $operatorType ?? '';
42-
$this->arguments = $arguments;
42+
// $this->arguments = $arguments ?? new QueryArguments();
4343
}
4444
/**
4545
* @api

0 commit comments

Comments
 (0)