Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
Equivset | |
100.00% |
16 / 16 |
|
100.00% |
8 / 8 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
all | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
normalize | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isEqual | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
has | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
load | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 | |
19 | namespace Wikimedia\Equivset; |
20 | |
21 | use ArrayIterator; |
22 | use IteratorAggregate; |
23 | use LogicException; |
24 | |
25 | /** |
26 | * Default Equivset |
27 | */ |
28 | class Equivset implements EquivsetInterface, IteratorAggregate { |
29 | |
30 | /** |
31 | * @var array<string,string> |
32 | */ |
33 | protected array $data; |
34 | |
35 | /** |
36 | * @var string |
37 | */ |
38 | protected string $dataPath; |
39 | |
40 | /** |
41 | * Equivset |
42 | * |
43 | * @param array<string,string> $data Equivalent Set |
44 | * @param string $dataPath Path of the equivset array. |
45 | */ |
46 | public function __construct( array $data = [], string $dataPath = '' ) { |
47 | $this->data = $data; |
48 | $this->dataPath = $dataPath ?: __DIR__ . '/../dist/equivset.php'; |
49 | } |
50 | |
51 | /** |
52 | * Get the equivset. |
53 | * |
54 | * @return array<string,string> An associative array of equivalent characters. |
55 | */ |
56 | public function all(): array { |
57 | if ( !$this->data ) { |
58 | $this->data = $this->load(); |
59 | } |
60 | |
61 | return $this->data; |
62 | } |
63 | |
64 | /** |
65 | * {@inheritdoc} |
66 | * |
67 | * @param string $value The string to normalize against the equivset. |
68 | * @return string |
69 | */ |
70 | public function normalize( string $value ): string { |
71 | $data = $this->all(); |
72 | |
73 | return strtr( $value, $data ); |
74 | } |
75 | |
76 | /** |
77 | * {@inheritdoc} |
78 | * |
79 | * @param string $str1 The first string. |
80 | * @param string $str2 The second string. |
81 | * |
82 | * @return bool |
83 | */ |
84 | public function isEqual( string $str1, string $str2 ): bool { |
85 | return $this->normalize( $str1 ) === $this->normalize( $str2 ); |
86 | } |
87 | |
88 | /** |
89 | * {@inheritdoc} |
90 | * |
91 | * @param string $key The character that was used. |
92 | * @return bool If the character has an equivalent. |
93 | */ |
94 | public function has( string $key ): bool { |
95 | $data = $this->all(); |
96 | |
97 | return array_key_exists( $key, $data ); |
98 | } |
99 | |
100 | /** |
101 | * {@inheritdoc} |
102 | * |
103 | * @param string $key The character that was used. |
104 | * @return string The equivalent character. |
105 | * @throws LogicException If character does not exist. |
106 | */ |
107 | public function get( string $key ): string { |
108 | $data = $this->all(); |
109 | |
110 | if ( !array_key_exists( $key, $data ) ) { |
111 | throw new LogicException( 'Equivalent Character Not Found' ); |
112 | } |
113 | |
114 | return $data[$key]; |
115 | } |
116 | |
117 | /** |
118 | * {@inheritdoc} |
119 | * |
120 | * @return ArrayIterator The complete Equivset. |
121 | */ |
122 | public function getIterator(): ArrayIterator { |
123 | return new ArrayIterator( $this->all() ); |
124 | } |
125 | |
126 | /** |
127 | * Get the equivset. |
128 | * |
129 | * @return array<string,string> An associative array of equivalent characters. |
130 | */ |
131 | protected function load(): array { |
132 | // This will naturally throw if the file does not exist, is not readable, |
133 | // or can't be parsed. |
134 | return require $this->dataPath; |
135 | } |
136 | } |