Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / libs / ReplacementArray.php
blob107ac4ed058bb4b8494447f71f8ab275822aa917
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Language;
23 /**
24 * Wrapper around strtr() that holds replacements
26 class ReplacementArray {
27 private array $data;
29 /**
30 * Create an object with the specified replacement array
31 * The array should have the same form as the replacement array for strtr()
32 * @param array $data
34 public function __construct( array $data = [] ) {
35 $this->data = $data;
38 /**
39 * @return array
41 public function __sleep() {
42 return [ 'data' ];
45 /**
46 * Set the whole replacement array at once
47 * @param array $data
49 public function setArray( array $data ) {
50 $this->data = $data;
53 /**
54 * @return array
56 public function getArray() {
57 return $this->data;
60 /**
61 * Set an element of the replacement array
62 * @param string $from
63 * @param string $to
65 public function setPair( $from, $to ) {
66 $this->data[$from] = $to;
69 /**
70 * @param array $data
72 public function mergeArray( $data ) {
73 $this->data = $data + $this->data;
76 /**
77 * @param ReplacementArray $other
79 public function merge( ReplacementArray $other ) {
80 $this->data = $other->data + $this->data;
83 /**
84 * @param string $from
86 public function removePair( $from ) {
87 unset( $this->data[$from] );
90 /**
91 * @param array $data
93 public function removeArray( $data ) {
94 foreach ( $data as $from => $to ) {
95 $this->removePair( $from );
99 /**
100 * @param string $subject
101 * @return string
103 public function replace( $subject ) {
104 return strtr( $subject, $this->data );
108 /** @deprecated class alias since 1.43 */
109 class_alias( ReplacementArray::class, 'ReplacementArray' );