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

15
src/DataStructures/QueryResponseRow.php

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

2
src/Enumerators/DeegraphEqualityOperator.php

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

2
src/Enumerators/DeegraphNumericalComparator.php

@ -1,6 +1,6 @@
<?php <?php
namespace Darksparrow\DeegraphPHP\Enumerators; namespace Darksparrow\DeegraphInteractions\Enumerators;
enum DeegraphNumericalComparator: string 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; namespace Darksparrow\DeegraphInteractions\Interfaces;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer; 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 interface QueryInstanceInterface
{ {
@ -11,7 +17,7 @@ interface QueryInstanceInterface
* *
* @param DeegraphServer $deegraphServer * @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 public function At(string $relativePath): PutQueryBuilder
{ {
self::EnsureNotSet($this->PutInto); self::EnsureNotSet($this->PutInto);
self::ValidateDeegraphPath($relativePath);
$this->PutAt = self::ValidateDeegraphPath( $this->PutAt = self::ValidateDeegraphPath(
target: $relativePath, target: $relativePath,
); );

3
src/QueryInstance/DeleteQuery.php

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

3
src/QueryInstance/GrantQuery.php

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

5
src/QueryInstance/InsertQuery.php

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

9
src/QueryInstance/PutQuery.php

@ -3,17 +3,18 @@
namespace Darksparrow\DeegraphInteractions\QueryInstance; namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer; 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\QueryResponse\PutQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass; 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( $response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query", endpoint: "/api/v1/@query",
method: "POST", method: HTTPMethod::POST,
body: $this->QueryString, body: $this->QueryString,
); );
return new PutQueryResponse(response: $response); return new PutQueryResponse(response: $response);

5
src/QueryInstance/SelectQuery.php

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

3
src/QueryResponse/SelectQueryResponse.php

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

5
src/Superclasses/QueryInstanceSuperclass.php

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

25
src/Traits/QueryBuilderTrait.php

@ -14,6 +14,17 @@ use ReflectionClass;
trait QueryBuilderTrait 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 protected function RegexValidate(string $target, string $pattern): string
{ {
if(!preg_match(pattern: $pattern, subject: $target)) if(!preg_match(pattern: $pattern, subject: $target))
@ -21,6 +32,14 @@ trait QueryBuilderTrait
return $target; 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 protected function EnsureNotSet(string $target): void
{ {
if($target != "") if($target != "")
@ -35,7 +54,11 @@ trait QueryBuilderTrait
} }
public function ValidateValues( public function ValidateValues(
DeleteQueryBuilder|GrantQueryBuilder|InsertQueryBuilder|PutQueryBuilder|SelectQueryBuilder $target DeleteQueryBuilder
|GrantQueryBuilder
|InsertQueryBuilder
|PutQueryBuilder
|SelectQueryBuilder $target
): void ): void
{ {
$nameBase = "Darksparrow\\DeegraphPHP\\Attributes"; $nameBase = "Darksparrow\\DeegraphPHP\\Attributes";

13
tests/QueryBuilderPutTest.php

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

Loading…
Cancel
Save