| 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 |
|
| 12 |
protected $config = []; |
| 13 |
|
| 14 |
protected function has( string $key ): bool { |
| 15 |
return array_key_exists( $key, $this->config ); |
| 16 |
} |
| 17 |
|
| 18 |
public function get( string $key ) { |
| 19 |
return $this->config[$key] ?? null; |
| 20 |
} |
| 21 |
|
| 22 |
protected function update( string $key, $value ) { |
| 23 |
$this->config[$key] = $value; |
| 24 |
} |
| 25 |
|
| 26 |
public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder { |
| 27 |
if ( !$mergeStrategies ) { |
| 28 |
$this->config = array_merge( $this->config, $values ); |
| 29 |
return $this; |
| 30 |
} |
| 31 |
|
| 32 |
foreach ( $values as $key => $newValue ) { |
| 33 |
|
| 34 |
if ( array_key_exists( $key, $this->config ) ) { |
| 35 |
$mergeStrategy = $mergeStrategies[$key] ?? null; |
| 36 |
if ( $mergeStrategy && is_array( $newValue ) ) { |
| 37 |
$oldValue = $this->config[$key]; |
| 38 |
if ( $oldValue && is_array( $oldValue ) ) { |
| 39 |
$newValue = $mergeStrategy->merge( $oldValue, $newValue ); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
$this->config[$key] = $newValue; |
| 44 |
} |
| 45 |
|
| 46 |
return $this; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function build(): IterableConfig { |
| 55 |
return new HashConfig( $this->config ); |
| 56 |
} |
| 57 |
|
| 58 |
public function setMultiDefault( $defaults, $mergeStrategies ): ConfigBuilder { |
| 59 |
foreach ( $defaults as $key => $defaultValue ) { |
| 60 |
|
| 61 |
if ( array_key_exists( $key, $this->config ) ) { |
| 62 |
$mergeStrategy = $mergeStrategies[$key] ?? null; |
| 63 |
if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) { |
| 64 |
$customValue = $this->config[$key]; |
| 65 |
if ( is_array( $customValue ) ) { |
| 66 |
$newValue = $mergeStrategy->merge( $defaultValue, $customValue ); |
| 67 |
$this->config[$key] = $newValue; |
| 68 |
} |
| 69 |
} |
| 70 |
} else { |
| 71 |
$this->config[$key] = $defaultValue; |
| 72 |
} |
| 73 |
} |
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
} |