Browse Source

will now verify Deegraph paths with regex

(regex nicked off alex in the office over discord lol)
pull/1/head
Cerys Lewis 5 months ago
parent
commit
1b6cf141f4
  1. 17
      src/Exceptions/QueryBuilderInvalidDeegraphPathException.php
  2. 9
      src/QueryBuilder/QueryBuilderTrait.php
  3. 1
      src/QueryBuilder/QueryBuilders/InsertQuery.php
  4. 10
      src/QueryBuilder/QueryBuilders/PutQuery.php

17
src/Exceptions/QueryBuilderInvalidDeegraphPathException.php

@ -0,0 +1,17 @@
<?php
namespace Darksparrow\DeegraphPHP\Exceptions;
use Exception;
class QueryBuilderInvalidDeegraphPathException extends Exception
{
public function __construct(
string $message = "The Deegraph path you have entered is not valid.",
int $code = 422
) {
parent::__construct($message, $code);
$this->message = "$message";
$this->code = $code;
}
}

9
src/QueryBuilder/QueryBuilderTrait.php

@ -8,7 +8,7 @@ use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderInvalidInputException;
trait QueryBuilderTrait
{
protected function Validate(string $target, string $pattern): string
protected function RegexValidate(string $target, string $pattern): string
{
if (!preg_match(pattern: $pattern, subject: $target))
throw new QueryBuilderInvalidInputException();
@ -20,4 +20,11 @@ trait QueryBuilderTrait
if ($target != "")
throw new QueryBuilderConflictingFieldAlreadyExistsException();
}
protected function ValidateDeegraphPath(string $target): string
{
if (!preg_match(pattern: "(^(\{[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}\})$)|(^\{[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}\}(\/([a-z][a-z0-9]*|[0-9]+|#|\*))+$)|(^(([a-z][a-z0-9]*|[0-9]+|#|\*))(\/([a-z][a-z0-9]*|[0-9]+|#|\*))*$)", subject: $target))
throw new QueryBuilderInvalidInputException();
return $target;
}
}

1
src/QueryBuilder/QueryBuilders/InsertQuery.php

@ -47,6 +47,7 @@ final class InsertQuery
public function RelativePath(string $relativePath): InsertQuery
{
self::ValidateDeegraphPath(target: $relativePath);
$this->RelativePath = $relativePath;
return $this;
}

10
src/QueryBuilder/QueryBuilders/PutQuery.php

@ -44,7 +44,7 @@ final class PutQuery
*/
public function Schema(string $uri): PutQuery
{
$this->PutWhat = self::Validate(
$this->PutWhat = self::RegexValidate(
target: "SCHEMA \"$uri\"",
pattern: "/SCHEMA \".+\"/"
);
@ -56,7 +56,7 @@ final class PutQuery
*/
public function URI(string $uri): PutQuery
{
$this->PutWhat = self::Validate(
$this->PutWhat = self::RegexValidate(
target: "URI \"$uri\"",
pattern: "/URI \".+\"/"
);
@ -68,7 +68,7 @@ final class PutQuery
*/
public function DataURI(string $mimeType, string $data): PutQuery
{
$this->PutWhat = self::Validate(
$this->PutWhat = self::RegexValidate(
target: "URI \"data:$mimeType;$data\"",
pattern: "/URI \"data:[a-zA-Z0-9]/[a-zA-Z0-9];.+\"/"
);
@ -83,7 +83,7 @@ final class PutQuery
public function At(string $node, string $uwu): PutQuery
{
self::EnsureNotSet($this->PutInto);
$this->PutAt = self::Validate(
$this->PutAt = self::RegexValidate(
target: 'AT {' . $node . '}/' . $uwu,
pattern: "/AT {[a-zA-Z0-9\-]+}\/[a-zA-Z0-9]+/"
);
@ -98,7 +98,7 @@ final class PutQuery
public function Into(string $relativePath, string $propertyName): PutQuery
{
self::EnsureNotSet($this->PutAt);
$this->PutInto = self::Validate(
$this->PutInto = self::RegexValidate(
target: "INTO \"$relativePath\" AS \"$propertyName\"",
pattern: "/INTO \"[a-zA-Z0-9\-]+\" AS \"[a-zA-Z0-9]+\"/"
);

Loading…
Cancel
Save