Passed
Push — v2 ( 0821e6...8b5ae0 )
by Berend
03:14
created

QueryCondition::__toString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 12
rs 10
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 17
26
	/* @var string|Array The binding(s) for the rhs of the condition */
27 17
	private $binding;
28 17
29 17
	/**
30
	 * Constructs a new Query condition for the supplied operator and operands
31
	 * @param mixed $left the lhs of the expression (Not escaped!)
32 17
	 * @param string $operator the SQL operator
33 17
	 * @param mixed $right the rhs of the expression (escaped)
34 17
	 */
35 11
	public function __construct($left, string $operator, $right)
36 11
	{
37 5
		$this->leftOperand = $left;
38 4
		$this->operator = $operator;
39 4
		$this->binding = null;
40 14
41 14
		switch (strtoupper($operator)) {
42
			case '<':
43 3
			case '<=':
44 3
			case '>=':
45 3
			case '>':
46 1
			case '=':
47
			case '<>':
48 2
			case 'LIKE':
49
			case 'NOT LIKE':
50 3
			case 'IS':
51
			case 'IS NOT':
52
				if (is_array($right)) {
53
					throw new QueryException("Array Unsupported for this operand type");
54
				}
55 17
				$this->rightOperand = $right;
56
				break;
57
58
			case 'NOT IN':
59
			case 'IN':
60 7
				if (is_string($right)) {
61
					$this->rightOperand = explode(', ', $right);
62 7
				} else {
63
					$this->rightOperand = $right;
64
				}
65 7
				break;
66
67 7
			default:
68 1
				throw new QueryException(sprintf("Unsupported operator \"%s\"", $operator));
69
		}
70 6
	}
71
72 7
	/**
73
	 * {@inheritdoc}
74
	 */
75
	public function getFlattenedConditions()
76
	{
77 17
		return [$this];
78
	}
79 17
80 10
	/**
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
Bug introduced by
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 7
	 */
84
	public function bind(Query $query, $bindingClause = 'where')
85
	{
86 17
		if (is_array($this->rightOperand)) {
87 3
			$this->binding = $query->addBindings($bindingClause, $this->rightOperand);
88
		} else {
89
			$this->binding = $query->addBinding($bindingClause, $this->rightOperand);
90 17
		}
91
	}
92
93
94
	public function clearBinding()
95
	{
96
		$this->binding = null;
97
	}
98
99
	/**
100
	 * {@inheritdoc}
101
	 */
102
	public function __toString()
103
	{
104
		if ($this->binding === null) {
105
			$rhs = $this->rightOperand;
106
		} else {
107
			$rhs = $this->binding;
108
		}
109
110
		if (is_array($rhs)) {
111
			$rhs = sprintf('(%s)', implode(', ', $rhs));
112
		}
113
114
		return sprintf('%s %s %s', $this->leftOperand, $this->operator, $rhs);
0 ignored issues
show
Bug introduced by
It seems like $rhs can also be of type array; however, parameter $args of sprintf() does only seem to accept 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

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