MediaWiki master
ConfigBuilderBase.php
Go to the documentation of this file.
1<?php
2
4
5abstract class ConfigBuilderBase implements ConfigBuilder {
6
7 abstract protected function has( string $key ): bool;
8
13 abstract protected function update( string $key, $value );
14
18 public function set(
19 string $key,
20 $newValue,
21 ?MergeStrategy $mergeStrategy = null
22 ): ConfigBuilder {
23 if ( $mergeStrategy && $this->has( $key ) && is_array( $newValue ) ) {
24 $oldValue = $this->get( $key );
25 if ( $oldValue && is_array( $oldValue ) ) {
26 $newValue = $mergeStrategy->merge( $oldValue, $newValue );
27 }
28 }
29 $this->update( $key, $newValue );
30 return $this;
31 }
32
36 public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder {
37 foreach ( $values as $key => $value ) {
38 $this->set( $key, $value, $mergeStrategies[$key] ?? null );
39 }
40 return $this;
41 }
42
46 public function setDefault(
47 string $key,
48 $defaultValue,
49 ?MergeStrategy $mergeStrategy = null
50 ): ConfigBuilder {
51 if ( $this->has( $key ) ) {
52 if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) {
53 $customValue = $this->get( $key );
54 if ( is_array( $customValue ) ) {
55 $newValue = $mergeStrategy->merge( $defaultValue, $customValue );
56 $this->update( $key, $newValue );
57 }
58 }
59 } else {
60 $this->update( $key, $defaultValue );
61 }
62
63 return $this;
64 }
65
69 public function setMultiDefault( array $defaults, array $mergeStrategies ): ConfigBuilder {
70 foreach ( $defaults as $key => $defaultValue ) {
71 $this->setDefault( $key, $defaultValue, $mergeStrategies[$key] ?? null );
72 }
73 return $this;
74 }
75
76}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
setMultiDefault(array $defaults, array $mergeStrategies)
Set defaults in a batch.ConfigBuilder
setMulti(array $values, array $mergeStrategies=[])
Set all values in the array.ConfigBuilder
setDefault(string $key, $defaultValue, ?MergeStrategy $mergeStrategy=null)
Set the default for the configuration $key to $defaultValue.If the $key is already set,...