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
22 * Wrapper around strtr() that holds replacements
24 class ReplacementArray
{
25 private $data = false;
28 * Create an object with the specified replacement array
29 * The array should have the same form as the replacement array for strtr()
32 public function __construct( $data = [] ) {
39 public function __sleep() {
40 return array( 'data' );
44 * Set the whole replacement array at once
47 public function setArray( $data ) {
54 public function getArray() {
59 * Set an element of the replacement array
63 public function setPair( $from, $to ) {
64 $this->data
[$from] = $to;
70 public function mergeArray( $data ) {
71 $this->data
= $data +
$this->data
;
75 * @param ReplacementArray $other
77 public function merge( ReplacementArray
$other ) {
78 $this->data
= $other->data +
$this->data
;
84 public function removePair( $from ) {
85 unset( $this->data
[$from] );
91 public function removeArray( $data ) {
92 foreach ( $data as $from => $to ) {
93 $this->removePair( $from );
98 * @param string $subject
101 public function replace( $subject ) {
102 return strtr( $subject, $this->data
);