Browse Source

just committing an untested Query Builder to show an example

pull/1/head
Cerys Lewis 7 months ago
parent
commit
d544274bd6
  1. 17
      src/Exceptions/QueryBuilderConflictingFieldAlreadyExistsException.php
  2. 17
      src/Exceptions/QueryBuilderInvalidInputException.php
  3. 17
      src/Exceptions/QueryBuilderRequiredFieldIsNotSetException.php
  4. 15
      src/QueryBuilder/QueryBuilder.php
  5. 23
      src/QueryBuilder/QueryBuilderTrait.php
  6. 113
      src/QueryBuilder/QueryBuilders/Put.php

17
src/Exceptions/QueryBuilderConflictingFieldAlreadyExistsException.php

@ -0,0 +1,17 @@
<?php
namespace Darksparrow\DeegraphPHP\Exceptions;
use Exception;
class QueryBuilderConflictingFieldAlreadyExistsException extends Exception
{
public function __construct(
string $message = "You have tried to set a field which would nullify a field you've previously set.",
int $code = 422
) {
parent::__construct($message, $code);
$this->message = "$message";
$this->code = $code;
}
}

17
src/Exceptions/QueryBuilderInvalidInputException.php

@ -0,0 +1,17 @@
<?php
namespace Darksparrow\DeegraphPHP\Exceptions;
use Exception;
class QueryBuilderInvalidInputException extends Exception
{
public function __construct(
string $message = "The value you have entered doesn't pass regex validation.",
int $code = 422
) {
parent::__construct($message, $code);
$this->message = "$message";
$this->code = $code;
}
}

17
src/Exceptions/QueryBuilderRequiredFieldIsNotSetException.php

@ -0,0 +1,17 @@
<?php
namespace Darksparrow\DeegraphPHP\Exceptions;
use Exception;
class QueryBuilderRequiredFieldIsNotSetException extends Exception
{
public function __construct(
string $message = "A field that is required for this operation has not been set.",
int $code = 422
) {
parent::__construct($message, $code);
$this->message = "$message";
$this->code = $code;
}
}

15
src/QueryBuilder/QueryBuilder.php

@ -0,0 +1,15 @@
<?php
namespace Darksparrow\DeegraphPHP\QueryBuilder;
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Insert;
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Put;
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Select;
class QueryBuilder
{
public function Put(): Put
{
return new Put();
}
}

23
src/QueryBuilder/QueryBuilderTrait.php

@ -0,0 +1,23 @@
<?php
namespace Darksparrow\DeegraphPHP\QueryBuilder;
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderException;
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderInvalidInputException;
trait QueryBuilderTrait
{
protected function Validate(string $target, string $pattern): string
{
if (!preg_match(pattern: $pattern, subject: $target))
throw new QueryBuilderInvalidInputException();
return $target;
}
protected function EnsureNotSet(string $target): void
{
if ($target != "")
throw new QueryBuilderConflictingFieldAlreadyExistsException();
}
}

113
src/QueryBuilder/QueryBuilders/Put.php

@ -0,0 +1,113 @@
<?php
namespace Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders;
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderInvalidInputException;
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderRequiredFieldIsNotSetException;
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilderTrait;
final class Put
{
use QueryBuilderTrait;
protected string $PutWhat = "";
protected string $PutAt = "";
protected string $PutInto = "";
protected bool $Safe = false;
public function __construct()
{
}
/**
* @throws QueryBuilderRequiredFieldIsNotSetException
*/
public function __toString(): string
{
$builder = "PUT";
if($this->PutWhat != "") $builder .= " $this->PutWhat";
if($this->PutAt != "") $builder .= " $this->PutAt";
elseif ($this->PutInto != "") $builder .= " $this->PutInto";
else throw new QueryBuilderRequiredFieldIsNotSetException();
if($this->Safe) $builder .= " SAFE";
return $builder;
}
/**
* @throws QueryBuilderInvalidInputException
*/
public function Schema(string $uri): Put
{
$this->PutWhat = self::Validate(
target: "SCHEMA \"$uri\"",
pattern: "/SCHEMA \".+\"/"
);
return $this;
}
/**
* @throws QueryBuilderInvalidInputException
*/
public function URI(string $uri): Put
{
$this->PutWhat = self::Validate(
target: "URI \"$uri\"",
pattern: "/URI \".+\"/"
);
return $this;
}
/**
* @throws QueryBuilderInvalidInputException
*/
public function DataURI(string $mimeType, string $data): Put
{
$this->PutWhat = self::Validate(
target: "URI \"data:$mimeType;$data\"",
pattern: "/URI \"data:[a-zA-Z0-9]/[a-zA-Z0-9];.+\"/"
);
return $this;
}
/**
* @throws QueryBuilderInvalidInputException
* @throws QueryBuilderConflictingFieldAlreadyExistsException
*/
public function At(string $node, string $uwu): Put
{
self::EnsureNotSet($this->PutInto);
$this->PutAt = self::Validate(
target: 'AT {' . $node . '}/' . $uwu,
pattern: "/AT {[a-zA-Z0-9\-]+}\/[a-zA-Z0-9]+/"
);
return $this;
}
/**
* @throws QueryBuilderInvalidInputException
* @throws QueryBuilderConflictingFieldAlreadyExistsException
*/
public function Into(string $relativePath, string $propertyName): Put
{
self::EnsureNotSet($this->PutAt);
$this->PutInto = self::Validate(
target: "INTO \"$relativePath\" AS \"$propertyName\"",
pattern: "/INTO \"[a-zA-Z0-9\-]+\" AS \"[a-zA-Z0-9]+\"/"
);
return $this;
}
public function Safe(): Put
{
$this->Safe = true;
return $this;
}
}
Loading…
Cancel
Save