Browse Source

just a bit of tidying, nowt exciting

Cerys Lewis 4 months ago
parent
commit
0a9f77f82f
  1. 6
      src/Attributes/QueryBuilderQuery.php
  2. 6
      src/Attributes/QueryBuilderRequiredField.php
  3. 8
      src/Core/DeegraphServer.php
  4. 13
      src/DataStructures/QueryResponseRow.php
  5. 2
      src/Enumerators/DeegraphEqualityOperator.php
  6. 2
      src/Enumerators/DeegraphNumericalComparator.php
  7. 14
      src/Enumerators/HTTPMethod.php
  8. 10
      src/Interfaces/QueryInstanceInterface.php
  9. 1
      src/QueryBuilder/QueryBuilders/PutQueryBuilder.php
  10. 3
      src/QueryInstance/DeleteQuery.php
  11. 3
      src/QueryInstance/GrantQuery.php
  12. 5
      src/QueryInstance/InsertQuery.php
  13. 9
      src/QueryInstance/PutQuery.php
  14. 5
      src/QueryInstance/SelectQuery.php
  15. 3
      src/QueryResponse/SelectQueryResponse.php
  16. 5
      src/Superclasses/QueryInstanceSuperclass.php
  17. 25
      src/Traits/QueryBuilderTrait.php
  18. 13
      tests/QueryBuilderPutTest.php

6
src/Attributes/QueryBuilderQuery.php

@ -14,6 +14,10 @@ use ReflectionClass;
{
public function __construct()
{
parent::__construct(new Name("QueryBuilderQuery", []), [], []);
parent::__construct(
name: new Name("QueryBuilderQuery", []),
args: [],
attributes: [],
);
}
}

6
src/Attributes/QueryBuilderRequiredField.php

@ -14,6 +14,10 @@ use PhpParser\Node\Name;
)
{
$this->Required = $required;
parent::__construct(new Name("QueryBuilderRequiredField", []), [], []);
parent::__construct(
name: new Name("QueryBuilderRequiredField", []),
args: [],
attributes: [],
);
}
}

8
src/Core/DeegraphServer.php

@ -3,6 +3,7 @@
namespace Darksparrow\DeegraphInteractions\Core;
use Darksparrow\DeegraphInteractions\DataStructures\ServerInfo;
use Darksparrow\DeegraphInteractions\Enumerators\HTTPMethod;
use Exception;
final class DeegraphServer
@ -38,6 +39,7 @@ final class DeegraphServer
* Runs an API request against the Deegraph Server, and returns back information about the Deegraph Server.
*
* @return ServerInfo
* @throws Exception
*/
public function ServerInfo(): ServerInfo
{
@ -45,7 +47,7 @@ final class DeegraphServer
return ServerInfo::FromAPIResponse(response: $response);
}
public function RunRawRequest(string $endpoint, string $method = "GET", string $body = null): string
public function RunRawRequest(string $endpoint, HTTPMethod $method = HTTPMethod::GET, string $body = null): string
{
$ch = curl_init("https://{$this->ServerDomain}:{$this->Port}{$endpoint}");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
@ -55,7 +57,7 @@ final class DeegraphServer
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($method == "POST")
if($method == HTTPMethod::POST)
{
curl_setopt($ch, CURLOPT_POST, 1);
}
@ -65,7 +67,7 @@ final class DeegraphServer
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if($method == "POST" || $method == "PUT")
if($method == HTTPMethod::POST || $method == HTTPMethod::PUT)
{
if($body != null)
{

13
src/DataStructures/QueryResponseRow.php

@ -7,22 +7,17 @@ class QueryResponseRow
public string $Search;
public array $Results;
public static function FromArray(array $array): QueryResponseRow
public function __construct(array $array)
{
$builder = new QueryResponseRow();
$builder->Search = key(array: $array);
foreach($array["{$builder->Search}"] as $key => $value)
$this->Search = key(array: $array);
foreach($array["{$this->Search}"] as $key => $value)
{
$builder->Results[] = new KeyValuePair(key: $key, value: $value);
$this->Results[] = new KeyValuePair(key: $key, value: $value);
}
return $builder;
}
public function __toString(): string
{
return json_encode($this, JSON_PRETTY_PRINT);
}
}

2
src/Enumerators/DeegraphEqualityOperator.php

@ -9,5 +9,5 @@ enum DeegraphEqualityOperator: string
case IS = "===";
case DIFFERENT = "!=";
case ISNT = "ISNT";
case IS_NOT = "ISNT";
}

2
src/Enumerators/DeegraphNumericalComparator.php

@ -1,6 +1,6 @@
<?php
namespace Darksparrow\DeegraphPHP\Enumerators;
namespace Darksparrow\DeegraphInteractions\Enumerators;
enum DeegraphNumericalComparator: string
{

14
src/Enumerators/HTTPMethod.php

@ -0,0 +1,14 @@
<?php
namespace Darksparrow\DeegraphInteractions\Enumerators;
enum HTTPMethod
{
case GET;
case POST;
case PUT;
case DELETE;
}

10
src/Interfaces/QueryInstanceInterface.php

@ -3,6 +3,12 @@
namespace Darksparrow\DeegraphInteractions\Interfaces;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryResponse\DeleteQueryResponse;
use Darksparrow\DeegraphInteractions\QueryResponse\GrantQueryResponse;
use Darksparrow\DeegraphInteractions\QueryResponse\InsertQueryResponse;
use Darksparrow\DeegraphInteractions\QueryResponse\PutQueryResponse;
use Darksparrow\DeegraphInteractions\QueryResponse\SelectQueryResponse;
interface QueryInstanceInterface
{
@ -11,7 +17,7 @@ interface QueryInstanceInterface
*
* @param DeegraphServer $deegraphServer
*
* @return mixed
* @return DeleteQueryResponse|GrantQueryResponse|InsertQueryResponse|PutQueryResponse|SelectQueryResponse
*/
public function RunQuery(DeegraphServer $deegraphServer);
public function RunQuery(DeegraphServer $deegraphServer): mixed;
}

1
src/QueryBuilder/QueryBuilders/PutQueryBuilder.php

@ -86,6 +86,7 @@ final class PutQueryBuilder implements QueryBuilderInterface
public function At(string $relativePath): PutQueryBuilder
{
self::EnsureNotSet($this->PutInto);
self::ValidateDeegraphPath($relativePath);
$this->PutAt = self::ValidateDeegraphPath(
target: $relativePath,
);

3
src/QueryInstance/DeleteQuery.php

@ -3,6 +3,7 @@
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Enumerators\HTTPMethod;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\DeleteQueryResponse;
use Darksparrow\DeegraphInteractions\QueryResponse\GrantQueryResponse;
@ -14,7 +15,7 @@ final class DeleteQuery extends QueryInstanceSuperclass implements QueryInstance
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@delete",
method: "DELETE",
method: HTTPMethod::DELETE,
body: $this->QueryString,
);
return new DeleteQueryResponse(response: $response);

3
src/QueryInstance/GrantQuery.php

@ -3,6 +3,7 @@
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Enumerators\HTTPMethod;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\GrantQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
@ -13,7 +14,7 @@ final class GrantQuery extends QueryInstanceSuperclass implements QueryInstanceI
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
method: HTTPMethod::POST,
body: $this->QueryString,
);
return new GrantQueryResponse(response: $response);

5
src/QueryInstance/InsertQuery.php

@ -3,17 +3,18 @@
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Enumerators\HTTPMethod;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\InsertQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class InsertQuery extends QueryInstanceSuperclass implements QueryInstanceInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
public function RunQuery(DeegraphServer $deegraphServer): InsertQueryResponse
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
method: HTTPMethod::POST,
body: $this->QueryString,
);
return new InsertQueryResponse(response: $response);

9
src/QueryInstance/PutQuery.php

@ -3,17 +3,18 @@
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
use Darksparrow\DeegraphInteractions\Enumerators\HTTPMethod;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\PutQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class PutQuery extends QueryInstanceSuperclass implements QueryResponseInterface
final class PutQuery extends QueryInstanceSuperclass implements QueryInstanceInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
public function RunQuery(DeegraphServer $deegraphServer): PutQueryResponse
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
method: HTTPMethod::POST,
body: $this->QueryString,
);
return new PutQueryResponse(response: $response);

5
src/QueryInstance/SelectQuery.php

@ -3,17 +3,18 @@
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Enumerators\HTTPMethod;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\SelectQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class SelectQuery extends QueryInstanceSuperclass implements QueryInstanceInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
public function RunQuery(DeegraphServer $deegraphServer): SelectQueryResponse
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
method: HTTPMethod::POST,
body: $this->QueryString,
);
return new SelectQueryResponse(response: $response);

3
src/QueryResponse/SelectQueryResponse.php

@ -2,6 +2,7 @@
namespace Darksparrow\DeegraphInteractions\QueryResponse;
use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseRow;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
final class SelectQueryResponse implements QueryResponseInterface
@ -21,7 +22,7 @@ final class SelectQueryResponse implements QueryResponseInterface
{
$this->Rows = [];
foreach($response["@rows"] as $row)
$this->Rows[] = QueryResponseRow::FromArray(array: $row);
$this->Rows[] = new QueryResponseRow(array: $row);
$this->RowFormat = $response["@row_format"];
}
}

5
src/Superclasses/QueryInstanceSuperclass.php

@ -22,6 +22,11 @@ class QueryInstanceSuperclass
* @return string
*/
public function __toString(): string
{
return $this->AsString();
}
public function AsString(): string
{
return $this->QueryString;
}

25
src/Traits/QueryBuilderTrait.php

@ -14,6 +14,17 @@ use ReflectionClass;
trait QueryBuilderTrait
{
/**
* Takes in a string and a pattern.
* If the string matches the pattern, it'll return the given string, if it doesn't, it'll throw an exception.
*
* @param string $target
* @param string $pattern
*
* @return string
*
* @throws QueryBuilderInvalidInputException
*/
protected function RegexValidate(string $target, string $pattern): string
{
if(!preg_match(pattern: $pattern, subject: $target))
@ -21,6 +32,14 @@ trait QueryBuilderTrait
return $target;
}
/**
* Ensures that the provided string is equal to empty string (""))
*
* @param string $target
*
* @return void
* @throws QueryBuilderConflictingFieldAlreadyExistsException
*/
protected function EnsureNotSet(string $target): void
{
if($target != "")
@ -35,7 +54,11 @@ trait QueryBuilderTrait
}
public function ValidateValues(
DeleteQueryBuilder|GrantQueryBuilder|InsertQueryBuilder|PutQueryBuilder|SelectQueryBuilder $target
DeleteQueryBuilder
|GrantQueryBuilder
|InsertQueryBuilder
|PutQueryBuilder
|SelectQueryBuilder $target
): void
{
$nameBase = "Darksparrow\\DeegraphPHP\\Attributes";

13
tests/QueryBuilderPutTest.php

@ -9,23 +9,22 @@ final class QueryBuilderPutTest extends TestCase
{
public function testPutURIAtSafeWithValidData()
{
$queryBuilder = new QueryBuilder();
$query = $queryBuilder->Put()
$query = QueryBuilder::Put()
->URI("https://schemas.auxiliumsoftware.co.uk/v1/collection.json")
->At(node: "970334ed-1f4f-465a-94d7-923a99698786", uwu: "todos")
->At(relativePath: "{970334ed-1f4f-465a-94d7-923a99698786}/todos")
->Safe();
self::assertEquals(
expected: 'PUT URI "https://schemas.auxiliumsoftware.co.uk/v1/collection.json" AT {970334ed-1f4f-465a-94d7-923a99698786}/todos SAFE',
actual: $query
actual: $query->Build()->AsString(),
);
}
public function testPutWithBothThings()
{
$this->expectException(QueryBuilderConflictingFieldAlreadyExistsException::class);
$queryBuilder = new QueryBuilder();
$query = $queryBuilder->Put()
$query = QueryBuilder::Put()
->URI("https://schemas.auxiliumsoftware.co.uk/v1/collection.json")
->At(node: "970334ed-1f4f-465a-94d7-923a99698786", uwu: "todos")
->At(relativePath: "{970334ed-1f4f-465a-94d7-923a99698786}/todos")
->Into(relativePath: "Relative Path", propertyName: "OwO")
->Safe();
}

Loading…
Cancel
Save