MediaWiki  master
ConfigBuilderBase.php
Go to the documentation of this file.
1 <?php
2 
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 
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 
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 
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 
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 }
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
setMultiDefault(array $defaults, array $mergeStrategies)
Set defaults in a batch.The default values The merge strategies indexed by config key ConfigBuilder i...
setMulti(array $values, array $mergeStrategies=[])
Set all values in the array.The merge strategies indexed by config key ConfigBuilder
setDefault(string $key, $defaultValue, MergeStrategy $mergeStrategy=null)
Set the default for the configuration $key to $defaultValue.If the $key is already set,...
has( $name)
Check whether a configuration option is set for the given name.