Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / libs / ReplacementArray.php
blob7fdb309383bd4e7f8e31edf9140487a3e488f042
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 /**
22 * Replacement array for FSS with fallback to strtr()
23 * Supports lazy initialisation of FSS resource
25 class ReplacementArray {
26 private $data = false;
27 private $fss = false;
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( $data = array() ) {
35 $this->data = $data;
38 /**
39 * @return array
41 public function __sleep() {
42 return array( 'data' );
45 public function __wakeup() {
46 $this->fss = false;
49 /**
50 * Set the whole replacement array at once
51 * @param array $data
53 public function setArray( $data ) {
54 $this->data = $data;
55 $this->fss = false;
58 /**
59 * @return array|bool
61 public function getArray() {
62 return $this->data;
65 /**
66 * Set an element of the replacement array
67 * @param string $from
68 * @param string $to
70 public function setPair( $from, $to ) {
71 $this->data[$from] = $to;
72 $this->fss = false;
75 /**
76 * @param array $data
78 public function mergeArray( $data ) {
79 $this->data = array_merge( $this->data, $data );
80 $this->fss = false;
83 /**
84 * @param ReplacementArray $other
86 public function merge( ReplacementArray $other ) {
87 $this->data = array_merge( $this->data, $other->data );
88 $this->fss = false;
91 /**
92 * @param string $from
94 public function removePair( $from ) {
95 unset( $this->data[$from] );
96 $this->fss = false;
99 /**
100 * @param array $data
102 public function removeArray( $data ) {
103 foreach ( $data as $from => $to ) {
104 $this->removePair( $from );
106 $this->fss = false;
110 * @param string $subject
111 * @return string
113 public function replace( $subject ) {
114 if ( function_exists( 'fss_prep_replace' ) ) {
115 if ( $this->fss === false ) {
116 $this->fss = fss_prep_replace( $this->data );
118 $result = fss_exec_replace( $this->fss, $subject );
119 } else {
120 $result = strtr( $subject, $this->data );
123 return $result;