| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\QueryBuilder\Clauses; |
| 4 |
|
| 5 |
use Give\Framework\QueryBuilder\Types\Operator; |
| 6 |
use InvalidArgumentException; |
| 7 |
|
| 8 |
/** |
| 9 |
* @since 2.19.0 |
| 10 |
*/ |
| 11 |
class Where |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
public $column; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var mixed |
| 20 |
*/ |
| 21 |
public $value; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $comparisonOperator; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $logicalOperator; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string|null |
| 35 |
*/ |
| 36 |
public $type; |
| 37 |
|
| 38 |
/** |
| 39 |
* @param string $column |
| 40 |
* @param string $value |
| 41 |
* @param string $comparisonOperator |
| 42 |
* @param string|null $logicalOperator |
| 43 |
*/ |
| 44 |
public function __construct($column, $value, $comparisonOperator, $logicalOperator) |
| 45 |
{ |
| 46 |
$this->column = trim($column); |
| 47 |
$this->value = $value; |
| 48 |
$this->comparisonOperator = $this->getComparisonOperator($comparisonOperator); |
| 49 |
$this->logicalOperator = $logicalOperator ? $this->getLogicalOperator($logicalOperator) : ''; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param string $comparisonOperator |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
private function getComparisonOperator($comparisonOperator) |
| 58 |
{ |
| 59 |
$operators = [ |
| 60 |
'<', |
| 61 |
'<=', |
| 62 |
'>', |
| 63 |
'>=', |
| 64 |
'<>', |
| 65 |
'!=', |
| 66 |
'=', |
| 67 |
Operator::LIKE, |
| 68 |
Operator::NOTLIKE, |
| 69 |
Operator::IN, |
| 70 |
Operator::NOTIN, |
| 71 |
Operator::BETWEEN, |
| 72 |
Operator::NOTBETWEEN, |
| 73 |
Operator::ISNULL, |
| 74 |
Operator::NOTNULL |
| 75 |
]; |
| 76 |
|
| 77 |
if (!in_array($comparisonOperator, $operators, true)) { |
| 78 |
throw new InvalidArgumentException( |
| 79 |
sprintf( |
| 80 |
'Unsupported comparison operator %s. Please use one of the supported operators (%s)', |
| 81 |
$comparisonOperator, |
| 82 |
implode(',', $operators) |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
return $comparisonOperator; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @param string $logicalOperator |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
private function getLogicalOperator($logicalOperator) |
| 96 |
{ |
| 97 |
$operators = [ |
| 98 |
Operator::_AND, |
| 99 |
Operator::_OR |
| 100 |
]; |
| 101 |
|
| 102 |
$logicalOperator = strtoupper($logicalOperator); |
| 103 |
|
| 104 |
if (!in_array($logicalOperator, $operators, true)) { |
| 105 |
throw new InvalidArgumentException( |
| 106 |
sprintf( |
| 107 |
'Unsupported logical operator %s. Please use one of the supported operators (%s)', |
| 108 |
$logicalOperator, |
| 109 |
implode(',', $operators) |
| 110 |
) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
return $logicalOperator; |
| 115 |
} |
| 116 |
} |
| 117 |
|