MediaWiki  master
MergeStrategy.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class MergeStrategy {
8 
10  public const ARRAY_MERGE_RECURSIVE = 'array_merge_recursive';
11 
13  public const ARRAY_REPLACE_RECURSIVE = 'array_replace_recursive';
14 
16  public const ARRAY_PLUS_2D = 'array_plus_2d';
17 
19  public const ARRAY_PLUS = 'array_plus';
20 
22  public const ARRAY_MERGE = 'array_merge';
23 
25  private $name;
26 
28  private $reversed;
29 
31  private static $strategies = [];
32 
34  private static $reversedStrategies = [];
35 
40  public static function newFromName( string $name ): self {
41  if ( !array_key_exists( $name, self::$strategies ) ) {
42  self::$strategies[$name] = new MergeStrategy( $name );
43  }
44  return self::$strategies[$name];
45  }
46 
51  private function __construct( string $name, bool $reversed = false ) {
52  $this->name = $name;
53  $this->reversed = $reversed;
54  }
55 
59  public function getName(): string {
60  return $this->name;
61  }
62 
74  public function merge( array $destination, array $source ): array {
75  if ( $this->reversed ) {
76  [ $destination, $source ] = [ $source, $destination ];
77  }
78 
79  switch ( $this->name ) {
80  case self::ARRAY_MERGE_RECURSIVE:
81  return array_merge_recursive( $destination, $source );
82  case self::ARRAY_REPLACE_RECURSIVE:
83  return array_replace_recursive( $destination, $source );
84  case self::ARRAY_PLUS_2D:
85  return wfArrayPlus2d( $source, $destination );
86  case self::ARRAY_PLUS:
87  return $source + $destination;
88  case self::ARRAY_MERGE:
89  return array_merge( $destination, $source );
90  default:
91  throw new SettingsBuilderException(
92  'Unknown merge strategy {name}',
93  [ 'name' => $this->name ]
94  );
95  }
96  }
97 
105  public function reverse(): self {
106  if ( !array_key_exists( $this->name, self::$reversedStrategies ) ) {
107  self::$reversedStrategies[$this->name] = new self( $this->name, !$this->reversed );
108  }
109  return self::$reversedStrategies[$this->name];
110  }
111 }
MediaWiki\Settings\Config\MergeStrategy\newFromName
static newFromName(string $name)
Definition: MergeStrategy.php:40
MediaWiki\Settings\Config
Definition: ArrayConfigBuilder.php:3
MediaWiki\Settings\Config\MergeStrategy\$name
string $name
Definition: MergeStrategy.php:21
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:2619
MediaWiki\Settings\Config\MergeStrategy\getName
getName()
Definition: MergeStrategy.php:59
MediaWiki\Settings\SettingsBuilderException
Definition: SettingsBuilderException.php:10
MediaWiki\Settings\Config\MergeStrategy
Definition: MergeStrategy.php:7
MediaWiki\Settings\Config\MergeStrategy\$reversed
bool $reversed
Definition: MergeStrategy.php:28
MediaWiki\Settings\Config\MergeStrategy\$strategies
static MergeStrategy[] $strategies
Definition: MergeStrategy.php:31
$source
$source
Definition: mwdoc-filter.php:34
MediaWiki\Settings\Config\MergeStrategy\__construct
__construct(string $name, bool $reversed=false)
Definition: MergeStrategy.php:51
MediaWiki\Settings\Config\MergeStrategy\merge
merge(array $destination, array $source)
Merge $source into $destination.
Definition: MergeStrategy.php:74
MediaWiki\Settings\Config\MergeStrategy\reverse
reverse()
Create a reversed merge strategy, which will merge $destination into $source instead of $source into ...
Definition: MergeStrategy.php:105
MediaWiki\Settings\Config\MergeStrategy\$reversedStrategies
static MergeStrategy[] $reversedStrategies
Definition: MergeStrategy.php:34