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