Files
PdoWrapper/src/Expression.php
2025-01-09 22:54:48 +01:00

119 lines
2.6 KiB
PHP

<?php
namespace Kletellier\PdoWrapper;
class Expression
{
private string $condition;
private string $operator;
private mixed $value;
public function __construct($condition, $operator, $value)
{
$this->condition = $condition;
$this->operator = $operator;
$this->value = $value;
}
/**
* Return raw value of the expression
*
* @return string
*/
public function raw(): string
{
$operator = trim(strtolower($this->operator));
if ($operator == "in") {
$values = implode(",", str_split(str_repeat("?", count($this->value))));
return "(" . $this->condition . " " . $this->operator . " (" . $values . ") )";
}
if ($operator == "between" && count($this->value) == 2) {
return "(" . $this->condition . " " . $this->operator . " ? AND ? )";
}
if ($operator == "notnull") {
return "(" . $this->condition . " IS NOT NULL )";
}
if ($operator == "isnull") {
return "(" . $this->condition . " IS NULL )";
}
return "(" . $this->condition . " " . $this->operator . " ? )";
}
/**
* Return if the condition need to evaluate data
*
* @return boolean
*/
public function hasData()
{
$operator = trim(strtolower($this->operator));
return (in_array($operator, ["isnull","notnull"])) ? false : true;
}
/**
* Return actual condition
*
* @return string
*/
public function getCondition(): string
{
return $this->condition;
}
/**
* Set condition
*
* @param string $condition
* @return self
*/
public function setCondition(string $condition): self
{
$this->condition = $condition;
return $this;
}
/**
* Get actual operator
*
* @return string
*/
public function getOperator(): string
{
return $this->operator;
}
/**
* Set operator
*
* @param string $operator
* @return self
*/
public function setOperator(string $operator): self
{
$this->operator = $operator;
return $this;
}
/**
* Get the value of the expression
*
* @return mixed
*/
public function getValue(): mixed
{
return $this->value;
}
/**
* Set the value of the expression
*
* @param mixed $value
* @return void
*/
public function setValue(mixed $value)
{
$this->value = $value;
return $this;
}
}