diff --git a/src/Exceptions/QueryBuilderInvalidDeegraphPathException.php b/src/Exceptions/QueryBuilderInvalidDeegraphPathException.php new file mode 100644 index 0000000..f2f8b06 --- /dev/null +++ b/src/Exceptions/QueryBuilderInvalidDeegraphPathException.php @@ -0,0 +1,17 @@ +message = "$message"; + $this->code = $code; + } +} diff --git a/src/QueryBuilder/QueryBuilderTrait.php b/src/QueryBuilder/QueryBuilderTrait.php index 65d3d72..8ce8d2b 100755 --- a/src/QueryBuilder/QueryBuilderTrait.php +++ b/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; + } } diff --git a/src/QueryBuilder/QueryBuilders/InsertQuery.php b/src/QueryBuilder/QueryBuilders/InsertQuery.php index 713f64c..17ea339 100755 --- a/src/QueryBuilder/QueryBuilders/InsertQuery.php +++ b/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; } diff --git a/src/QueryBuilder/QueryBuilders/PutQuery.php b/src/QueryBuilder/QueryBuilders/PutQuery.php index 2e50dbe..0206c05 100755 --- a/src/QueryBuilder/QueryBuilders/PutQuery.php +++ b/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]+\"/" );