You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.1 KiB

<?php
namespace Darksparrow\DeegraphInteractions\Traits;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderInvalidInputException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\DeleteQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\GrantQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQueryBuilder;
use Exception;
use ReflectionClass;
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
{
if(!preg_match(pattern: $pattern, subject: $target))
throw new QueryBuilderInvalidInputException();
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
{
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;
}
public function ValidateValues(
DeleteQueryBuilder
|GrantQueryBuilder
|InsertQueryBuilder
|PutQueryBuilder
|SelectQueryBuilder $target
): void
{
$nameBase = "Darksparrow\\DeegraphPHP\\Attributes";
$reflection = new ReflectionClass($target);
$properties = $reflection->getProperties();
foreach($properties as $property)
{
$attributes = $property->getAttributes();
$propertyName = $property->getName();
$propertyValue = $property->getValue($target);
if(sizeof($attributes) == 0) continue;
foreach($attributes as $attribute)
{
if($attribute->getName() == "$nameBase\\QueryBuilderRequiredField")
{
if($propertyValue == "")
throw new Exception();
}
}
}
}
}