PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.32
Booking for Appointments and Events Calendar – Amelia v1.2.32
2.4.5 2.4.4 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 1 year ago
Statement.php
142 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(
94 $paramsKeys,
95 function ($a, $b) {
96 return strlen($b) - strlen($a);
97 }
98 );
99
100 ksort($paramsValues);
101 ksort($paramsTypes);
102
103 $referencedQueryParams = [];
104
105 foreach ($paramsValues as $key => &$value) {
106 $referencedQueryParams[$key] = &$value;
107 }
108
109 $parsedQuery = str_replace($paramsKeys, '?', $this->query->getValue());
110
111 if ($stmt = $this->mysqli->prepare($parsedQuery)) {
112 if ($referencedQueryParams) {
113 call_user_func_array(
114 array($stmt, 'bind_param'),
115 array_merge([str_replace('N', 'i', implode('', $paramsTypes))], $referencedQueryParams)
116 );
117 }
118
119 $success = $stmt->execute();
120
121 $this->result->setValue($stmt->get_result());
122 } else {
123 throw new \Exception();
124 }
125
126 $this->params = [];
127
128 return $stmt && $success;
129 }
130
131 /**
132 * @param string $key
133 * @param string $value
134 *
135 * @return mixed
136 */
137 public function bindParam($key, $value)
138 {
139 $this->params[$key] = $value;
140 }
141 }
142