Cerys
4 weeks ago
6 changed files with 239 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphInteractions\QueryBuilder\LinkQuery; |
|||
|
|||
use Darksparrow\DeegraphInteractions\Core\DeegraphServer; |
|||
use Darksparrow\DeegraphInteractions\QueryBuilder\DirectoryQuery\DirectoryQueryResponse; |
|||
|
|||
class LinkQuery |
|||
{ |
|||
protected string $QueryString; |
|||
|
|||
public function __construct(string $queryString) |
|||
{ |
|||
$this->QueryString = $queryString; |
|||
} |
|||
public function __toString() |
|||
{ |
|||
return $this->QueryString; |
|||
} |
|||
|
|||
public function RunQuery(DeegraphServer $server): LinkQueryResponse |
|||
{ |
|||
$response = $server->RunRawRequest( |
|||
endpoint: "/api/v1/@query", |
|||
method: "POST", |
|||
body: $this->QueryString |
|||
); |
|||
$temp = json_decode($response, true); |
|||
return new LinkQueryResponse($temp); |
|||
} |
|||
} |
@ -0,0 +1,106 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphInteractions\QueryBuilder\LinkQuery; |
|||
|
|||
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery; |
|||
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField; |
|||
use Darksparrow\DeegraphInteractions\QueryBuilder\DirectoryQuery\DirectoryQuery; |
|||
use Darksparrow\DeegraphInteractions\QueryBuilder\DirectoryQuery\DirectoryQueryBuilder; |
|||
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait; |
|||
|
|||
#[QueryBuilderQuery] |
|||
class LinkQueryBuilder |
|||
{ |
|||
use QueryBuilderTrait; |
|||
|
|||
#[QueryBuilderRequiredField] |
|||
protected string $RelativePath1 = ""; |
|||
|
|||
#[QueryBuilderRequiredField] |
|||
protected string $ToOrOf = ""; |
|||
|
|||
#[QueryBuilderRequiredField] |
|||
protected string $RelativePath2 = ""; |
|||
|
|||
#[QueryBuilderRequiredField] |
|||
protected string $PropertyName = ""; |
|||
|
|||
|
|||
protected string $Mode = ""; |
|||
|
|||
|
|||
|
|||
|
|||
public function LinkWhat(string $relativePath): LinkQueryBuilder |
|||
{ |
|||
self::ValidateDeegraphPath(target: $relativePath); |
|||
$this->RelativePath1 = $relativePath; |
|||
return $this; |
|||
} |
|||
public function LinkTo(string $relativePath): LinkQueryBuilder |
|||
{ |
|||
self::EnsureNotSet($this->RelativePath2); |
|||
self::ValidateDeegraphPath(target: $relativePath); |
|||
$this->RelativePath2 = $relativePath; |
|||
$this->ToOrOf = "TO"; |
|||
return $this; |
|||
} |
|||
public function LinkOf(string $relativePath): LinkQueryBuilder |
|||
{ |
|||
self::EnsureNotSet($this->RelativePath2); |
|||
self::ValidateDeegraphPath(target: $relativePath); |
|||
$this->RelativePath2 = $relativePath; |
|||
$this->ToOrOf = "OF"; |
|||
return $this; |
|||
} |
|||
|
|||
|
|||
|
|||
public function PropertyName(string $propertyName): LinkQueryBuilder |
|||
{ |
|||
$this->PropertyName = $propertyName; |
|||
return $this; |
|||
} |
|||
|
|||
|
|||
|
|||
public function Overwrite(): LinkQueryBuilder |
|||
{ |
|||
self::EnsureNotSet($this->Mode); |
|||
$this->Mode = "OVERWRITE"; |
|||
return $this; |
|||
} |
|||
public function Replace(): LinkQueryBuilder |
|||
{ |
|||
self::EnsureNotSet($this->Mode); |
|||
$this->Mode = "REPLACE"; |
|||
return $this; |
|||
} |
|||
public function Force(): LinkQueryBuilder |
|||
{ |
|||
self::EnsureNotSet($this->Mode); |
|||
$this->Mode = "FORCE"; |
|||
return $this; |
|||
} |
|||
|
|||
|
|||
|
|||
public function Build(): LinkQuery |
|||
{ |
|||
self::ValidateValues(target: $this); |
|||
|
|||
$builder = "LINK " |
|||
. $this->RelativePath1 |
|||
. " " |
|||
. $this->ToOrOf |
|||
. " " |
|||
. $this->RelativePath2 |
|||
. " AS " |
|||
. $this->PropertyName; |
|||
|
|||
if($this->Mode != "") $builder .= " " . $this->Mode; |
|||
|
|||
return new LinkQuery(queryString: $builder); |
|||
} |
|||
} |
|||
|
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphInteractions\QueryBuilder\LinkQuery; |
|||
|
|||
class LinkQueryResponse |
|||
{ |
|||
public function __construct(array $deegraphResponse) |
|||
{ |
|||
} |
|||
} |
@ -0,0 +1,85 @@ |
|||
<?php |
|||
|
|||
|
|||
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException; |
|||
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder; |
|||
use PHPUnit\Framework\TestCase; |
|||
|
|||
final class QueryBuilderLinkTest extends TestCase |
|||
{ |
|||
private string $TestUUID = "{00000000-0000-0000-0000-000000000000}"; |
|||
|
|||
public function test0() |
|||
{ |
|||
self::assertEquals( |
|||
expected: "LINK {$this->TestUUID} TO {$this->TestUUID} AS test", |
|||
actual: QueryBuilder::Link() |
|||
->LinkWhat($this->TestUUID) |
|||
->LinkTo($this->TestUUID) |
|||
->PropertyName("test") |
|||
->Build() |
|||
); |
|||
} |
|||
public function test1() |
|||
{ |
|||
self::assertEquals( |
|||
expected: "LINK {$this->TestUUID} TO {$this->TestUUID} AS test OVERWRITE", |
|||
actual: QueryBuilder::Link() |
|||
->LinkWhat($this->TestUUID) |
|||
->LinkTo($this->TestUUID) |
|||
->PropertyName("test") |
|||
->Overwrite() |
|||
->Build() |
|||
); |
|||
} |
|||
public function test2() |
|||
{ |
|||
self::assertEquals( |
|||
expected: "LINK {$this->TestUUID} TO {$this->TestUUID} AS test REPLACE", |
|||
actual: QueryBuilder::Link() |
|||
->LinkWhat($this->TestUUID) |
|||
->LinkTo($this->TestUUID) |
|||
->PropertyName("test") |
|||
->Replace() |
|||
->Build() |
|||
); |
|||
} |
|||
public function test3() |
|||
{ |
|||
self::assertEquals( |
|||
expected: "LINK {$this->TestUUID} TO {$this->TestUUID} AS test FORCE", |
|||
actual: QueryBuilder::Link() |
|||
->LinkWhat($this->TestUUID) |
|||
->LinkTo($this->TestUUID) |
|||
->PropertyName("test") |
|||
->Force() |
|||
->Build() |
|||
); |
|||
} |
|||
public function test4() |
|||
{ |
|||
self::assertEquals( |
|||
expected: "LINK {$this->TestUUID} OF {$this->TestUUID} AS test", |
|||
actual: QueryBuilder::Link() |
|||
->LinkWhat($this->TestUUID) |
|||
->LinkOf($this->TestUUID) |
|||
->PropertyName("test") |
|||
->Build() |
|||
); |
|||
} |
|||
public function test5() |
|||
{ |
|||
self::expectException(QueryBuilderConflictingFieldAlreadyExistsException::class); |
|||
self::assertEquals( |
|||
expected: "LINK {$this->TestUUID} OF {$this->TestUUID} AS test", |
|||
actual: QueryBuilder::Link() |
|||
->LinkWhat($this->TestUUID) |
|||
->LinkOf($this->TestUUID) |
|||
->PropertyName("test") |
|||
->Overwrite() |
|||
->Replace() |
|||
->Force() |
|||
->Build() |
|||
); |
|||
} |
|||
} |
Loading…
Reference in new issue