Completed
Branch master (939199)
by
unknown
39:35
created

includes/libs/ReplacementArray.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * Wrapper around strtr() that holds replacements
23
 */
24
class ReplacementArray {
25
	private $data = false;
26
27
	/**
28
	 * Create an object with the specified replacement array
29
	 * The array should have the same form as the replacement array for strtr()
30
	 * @param array $data
31
	 */
32
	public function __construct( $data = [] ) {
33
		$this->data = $data;
34
	}
35
36
	/**
37
	 * @return array
38
	 */
39
	public function __sleep() {
40
		return [ 'data' ];
41
	}
42
43
	/**
44
	 * Set the whole replacement array at once
45
	 * @param array $data
46
	 */
47
	public function setArray( $data ) {
48
		$this->data = $data;
49
	}
50
51
	/**
52
	 * @return array|bool
53
	 */
54
	public function getArray() {
55
		return $this->data;
56
	}
57
58
	/**
59
	 * Set an element of the replacement array
60
	 * @param string $from
61
	 * @param string $to
62
	 */
63
	public function setPair( $from, $to ) {
64
		$this->data[$from] = $to;
65
	}
66
67
	/**
68
	 * @param array $data
69
	 */
70
	public function mergeArray( $data ) {
71
		$this->data = $data + $this->data;
72
	}
73
74
	/**
75
	 * @param ReplacementArray $other
76
	 */
77
	public function merge( ReplacementArray $other ) {
78
		$this->data = $other->data + $this->data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $other->data + $this->data of type integer or double is incompatible with the declared type boolean of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
	}
80
81
	/**
82
	 * @param string $from
83
	 */
84
	public function removePair( $from ) {
85
		unset( $this->data[$from] );
86
	}
87
88
	/**
89
	 * @param array $data
90
	 */
91
	public function removeArray( $data ) {
92
		foreach ( $data as $from => $to ) {
93
			$this->removePair( $from );
94
		}
95
	}
96
97
	/**
98
	 * @param string $subject
99
	 * @return string
100
	 */
101
	public function replace( $subject ) {
102
		return strtr( $subject, $this->data );
103
	}
104
}
105