Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ReplacementArray | |
0.00% |
0 / 11 |
|
0.00% |
0 / 10 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __sleep | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setPair | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| mergeArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| merge | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removePair | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeArray | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| replace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace Wikimedia; |
| 8 | |
| 9 | /** |
| 10 | * Wrapper around strtr() that holds replacements |
| 11 | */ |
| 12 | class ReplacementArray { |
| 13 | private array $data; |
| 14 | |
| 15 | /** |
| 16 | * Create an object with the specified replacement array |
| 17 | * The array should have the same form as the replacement array for strtr() |
| 18 | * @param array $data |
| 19 | */ |
| 20 | public function __construct( array $data = [] ) { |
| 21 | $this->data = $data; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return array |
| 26 | */ |
| 27 | public function __sleep() { |
| 28 | return [ 'data' ]; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Set the whole replacement array at once |
| 33 | */ |
| 34 | public function setArray( array $data ) { |
| 35 | $this->data = $data; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return array |
| 40 | */ |
| 41 | public function getArray() { |
| 42 | return $this->data; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Set an element of the replacement array |
| 47 | * @param string $from |
| 48 | * @param string $to |
| 49 | */ |
| 50 | public function setPair( $from, $to ) { |
| 51 | $this->data[$from] = $to; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param array $data |
| 56 | */ |
| 57 | public function mergeArray( $data ) { |
| 58 | $this->data = $data + $this->data; |
| 59 | } |
| 60 | |
| 61 | public function merge( ReplacementArray $other ) { |
| 62 | $this->data = $other->data + $this->data; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param string $from |
| 67 | */ |
| 68 | public function removePair( $from ) { |
| 69 | unset( $this->data[$from] ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param array $data |
| 74 | */ |
| 75 | public function removeArray( $data ) { |
| 76 | foreach ( $data as $from => $to ) { |
| 77 | $this->removePair( $from ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param string $subject |
| 83 | * @return string |
| 84 | */ |
| 85 | public function replace( $subject ) { |
| 86 | return strtr( $subject, $this->data ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** @deprecated class alias since 1.43 */ |
| 91 | class_alias( ReplacementArray::class, 'ReplacementArray' ); |
| 92 | /** @deprecated class alias since 1.45 */ |
| 93 | class_alias( ReplacementArray::class, 'MediaWiki\\Language\\ReplacementArray' ); |