1 | <?php |
||
2 | namespace Kir\MySQL\Builder\Internal; |
||
3 | |||
4 | use Kir\MySQL\Database; |
||
5 | use Stringable; |
||
6 | |||
7 | /** |
||
8 | * @phpstan-import-type DBParameterValueType from Types |
||
9 | * @phpstan-import-type DBWhereExpressionType from Types |
||
10 | */ |
||
11 | final class ConditionBuilder { |
||
12 | /** |
||
13 | * @param Database $db |
||
14 | * @param string $query |
||
15 | * @param array<int, array{DBWhereExpressionType, list<DBParameterValueType>}> $conditions |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
16 | * @param string $token |
||
17 | * @return string |
||
18 | */ |
||
19 | public static function build(Database $db, string $query, array $conditions, string $token): string { |
||
20 | if(!count($conditions)) { |
||
21 | return $query; |
||
22 | } |
||
23 | $query .= "{$token}\n"; |
||
24 | $arr = []; |
||
25 | foreach($conditions as [$expression, $arguments]) { |
||
26 | if(is_array($expression)) { |
||
27 | foreach($expression as $key => $value) { |
||
28 | $key = self::formatKey($key); |
||
29 | if($value === null) { |
||
30 | $arr = self::buildCondition($arr, "ISNULL({$key})", [$value], $db); |
||
31 | } else { |
||
32 | $arr = self::buildCondition($arr, "{$key}=?", [$value], $db); |
||
33 | } |
||
34 | } |
||
35 | } else { |
||
36 | /** @var Stringable|string $expression */ |
||
37 | $arr = self::buildCondition($arr, (string) $expression, $arguments, $db); |
||
38 | } |
||
39 | } |
||
40 | $query .= implode("\n\tAND\n", $arr); |
||
41 | return "{$query}\n"; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string[] $conditions |
||
46 | * @param string $expression |
||
47 | * @param list<DBParameterValueType> $arguments |
||
0 ignored issues
–
show
The type
Kir\MySQL\Builder\Internal\list was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
48 | * @param Database $db |
||
49 | * @return string[] |
||
50 | */ |
||
51 | private static function buildCondition(array $conditions, string $expression, array $arguments, Database $db): array { |
||
52 | $expr = $db->quoteExpression($expression, $arguments); |
||
53 | $conditions[] = "\t({$expr})"; |
||
54 | return $conditions; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $key |
||
59 | * @return string |
||
60 | */ |
||
61 | private static function formatKey(string $key): string { |
||
62 | if(strpos($key, '`') !== false || strpos($key, '(') !== false) { |
||
63 | return $key; |
||
64 | } |
||
65 | $keyParts = explode('.', $key); |
||
66 | $fn = static fn(string $part) => "`{$part}`"; |
||
67 | $enclosedKeyParts = array_map($fn, $keyParts); |
||
68 | return implode('.', $enclosedKeyParts); |
||
69 | } |
||
70 | } |
||
71 |