|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\sql\framework; |
|
4
|
|
|
|
|
5
|
|
|
use UnexpectedValueException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class represents an as-of-yet unprepared SQL statement. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Statement implements Executable |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $sql; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array map where placeholder name maps to a scalar value, or array of scalar values |
|
19
|
|
|
*/ |
|
20
|
|
|
private $params = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $sql SQL statement (with placeholders) |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function __construct($sql) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->sql = $sql; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Bind an individual placeholder name to a given value. |
|
32
|
|
|
* |
|
33
|
|
|
* Acceptable value types are scalar types (string, int, float, bool, null) and arrays of scalar values. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $name placeholder name |
|
36
|
|
|
* @param array|string|int|bool|null $value value to bind |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function bind($name, $value) |
|
41
|
|
|
{ |
|
42
|
1 |
|
static $SCALAR_TYPES = [ |
|
43
|
|
|
'integer' => true, |
|
44
|
|
|
'double' => true, |
|
45
|
|
|
'string' => true, |
|
46
|
|
|
'boolean' => true, |
|
47
|
|
|
'NULL' => true, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
1 |
|
$value_type = gettype($value); |
|
51
|
|
|
|
|
52
|
1 |
|
if ($value_type === 'array') { |
|
53
|
1 |
|
foreach ($value as $item) { |
|
|
|
|
|
|
54
|
1 |
|
$item_type = gettype($item); |
|
55
|
|
|
|
|
56
|
1 |
|
if (! isset($SCALAR_TYPES[$item_type])) { |
|
57
|
1 |
|
throw new UnexpectedValueException("unexpected item type in array: {$item_type}"); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
1 |
|
if (! isset($SCALAR_TYPES[$value_type])) { |
|
62
|
1 |
|
throw new UnexpectedValueException("unexpected value type: {$value_type}"); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$this->params[$name] = $value; |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Applies a set of placeholder name/value pairs and binds them to individual placeholders. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $params placeholder name/value pairs |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function apply(array $params) |
|
77
|
|
|
{ |
|
78
|
1 |
|
foreach ($params as $name => $value) { |
|
79
|
1 |
|
$this->bind($name, $value); |
|
80
|
|
|
} |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getSQL() |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->sql; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritdoc |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getParams() |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->params; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.