Issues (5)

src/QueryCondition.php (2 issues)

Labels
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
namespace miBadger\Query;
10
11
/**
12
 *
13
 * @since 2.0.0
14
 */
15
class QueryCondition implements QueryExpression
16
{
17
	/* @var string The lhs of the query condition. */
18
	private $leftOperand;
19
20
	/* @var boolean|string|int|array The rhs of the query condition. */
21
	private $rightOperand;
22
23
	/* @var string The binary operator of this condition. */
24
	private $operator;
25
26
	/* @var string|array The binding(s) for the rhs of the condition */
27
	private $binding;
28
29
	/**
30
	 * Constructs a new Query condition for the supplied operator and operands
31
	 * @param mixed $left the lhs of the expression (Not escaped!)
32
	 * @param string $operator the SQL operator
33
	 * @param mixed $right the rhs of the expression (escaped)
34
	 */
35
	public function __construct($left, string $operator, $right)
36
	{
37
		$this->leftOperand = $left;
38
		$this->operator = $operator;
39
		$this->binding = null;
40
41
		switch (strtoupper($operator)) {
42
			case '<':
43
			case '<=':
44
			case '>=':
45
			case '>':
46
			case '=':
47
			case '<>':
48
			case 'LIKE':
49
			case 'NOT LIKE':
50
			case 'IS':
51
			case 'IS NOT':
52
				if (is_array($right)) {
53
					throw new QueryException("Array Unsupported for this operand type");
54
				}
55
				$this->rightOperand = $right;
56
				break;
57
58
			case 'NOT IN':
59
			case 'IN':
60
				if (is_string($right)) {
61
					$this->rightOperand = explode(', ', $right);
62
				} else {
63
					$this->rightOperand = $right;
64
				}
65
				break;
66
67
			default:
68
				throw new QueryException(sprintf("Unsupported operator \"%s\"", $operator));
69
		}
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75
	public function getFlattenedConditions()
76
	{
77
		return [$this];
78
	}
79
80
	/**
81
	 * Adds a binding to the supplied query for these query condition parameters
82
	 * @param miBadger\Query\Query $query the Query to bind to
0 ignored issues
show
The type miBadger\Query\miBadger\Query\Query was not found. Did you mean miBadger\Query\Query? If so, make sure to prefix the type with \.
Loading history...
83
	 * @param string $bindingClause The clause to bind to (WHERE, HAVING)
84
	 */
85
	public function bind(Query $query, string $bindingClause)
86
	{
87
		if (is_array($this->rightOperand)) {
88
			$this->binding = $query->addBindings($bindingClause, $this->rightOperand);
89
		} else {
90
			$this->binding = $query->addBinding($bindingClause, $this->rightOperand);
91
		}
92
	}
93
94
95
	public function clearBinding()
96
	{
97
		$this->binding = null;
98
	}
99
100
	/**
101
	 * {@inheritdoc}
102
	 */
103
	public function __toString()
104
	{
105
		if ($this->binding === null) {
106
			$rhs = $this->rightOperand;
107
		} else {
108
			$rhs = $this->binding;
109
		}
110
111
		if (is_array($rhs)) {
112
			$rhs = sprintf('(%s)', implode(', ', $rhs));
113
		}
114
115
		return sprintf('%s %s %s', $this->leftOperand, $this->operator, $rhs);
0 ignored issues
show
It seems like $rhs can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
		return sprintf('%s %s %s', $this->leftOperand, $this->operator, /** @scrutinizer ignore-type */ $rhs);
Loading history...
116
	}
117
}
118