Merge "SpecialMovepage: Convert form to use OOUI controls"
[lhc/web/wiklou.git] / includes / libs / ReplacementArray.php
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 /**
22 * Replacement array for FSS with fallback to strtr()
23 * Supports lazy initialisation of FSS resource
24 */
25 class ReplacementArray {
26 private $data = false;
27 private $fss = false;
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( $data = array() ) {
35 $this->data = $data;
36 }
37
38 /**
39 * @return array
40 */
41 public function __sleep() {
42 return array( 'data' );
43 }
44
45 public function __wakeup() {
46 $this->fss = false;
47 }
48
49 /**
50 * Set the whole replacement array at once
51 * @param array $data
52 */
53 public function setArray( $data ) {
54 $this->data = $data;
55 $this->fss = false;
56 }
57
58 /**
59 * @return array|bool
60 */
61 public function getArray() {
62 return $this->data;
63 }
64
65 /**
66 * Set an element of the replacement array
67 * @param string $from
68 * @param string $to
69 */
70 public function setPair( $from, $to ) {
71 $this->data[$from] = $to;
72 $this->fss = false;
73 }
74
75 /**
76 * @param array $data
77 */
78 public function mergeArray( $data ) {
79 $this->data = $data + $this->data;
80 $this->fss = false;
81 }
82
83 /**
84 * @param ReplacementArray $other
85 */
86 public function merge( ReplacementArray $other ) {
87 $this->data = $other->data + $this->data;
88 $this->fss = false;
89 }
90
91 /**
92 * @param string $from
93 */
94 public function removePair( $from ) {
95 unset( $this->data[$from] );
96 $this->fss = false;
97 }
98
99 /**
100 * @param array $data
101 */
102 public function removeArray( $data ) {
103 foreach ( $data as $from => $to ) {
104 $this->removePair( $from );
105 }
106 $this->fss = false;
107 }
108
109 /**
110 * @param string $subject
111 * @return string
112 */
113 public function replace( $subject ) {
114 if (
115 function_exists( 'fss_prep_replace' ) &&
116 version_compare( PHP_VERSION, '5.5.0' ) < 0
117 ) {
118 if ( $this->fss === false ) {
119 $this->fss = fss_prep_replace( $this->data );
120 }
121 $result = fss_exec_replace( $this->fss, $subject );
122 } else {
123 $result = strtr( $subject, $this->data );
124 }
125
126 return $result;
127 }
128 }