Skip to content

Commit

Permalink
Cleaned up ValidatesExpressions.php
Browse files Browse the repository at this point in the history
  • Loading branch information
LaravelFreelancerNL committed Sep 21, 2021
1 parent 64f2733 commit b4f91d5
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 124 deletions.
126 changes: 2 additions & 124 deletions src/Traits/ValidatesExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,8 @@

trait ValidatesExpressions
{
/**
* @param mixed $value
*
* @return bool
*/
public function isBind($value): bool
{
if (is_string($value)) {
return true;
}
if (is_object($value)) {
return true;
}

return false;
}

/**
* @param mixed $value
*
* @return bool
*/
public function isCollectionBind($value): bool
{
if (is_string($value)) {
return true;
}

return false;
}
use ValidatesOperators;
use ValidatesReferences;

/**
* @param mixed $value
Expand Down Expand Up @@ -110,33 +82,6 @@ public function isFunction($value): bool
return $value instanceof FunctionExpression;
}

/**
* @param mixed $operator
* @return bool
*/
public function isLogicalOperator($operator): bool
{
return isset($this->logicalOperators[strtoupper($operator)]);
}

/**
* @param mixed $operator
* @return bool
*/
public function isComparisonOperator($operator): bool
{
return isset($this->comparisonOperators[strtoupper($operator)]);
}

/**
* @param mixed $operator
* @return bool
*/
public function isArithmeticOperator($operator): bool
{
return isset($this->arithmeticOperators[$operator]);
}

/**
* @param mixed $value
* @return bool
Expand Down Expand Up @@ -218,73 +163,6 @@ public function isId($value): bool
return false;
}

/**
* @param mixed $value
*
* @return bool
*/
public function isVariable($value): bool
{
if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
return true;
}

return false;
}

/**
* @param mixed $value
* @param array $registeredVariables
* @return bool
*/
public function isRegisteredVariable($value, $registeredVariables = []): bool
{
return isset($registeredVariables[$value]);
}

/**
* @param mixed $value
*
* @return bool
*/
public function isAttribute($value): bool
{
$pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
. '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
if (
is_string($value) &&
preg_match($pattern, $value)
) {
return true;
}

return false;
}

/**
* @param mixed $value
* @param array $registeredVariables
* @return bool
*/
public function isReference($value, $registeredVariables = []): bool
{
$variables = '';
if (!empty($registeredVariables)) {
$variables = implode('|', $registeredVariables);
}

if (! is_string($value) || empty($value)) {
return false;
}

return (bool) preg_match(
'/^('
. $variables
. '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
$value
);
}

/**
* @param mixed $value
*
Expand Down
33 changes: 33 additions & 0 deletions src/Traits/ValidatesOperators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LaravelFreelancerNL\FluentAQL\Traits;

trait ValidatesOperators
{
/**
* @param mixed $operator
* @return bool
*/
public function isLogicalOperator($operator): bool
{
return isset($this->logicalOperators[strtoupper($operator)]);
}

/**
* @param mixed $operator
* @return bool
*/
public function isComparisonOperator($operator): bool
{
return isset($this->comparisonOperators[strtoupper($operator)]);
}

/**
* @param mixed $operator
* @return bool
*/
public function isArithmeticOperator($operator): bool
{
return isset($this->arithmeticOperators[$operator]);
}
}
104 changes: 104 additions & 0 deletions src/Traits/ValidatesReferences.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace LaravelFreelancerNL\FluentAQL\Traits;

trait ValidatesReferences
{
/**
* @param mixed $value
*
* @return bool
*/
public function isBind($value): bool
{
if (is_string($value)) {
return true;
}
if (is_object($value)) {
return true;
}

return false;
}

/**
* @param mixed $value
*
* @return bool
*/
public function isCollectionBind($value): bool
{
if (is_string($value)) {
return true;
}

return false;
}

/**
* @param mixed $value
*
* @return bool
*/
public function isVariable($value): bool
{
if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
return true;
}

return false;
}

/**
* @param mixed $value
* @param array $registeredVariables
* @return bool
*/
public function isRegisteredVariable($value, $registeredVariables = []): bool
{
return isset($registeredVariables[$value]);
}

/**
* @param mixed $value
*
* @return bool
*/
public function isAttribute($value): bool
{
$pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
. '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
if (
is_string($value) &&
preg_match($pattern, $value)
) {
return true;
}

return false;
}

/**
* @param mixed $value
* @param array $registeredVariables
* @return bool
*/
public function isReference($value, $registeredVariables = []): bool
{
$variables = '';
if (!empty($registeredVariables)) {
$variables = implode('|', $registeredVariables);
}

if (! is_string($value) || empty($value)) {
return false;
}

return (bool) preg_match(
'/^('
. $variables
. '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
$value
);
}
}

0 comments on commit b4f91d5

Please sign in to comment.