|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
|
6
|
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Component; |
|
8
|
|
|
|
|
9
|
|
|
use function array_merge_recursive; |
|
10
|
|
|
use function implode; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
use function strcasecmp; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Parses a list of options. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class OptionsArray implements Component |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param array<int, mixed> $options The array of options. Options that have a value |
|
21
|
|
|
* must be an array with at least two keys `name` and |
|
22
|
|
|
* `expr` or `value`. |
|
23
|
|
|
*/ |
|
24
|
1106 |
|
public function __construct(public array $options = []) |
|
25
|
|
|
{ |
|
26
|
1106 |
|
} |
|
27
|
|
|
|
|
28
|
194 |
|
public function build(): string |
|
29
|
|
|
{ |
|
30
|
194 |
|
if (empty($this->options)) { |
|
31
|
164 |
|
return ''; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
118 |
|
$options = []; |
|
35
|
118 |
|
foreach ($this->options as $option) { |
|
36
|
118 |
|
if (! is_array($option)) { |
|
37
|
100 |
|
$options[] = $option; |
|
38
|
|
|
} else { |
|
39
|
70 |
|
$options[] = $option['name'] |
|
40
|
70 |
|
. (! empty($option['equals']) ? '=' : ' ') |
|
41
|
70 |
|
. (! empty($option['expr']) ? $option['expr'] : $option['value']); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
118 |
|
return implode(' ', $options); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Checks if it has the specified option and returns it value or true. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key the key to be checked |
|
52
|
|
|
* @param bool $getExpr Gets the expression instead of the value. |
|
53
|
|
|
* The value is the processed form of the expression. |
|
54
|
|
|
*/ |
|
55
|
464 |
|
public function has(string $key, bool $getExpr = false): mixed |
|
56
|
|
|
{ |
|
57
|
464 |
|
foreach ($this->options as $option) { |
|
58
|
438 |
|
if (is_array($option)) { |
|
59
|
114 |
|
if (! strcasecmp($key, $option['name'])) { |
|
60
|
16 |
|
return $getExpr ? $option['expr'] : $option['value']; |
|
61
|
|
|
} |
|
62
|
436 |
|
} elseif (! strcasecmp($key, $option)) { |
|
63
|
418 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
440 |
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Removes the option from the array. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $key the key to be removed |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool whether the key was found and deleted or not |
|
76
|
|
|
*/ |
|
77
|
10 |
|
public function remove(string $key): bool |
|
78
|
|
|
{ |
|
79
|
10 |
|
foreach ($this->options as $idx => $option) { |
|
80
|
10 |
|
if (is_array($option)) { |
|
81
|
10 |
|
if (! strcasecmp($key, $option['name'])) { |
|
82
|
10 |
|
unset($this->options[$idx]); |
|
83
|
|
|
|
|
84
|
10 |
|
return true; |
|
85
|
|
|
} |
|
86
|
10 |
|
} elseif (! strcasecmp($key, $option)) { |
|
87
|
10 |
|
unset($this->options[$idx]); |
|
88
|
|
|
|
|
89
|
10 |
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Merges the specified options with these ones. Values with same ID will be |
|
98
|
|
|
* replaced. |
|
99
|
|
|
*/ |
|
100
|
22 |
|
public function merge(OptionsArray $options): void |
|
101
|
|
|
{ |
|
102
|
22 |
|
$this->options = array_merge_recursive($this->options, $options->options); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Checks tf there are no options set. |
|
107
|
|
|
*/ |
|
108
|
196 |
|
public function isEmpty(): bool |
|
109
|
|
|
{ |
|
110
|
196 |
|
return empty($this->options); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
98 |
|
public function __toString(): string |
|
114
|
|
|
{ |
|
115
|
98 |
|
return $this->build(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|