1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Component\Db; |
4
|
|
|
|
5
|
|
|
use App\Component\Model\Orm\Orm; |
6
|
|
|
use App\Component\Db\Factory; |
7
|
|
|
|
8
|
|
|
class Core |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* connection |
12
|
|
|
* |
13
|
|
|
* @var \PDO | boolean |
14
|
|
|
*/ |
15
|
|
|
protected $connection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* logger |
19
|
|
|
* |
20
|
|
|
* @var \Monolog\Logger |
21
|
|
|
*/ |
22
|
|
|
protected $logger; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* sql |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $sql; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* statement |
33
|
|
|
* |
34
|
|
|
* @var \PDOStatement |
35
|
|
|
*/ |
36
|
|
|
protected $statement; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* fetch mode |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $fetchMode = \PDO::FETCH_ASSOC; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* database name |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $database; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* rowset result |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $rowset; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* instanciate |
61
|
|
|
*/ |
62
|
5 |
|
public function __construct() |
63
|
|
|
{ |
64
|
5 |
|
$this->rowset = []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* set connection from Orm instance |
69
|
|
|
* |
70
|
|
|
* @param Orm $ormInstance |
71
|
|
|
* @return Core |
72
|
|
|
*/ |
73
|
2 |
|
public function fromOrm(Orm &$ormInstance): Core |
74
|
|
|
{ |
75
|
2 |
|
$this->database = $ormInstance->getDatabase(); |
76
|
2 |
|
$container = $ormInstance->getContainer(); |
77
|
2 |
|
$this->logger = $container->getService(\Monolog\Logger::class); |
78
|
2 |
|
$factory = new Factory($container); |
79
|
2 |
|
$this->connection = $factory->getConnection( |
80
|
2 |
|
$ormInstance->getSlot(), |
81
|
2 |
|
$this->database |
82
|
|
|
); |
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* run query with bind values and types |
88
|
|
|
* |
89
|
|
|
* @param string $sql |
90
|
|
|
* @param array $bindParams |
91
|
|
|
* @param array $bindTypes |
92
|
|
|
* @return Core |
93
|
|
|
*/ |
94
|
1 |
|
public function run( |
95
|
|
|
string $sql, |
96
|
|
|
array $bindParams = [], |
97
|
|
|
array $bindTypes = [] |
98
|
|
|
): Core { |
99
|
1 |
|
$this->sql = $sql; |
100
|
|
|
try { |
101
|
1 |
|
$this->statement = $this->connection->prepare($sql); |
|
|
|
|
102
|
1 |
|
if ($this->statement instanceof \PDOStatement) { |
103
|
1 |
|
$this->statement->setFetchMode($this->fetchMode); |
104
|
1 |
|
if (!empty($bindParams)) { |
105
|
1 |
|
$this->bindArray( |
106
|
1 |
|
$this->statement, |
107
|
1 |
|
$bindParams, |
108
|
1 |
|
$bindTypes |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} catch (\PDOException $exc) { |
113
|
|
|
$this->logger->alert('Prepare failed'); |
114
|
|
|
$this->logger->alert($exc->getMessage()); |
115
|
|
|
} |
116
|
|
|
try { |
117
|
1 |
|
$this->statement->execute(); |
118
|
1 |
|
} catch (\PDOException $exc) { |
119
|
1 |
|
$this->logger->alert('Execute failed'); |
120
|
1 |
|
$this->logger->alert($exc->getMessage()); |
121
|
|
|
} |
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* hydrate |
127
|
|
|
* |
128
|
|
|
* @return Core |
129
|
|
|
*/ |
130
|
1 |
|
public function hydrate(): Core |
131
|
|
|
{ |
132
|
1 |
|
if ($this->statement instanceof \PDOStatement) { |
|
|
|
|
133
|
|
|
$this->rowset = $this->statement->fetchAll($this->fetchMode); |
134
|
|
|
$this->statement->closeCursor(); |
135
|
|
|
} |
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* return run result |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
1 |
|
public function getRowset(): array |
145
|
|
|
{ |
146
|
1 |
|
return $this->rowset; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* bindArray |
151
|
|
|
* |
152
|
|
|
* @param \PDOStatement $poStatement |
153
|
|
|
* @param array $paArray |
154
|
|
|
* @param array $forcedTypes |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
1 |
|
public function bindArray(\PDOStatement &$poStatement, array &$paArray, array $forcedTypes = []) |
158
|
|
|
{ |
159
|
1 |
|
foreach ($paArray as $k => $v) { |
160
|
1 |
|
$type = (is_int($v)) ? \PDO::PARAM_INT : \PDO::PARAM_STR; |
161
|
|
|
|
162
|
1 |
|
if (isset($forcedTypes[$k])) { |
163
|
|
|
$type = $forcedTypes[$k]; |
164
|
|
|
$v = ($type == \PDO::PARAM_INT) ? (int) $v : $v; |
165
|
|
|
} |
166
|
1 |
|
$value = is_array($v) ? serialize($v) : $v; |
167
|
1 |
|
$key = $k; |
168
|
|
|
try { |
169
|
1 |
|
$poStatement->bindValue($key, $value, $type); |
170
|
|
|
} catch (\PDOException $exc) { |
171
|
|
|
$this->logger->alert( |
172
|
|
|
'Sql Bind Error [' . $key . ':' . $value . ':' . $type . ']' |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
} |
176
|
1 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.