| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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 |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Language; |
| 22 | |
| 23 | /** |
| 24 | * Wrapper around strtr() that holds replacements |
| 25 | */ |
| 26 | class ReplacementArray { |
| 27 | private array $data; |
| 28 | |
| 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 |
| 33 | */ |
| 34 | public function __construct( array $data = [] ) { |
| 35 | $this->data = $data; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return array |
| 40 | */ |
| 41 | public function __sleep() { |
| 42 | return [ 'data' ]; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Set the whole replacement array at once |
| 47 | * @param array $data |
| 48 | */ |
| 49 | public function setArray( array $data ) { |
| 50 | $this->data = $data; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return array |
| 55 | */ |
| 56 | public function getArray() { |
| 57 | return $this->data; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set an element of the replacement array |
| 62 | * @param string $from |
| 63 | * @param string $to |
| 64 | */ |
| 65 | public function setPair( $from, $to ) { |
| 66 | $this->data[$from] = $to; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param array $data |
| 71 | */ |
| 72 | public function mergeArray( $data ) { |
| 73 | $this->data = $data + $this->data; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param ReplacementArray $other |
| 78 | */ |
| 79 | public function merge( ReplacementArray $other ) { |
| 80 | $this->data = $other->data + $this->data; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string $from |
| 85 | */ |
| 86 | public function removePair( $from ) { |
| 87 | unset( $this->data[$from] ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param array $data |
| 92 | */ |
| 93 | public function removeArray( $data ) { |
| 94 | foreach ( $data as $from => $to ) { |
| 95 | $this->removePair( $from ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param string $subject |
| 101 | * @return string |
| 102 | */ |
| 103 | public function replace( $subject ) { |
| 104 | return strtr( $subject, $this->data ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** @deprecated class alias since 1.43 */ |
| 109 | class_alias( ReplacementArray::class, 'ReplacementArray' ); |