PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / DB / MySQLi / Statement.php
ameliabooking / src / Infrastructure / DB / MySQLi Last commit date
Connection.php 2 years ago Query.php 7 years ago Result.php 7 years ago Statement.php 4 years ago
Statement.php
139 lines
1 <?php
2
3 namespace AmeliaBooking\Infrastructure\DB\MySQLi;
4
5 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
6 use mysqli;
7
8 /**
9 * Class Statement
10 *
11 * @package AmeliaBooking\Infrastructure\DB\MySQLi
12 */
13 class Statement
14 {
15 /** @var mysqli $mysqli */
16 private $mysqli;
17
18 /** @var Query $query */
19 private $query;
20
21 /** @var Result $result */
22 private $result;
23
24 /** @var array $params */
25 private $params = [];
26
27 /**
28 * @param mysqli $mysqli
29 * @param Result $result
30 * @param Query $query
31 */
32 public function __construct($mysqli, $result, $query)
33 {
34 $this->mysqli = $mysqli;
35 $this->result = $result;
36 $this->query = $query;
37 }
38
39 /**
40 *
41 * @return mixed
42 */
43 public function fetch()
44 {
45 return $this->result->getValue()->fetch_assoc();
46 }
47
48 /**
49 *
50 * @return mixed
51 */
52 public function fetchAll()
53 {
54 $rows = [];
55
56 while ($row = $this->result->getValue()->fetch_assoc()) {
57 $rows[] = $row;
58 }
59
60 return $rows;
61 }
62 /**
63 *
64 * @return mixed
65 */
66 public function rowCount()
67 {
68 return $this->result->getValue()->num_rows;
69 }
70
71
72 /**
73 * @param array $params
74 *
75 * @return mixed
76 */
77 public function execute($params = [])
78 {
79 $this->params = array_merge($this->params, $params);
80
81 $paramsKeys = [];
82 $paramsValues = [];
83 $paramsTypes = [];
84
85 foreach ($this->params as $key => $value) {
86 $index = strpos($this->query->getValue(), $key);
87
88 $paramsKeys[$index] = $key;
89 $paramsValues[$index] = $value;
90 $paramsTypes[$index] = gettype($this->params[$key])[0];
91 }
92
93 usort($paramsKeys, function ($a, $b) {
94 return strlen($b) - strlen($a);
95 });
96
97 ksort($paramsValues);
98 ksort($paramsTypes);
99
100 $referencedQueryParams = [];
101
102 foreach ($paramsValues as $key => &$value) {
103 $referencedQueryParams[$key] = &$value;
104 }
105
106 $parsedQuery = str_replace($paramsKeys, '?', $this->query->getValue());
107
108 if ($stmt = $this->mysqli->prepare($parsedQuery)) {
109 if ($referencedQueryParams) {
110 call_user_func_array(
111 array($stmt, 'bind_param'),
112 array_merge([str_replace('N', 'i', implode('', $paramsTypes))], $referencedQueryParams)
113 );
114 }
115
116 $success = $stmt->execute();
117
118 $this->result->setValue($stmt->get_result());
119 } else {
120 throw new \Exception();
121 }
122
123 $this->params = [];
124
125 return $stmt && $success;
126 }
127
128 /**
129 * @param string $key
130 * @param string $value
131 *
132 * @return mixed
133 */
134 public function bindParam($key, $value)
135 {
136 $this->params[$key] = $value;
137 }
138 }
139