| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |