File: MergeStrategy.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (106 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace MediaWiki\Settings\Config;

use MediaWiki\Settings\SettingsBuilderException;
use function array_key_exists;

class MergeStrategy {

	public const ARRAY_MERGE_RECURSIVE = 'array_merge_recursive';
	public const ARRAY_REPLACE_RECURSIVE = 'array_replace_recursive';
	public const ARRAY_PLUS_2D = 'array_plus_2d';
	public const ARRAY_PLUS = 'array_plus';
	public const ARRAY_MERGE = 'array_merge';
	public const REPLACE = 'replace';

	/** @var string */
	private $name;

	/** @var bool */
	private $reversed;

	/** @var MergeStrategy[] */
	private static $strategies = [];

	/** @var MergeStrategy[] */
	private static $reversedStrategies = [];

	/**
	 * @param string $name
	 * @return static
	 */
	public static function newFromName( string $name ): self {
		if ( !array_key_exists( $name, self::$strategies ) ) {
			self::$strategies[$name] = new MergeStrategy( $name );
		}
		return self::$strategies[$name];
	}

	/**
	 * @param string $name
	 * @param bool $reversed
	 */
	private function __construct( string $name, bool $reversed = false ) {
		$this->name = $name;
		$this->reversed = $reversed;
	}

	/**
	 * @return string
	 */
	public function getName(): string {
		return $this->name;
	}

	/**
	 * Merge $source into $destination.
	 *
	 * @note For all merge strategies except self::ARRAY_MERGE_RECURSIVE,
	 * for the values that have the same key, the value from $source will
	 * override the value in the $destination.
	 *
	 * @param array $destination
	 * @param array $source
	 * @return array
	 */
	public function merge( array $destination, array $source ): array {
		if ( $this->reversed ) {
			[ $destination, $source ] = [ $source, $destination ];
		}

		switch ( $this->name ) {
			case self::REPLACE:
				return $source;
			case self::ARRAY_MERGE_RECURSIVE:
				return array_merge_recursive( $destination, $source );
			case self::ARRAY_REPLACE_RECURSIVE:
				return array_replace_recursive( $destination, $source );
			case self::ARRAY_PLUS_2D:
				return wfArrayPlus2d( $source, $destination );
			case self::ARRAY_PLUS:
				return $source + $destination;
			case self::ARRAY_MERGE:
				return array_merge( $destination, $source );
			default:
				throw new SettingsBuilderException(
					'Unknown merge strategy {name}',
					[ 'name' => $this->name ]
				);
		}
	}

	/**
	 * Create a reversed merge strategy, which will merge $destination into $source
	 * instead of $source into $destination.
	 *
	 * @see self::merge
	 * @return MergeStrategy
	 */
	public function reverse(): self {
		if ( !array_key_exists( $this->name, self::$reversedStrategies ) ) {
			self::$reversedStrategies[$this->name] = new self( $this->name, !$this->reversed );
		}
		return self::$reversedStrategies[$this->name];
	}
}