Commit 4b599d32 authored by François Agneray's avatar François Agneray
Browse files

Verification du type

parent 8f76f65a
......@@ -21,6 +21,8 @@ class Between extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value1, string $value2)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value1);
$this->verifyTypeCompatibility($value2);
$this->value1 = $value1;
$this->value2 = $value2;
}
......
......@@ -19,6 +19,7 @@ class Equal extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,7 @@ class GreaterThan extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,7 @@ class GreaterThanEqual extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,9 @@ class In extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, array $values)
{
parent::__construct($expr, $column, $columnType);
foreach ($values as $value) {
$this->verifyTypeCompatibility($value);
}
$this->values = $values;
}
......
......@@ -19,6 +19,7 @@ class LessThan extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,7 @@ class LessThanEqual extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,7 @@ class Like extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,7 @@ class NotEqual extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -19,6 +19,9 @@ class NotIn extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, array $values)
{
parent::__construct($expr, $column, $columnType);
foreach ($values as $value) {
$this->verifyTypeCompatibility($value);
}
$this->values = $values;
}
......
......@@ -19,6 +19,7 @@ class NotLike extends Operator
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value);
$this->value = $value;
}
......
......@@ -12,6 +12,7 @@ namespace App\Utils\Operator;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Types\Type;
use is_numeric;
abstract class Operator implements IOperator
{
......@@ -37,15 +38,33 @@ abstract class Operator implements IOperator
$this->columnType = $columnType;
}
protected function verifyTypeCompatibility(string $value): void
{
if ($this->isColumnTypeNumeric() && !is_numeric($value)) {
throw OperatorException::valueIsNotNumeric($value);
}
}
protected function getSqlValue(string $value): string
{
if ($this->isColumnTypeNumeric()) {
return $value;
} else {
return $this->expr->literal($value);
}
}
protected function isColumnTypeNumeric(): bool
{
switch ($this->columnType) {
case Type::STRING:
case Type::TEXT:
case Type::DATE:
return $this->expr->literal($value);
case Type::BIGINT:
case Type::DECIMAL:
case Type::INTEGER:
case Type::SMALLINT:
case Type::FLOAT:
return true;
default:
return $value;
return false;
}
}
}
......@@ -33,4 +33,9 @@ class OperatorException extends SearchException
{
return new self("Operator '" . $operator . "' needs at least 1 parameter to work");
}
public static function valueIsNotNumeric($value)
{
return new self("You passed a string value '" . $value . "' or the criterion value required a numeric value");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment