Browse Source

work on SELECT query response handling

dev
Cerys 3 weeks ago
parent
commit
1b11474b40
  1. 30
      src/DataStructures/QueryResponseRow.php
  2. 36
      src/DataStructures/QueryResponseWrapper.php
  3. 6
      src/QueryBuilder/SelectQuery/SelectQueryResponse.php
  4. 27
      src/QueryBuilder/SelectQuery/SelectQueryResponseRow.php

30
src/DataStructures/QueryResponseRow.php

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

36
src/DataStructures/QueryResponseWrapper.php

@ -1,36 +0,0 @@
<?php
namespace Darksparrow\DeegraphInteractions\DataStructures;
class QueryResponseWrapper
{
public array $Rows;
public string $RowFormat;
public static function FromAPIResponse(string $response): QueryResponseWrapper
{
$temp = json_decode($response, true);
$builder = new QueryResponseWrapper();
foreach($temp["@rows"] as $row) $builder->Rows[] = QueryResponseRow::FromArray(array: $row);
$builder->RowFormat = $temp["@row_format"];
return $builder;
}
public function Flatten(): array
{
$builder = [];
foreach($this->Rows as $row)
{
foreach($row->Results as $result)
{
$builder[] = $result;
}
}
return $builder;
}
}

6
src/QueryBuilder/SelectQuery/SelectQueryResponse.php

@ -2,8 +2,6 @@
namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery; namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseRow;
final class SelectQueryResponse final class SelectQueryResponse
{ {
public array $Rows; public array $Rows;
@ -11,8 +9,10 @@ final class SelectQueryResponse
public function __construct(array $response) public function __construct(array $response)
{ {
// var_dump($response);
foreach($response["@rows"] as $row) foreach($response["@rows"] as $row)
$this->Rows[] = new QueryResponseRow(array: $row); $this->Rows[] = new SelectQueryResponseRow(array: $row);
$this->RowFormat = $response["@row_format"]; $this->RowFormat = $response["@row_format"];
} }
} }

27
src/QueryBuilder/SelectQuery/SelectQueryResponseRow.php

@ -0,0 +1,27 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
class SelectQueryResponseRow
{
public ?array $Properties = null;
public function __construct(array $array)
{
if($array == [])
{
return;
}
foreach($array as $property=>$data)
{
foreach($data as $key => $value) {
$this->Properties[$property][$key] = $value;
}
}
}
public function __toString(): string
{
return json_encode($this, JSON_PRETTY_PRINT);
}
}
Loading…
Cancel
Save