Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
ConfigBuilderBase | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
16 | |
100.00% |
1 / 1 |
has | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
update | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
set | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
6 | |||
setMulti | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setDefault | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
setMultiDefault | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Settings\Config; |
4 | |
5 | abstract class ConfigBuilderBase implements ConfigBuilder { |
6 | |
7 | abstract protected function has( string $key ): bool; |
8 | |
9 | abstract protected function update( string $key, $value ); |
10 | |
11 | /** |
12 | * @inheritDoc |
13 | */ |
14 | public function set( |
15 | string $key, |
16 | $newValue, |
17 | MergeStrategy $mergeStrategy = null |
18 | ): ConfigBuilder { |
19 | if ( $mergeStrategy && $this->has( $key ) && is_array( $newValue ) ) { |
20 | $oldValue = $this->get( $key ); |
21 | if ( $oldValue && is_array( $oldValue ) ) { |
22 | $newValue = $mergeStrategy->merge( $oldValue, $newValue ); |
23 | } |
24 | } |
25 | $this->update( $key, $newValue ); |
26 | return $this; |
27 | } |
28 | |
29 | /** |
30 | * @inheritDoc |
31 | */ |
32 | public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder { |
33 | foreach ( $values as $key => $value ) { |
34 | $this->set( $key, $value, $mergeStrategies[$key] ?? null ); |
35 | } |
36 | return $this; |
37 | } |
38 | |
39 | /** |
40 | * @inheritDoc |
41 | */ |
42 | public function setDefault( |
43 | string $key, |
44 | $defaultValue, |
45 | MergeStrategy $mergeStrategy = null |
46 | ): ConfigBuilder { |
47 | if ( $this->has( $key ) ) { |
48 | if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) { |
49 | $customValue = $this->get( $key ); |
50 | if ( is_array( $customValue ) ) { |
51 | $newValue = $mergeStrategy->merge( $defaultValue, $customValue ); |
52 | $this->update( $key, $newValue ); |
53 | } |
54 | } |
55 | } else { |
56 | $this->update( $key, $defaultValue ); |
57 | } |
58 | |
59 | return $this; |
60 | } |
61 | |
62 | /** |
63 | * @inheritDoc |
64 | */ |
65 | public function setMultiDefault( array $defaults, array $mergeStrategies ): ConfigBuilder { |
66 | foreach ( $defaults as $key => $defaultValue ) { |
67 | $this->setDefault( $key, $defaultValue, $mergeStrategies[$key] ?? null ); |
68 | } |
69 | return $this; |
70 | } |
71 | |
72 | } |