1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace QB\Generic\Params; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use PDO; |
9
|
|
|
|
10
|
|
|
class Params |
11
|
|
|
{ |
12
|
|
|
public const ALL_STRING = 1; |
13
|
|
|
public const ALL_AUTO = 2; |
14
|
|
|
public const ALL_MANUAL = 4; |
15
|
|
|
|
16
|
|
|
protected int $paramHandle; |
17
|
|
|
|
18
|
|
|
protected bool $useNamedParams; |
19
|
|
|
|
20
|
|
|
/** @var array<int,array<int,mixed>> */ |
21
|
|
|
protected array $params = []; |
22
|
|
|
|
23
|
|
|
/** @var int Helps tracking the extensions done on the SQL originally received */ |
24
|
|
|
protected int $extendedBy = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Params constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $params |
30
|
|
|
* @param int $paramHandle |
31
|
|
|
*/ |
32
|
80 |
|
public function __construct(array $params = [], int $paramHandle = self::ALL_AUTO) |
33
|
|
|
{ |
34
|
80 |
|
$this->useNamedParams = !array_key_exists(0, $params); |
35
|
|
|
|
36
|
80 |
|
$this->validateParamHandle($paramHandle); |
37
|
76 |
|
$this->validateParamKeys(array_keys($params)); |
38
|
|
|
|
39
|
74 |
|
$this->paramHandle = $paramHandle; |
40
|
|
|
|
41
|
74 |
|
$this->bind($params, $paramHandle); |
42
|
74 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $paramHandle |
46
|
|
|
*/ |
47
|
80 |
|
protected function validateParamHandle(int $paramHandle): void |
48
|
|
|
{ |
49
|
80 |
|
if (!in_array($paramHandle, [self::ALL_STRING, self::ALL_AUTO, self::ALL_MANUAL])) { |
50
|
4 |
|
throw new InvalidArgumentException( |
51
|
4 |
|
sprintf('invalid param handle received: %d.', $paramHandle) |
52
|
|
|
); |
53
|
|
|
} |
54
|
76 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $paramKeys |
58
|
|
|
*/ |
59
|
76 |
|
protected function validateParamKeys(array $paramKeys): void |
60
|
|
|
{ |
61
|
76 |
|
if ($this->useNamedParams) { |
62
|
52 |
|
foreach ($paramKeys as $paramKey) { |
63
|
15 |
|
if (is_int($paramKey)) { |
64
|
2 |
|
throw new InvalidArgumentException( |
65
|
2 |
|
sprintf('string param key was expected, int received: %d.', $paramKey) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
50 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
40 |
|
$next = 0; |
74
|
40 |
|
foreach ($paramKeys as $paramKey) { |
75
|
40 |
|
if ($paramKey !== $next) { |
76
|
|
|
throw new InvalidArgumentException( |
77
|
|
|
sprintf('key was expected to be %d, received: %s.', $next, $paramKey) |
78
|
|
|
); |
79
|
|
|
} |
80
|
40 |
|
$next++; |
81
|
|
|
} |
82
|
40 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $params |
86
|
|
|
* @param int $paramHandle |
87
|
|
|
*/ |
88
|
74 |
|
public function bind(array $params, int $paramHandle = self::ALL_AUTO) |
89
|
|
|
{ |
90
|
74 |
|
foreach ($params as $origKey => $var) { |
91
|
53 |
|
if ($this->useNamedParams) { |
92
|
13 |
|
$this->params[$origKey] = $this->getFinalParam($var, $paramHandle); |
93
|
|
|
} else { |
94
|
40 |
|
$this->params[] = $this->getFinalParam($var, $paramHandle); |
95
|
|
|
} |
96
|
|
|
} |
97
|
74 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $var |
101
|
|
|
* @param int $paramHandle |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
53 |
|
protected function getFinalParam($var, int $paramHandle): array |
106
|
|
|
{ |
107
|
|
|
switch ($paramHandle) { |
108
|
53 |
|
case self::ALL_MANUAL: |
109
|
6 |
|
return [$var[0], $var[1]]; |
110
|
47 |
|
case self::ALL_AUTO: |
111
|
41 |
|
if ($var === null) { |
112
|
2 |
|
return [$var, PDO::PARAM_NULL]; |
113
|
39 |
|
} elseif (is_bool($var)) { |
114
|
4 |
|
return [$var, PDO::PARAM_BOOL]; |
115
|
35 |
|
} elseif (is_int($var)) { |
116
|
14 |
|
return [$var, PDO::PARAM_INT]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
33 |
|
return [$var, PDO::PARAM_STR]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function usesNamedParams(): bool |
127
|
|
|
{ |
128
|
|
|
return $this->useNamedParams; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function usesUnnamedParams(): bool |
135
|
|
|
{ |
136
|
|
|
return !$this->useNamedParams; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
44 |
|
public function getAll(): array |
143
|
|
|
{ |
144
|
44 |
|
return $this->params; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|