Browse Source

working on getting a query response into classes

pull/4/head
Cerys Lewis 6 months ago
parent
commit
63d0f34705
  1. 4
      composer.json
  2. 21
      src/Core/DeegraphServer.php
  3. 32
      src/DataStructures/KeyValuePair.php
  4. 28
      src/DataStructures/QueryResponseRow.php
  5. 17
      src/DataStructures/QueryResponseWrapper.php
  6. 3
      src/QueryBuilder/QueryBuilder.php
  7. 5
      src/QueryBuilder/QueryBuilders/SelectQuery.php

4
composer.json

@ -22,10 +22,10 @@
}, },
"require": { "require": {
"php": ">=8.1", "php": ">=8.1",
"ext-curl": "*", "ext-curl": "*"
"phpunit/phpunit": "^9.5"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.5"
}, },
"scripts": { "scripts": {
"test": "phpunit" "test": "phpunit"

21
src/Core/DeegraphServer.php

@ -1,6 +1,6 @@
<?php <?php
namespace Darksparrow\DeegraphPHP\Core; namespace Darksparrow\DeegraphInteractions\Core;
use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseWrapper; use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseWrapper;
use Darksparrow\DeegraphInteractions\DataStructures\ServerInfo; use Darksparrow\DeegraphInteractions\DataStructures\ServerInfo;
@ -10,10 +10,12 @@ use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery;
class DeegraphServer class DeegraphServer
{ {
private string $ServerDomain;
private int $Port;
private string $Token; private string $Token;
private string $Actor; private string $Actor;
private string $ServerDomain;
private int $Port;
private bool $AllowSelfSignedCerts; private bool $AllowSelfSignedCerts;
public function __construct( public function __construct(
@ -23,10 +25,11 @@ class DeegraphServer
int $port = 8088, int $port = 8088,
bool $allowSelfSignedCerts = false) bool $allowSelfSignedCerts = false)
{ {
$this->ServerDomain = $server;
$this->Port = $port;
$this->Token = $token; $this->Token = $token;
$this->Actor = $actor; $this->Actor = $actor;
$this->ServerDomain = $server;
$this->Port = $port;
$this->AllowSelfSignedCerts = $allowSelfSignedCerts; $this->AllowSelfSignedCerts = $allowSelfSignedCerts;
} }
@ -65,8 +68,14 @@ class DeegraphServer
} }
} }
$result = curl_exec($ch); $result = curl_exec($ch);
if(curl_error($ch) == "SSL certificate problem: unable to get local issuer certificate")
{
throw new \Exception();
die();
}
curl_close($ch); curl_close($ch);
return $result; return $result;

32
src/DataStructures/KeyValuePair.php

@ -0,0 +1,32 @@
<?php
namespace Darksparrow\DeegraphInteractions\DataStructures;
class KeyValuePair
{
public string $Key;
public ?string $Value;
public function __construct(string $key, ?string $value)
{
$this->Key = $key;
$this->Value = $value;
}
public function __toString(): string
{
if(isset($this->Key) && isset($this->Value))
return "{$this->Key} => \"{$this->Value}\"";
if(!isset($this->Key) && isset($this->Value))
return "NULL => \"{$this->Value}\"";
if(isset($this->Key) && !isset($this->Value))
return "{$this->Key} => NULL";
if(isset($this->Key) && isset($this->Value))
return "NULL => NULL";
return "FAIL";
}
}

28
src/DataStructures/QueryResponseRow.php

@ -0,0 +1,28 @@
<?php
namespace Darksparrow\DeegraphInteractions\DataStructures;
class QueryResponseRow
{
public string $Search;
public array $Results;
public static function FromArray(array $array): QueryResponseRow
{
$builder = new QueryResponseRow();
$builder->Search = key(array: $array);
foreach($array["{$builder->Search}"] as $key=>$value)
{
$builder->Results[] = new KeyValuePair(key: $key, value: $value);
}
return $builder;
}
public function __toString(): string
{
return json_encode($this, JSON_PRETTY_PRINT);
}
}

17
src/DataStructures/QueryResponseWrapper.php

@ -13,9 +13,24 @@ class QueryResponseWrapper
$builder = new QueryResponseWrapper(); $builder = new QueryResponseWrapper();
$builder->Rows = $temp["@rows"]; foreach($temp["@rows"] as $row) $builder->Rows[] = QueryResponseRow::FromArray(array: $row);
$builder->RowFormat = $temp["@row_format"]; $builder->RowFormat = $temp["@row_format"];
return $builder; return $builder;
} }
public function Flatten(): array
{
$builder = [];
foreach($this->Rows as $row)
{
foreach($row->Results as $result)
{
$builder[] = $result;
}
}
return $builder;
}
} }

3
src/QueryBuilder/QueryBuilder.php

@ -6,9 +6,8 @@ use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery;
class QueryBuilder final class QueryBuilder
{ {
public static function Insert(): InsertQuery public static function Insert(): InsertQuery
{ {
return new InsertQuery(); return new InsertQuery();

5
src/QueryBuilder/QueryBuilders/SelectQuery.php

@ -38,6 +38,11 @@ final class SelectQuery
return $builder; return $builder;
} }
public function RelativePath(string $relativePath): SelectQuery
{
$this->RelativePaths = [$relativePath];
return $this;
}
public function RelativePaths(array $relativePaths): SelectQuery public function RelativePaths(array $relativePaths): SelectQuery
{ {
$this->RelativePaths = $relativePaths; $this->RelativePaths = $relativePaths;

Loading…
Cancel
Save