Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
35.00% |
7 / 20 |
|
15.38% |
2 / 13 |
CRAP | |
0.00% |
0 / 1 |
LanguageResult | |
35.00% |
7 / 20 |
|
15.38% |
2 / 13 |
76.79 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
offsetGet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
offsetSet | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
offsetUnset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
whitelist | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
blacklist | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
close | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
bestResults | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
limit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Localization\LanguageDetection |
8 | * @author Patrick Schur <patrick_schur@outlook.de> |
9 | * @copyright Patrick Schur |
10 | * @license https://opensource.org/licenses/mit-license.html MIT |
11 | * @link https://github.com/patrickschur/language-detection |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Localization\LanguageDetection; |
16 | |
17 | /** |
18 | * Langauge match result |
19 | * |
20 | * @package phpOMS\Localization\LanguageDetection |
21 | * @license https://opensource.org/licenses/mit-license.html MIT |
22 | * @link https://github.com/patrickschur/language-detection |
23 | * @since 1.0.0 |
24 | */ |
25 | class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializable |
26 | { |
27 | /** |
28 | * Match threshold |
29 | * |
30 | * @var int |
31 | * @since 1.0.0 |
32 | */ |
33 | private const THRESHOLD = .025; |
34 | |
35 | /** |
36 | * Match values per language |
37 | * |
38 | * @var array<int|float, int|float> |
39 | * @sicne 1.0.0 |
40 | */ |
41 | private array $result = []; |
42 | |
43 | /** |
44 | * Constructor. |
45 | * |
46 | * @param array $result Langauge match results |
47 | * |
48 | * @since 1.0.0 |
49 | */ |
50 | public function __construct(array $result = []) |
51 | { |
52 | $this->result = $result; |
53 | } |
54 | |
55 | /** |
56 | * {@inheritdoc} |
57 | */ |
58 | public function offsetExists($offset) : bool |
59 | { |
60 | return isset($this->result[$offset]); |
61 | } |
62 | |
63 | /** |
64 | * {@inheritdoc} |
65 | */ |
66 | public function offsetGet($offset) : ?float |
67 | { |
68 | return $this->result[$offset] ?? null; |
69 | } |
70 | |
71 | /** |
72 | * {@inheritdoc} |
73 | */ |
74 | public function offsetSet($offset, $value) : void |
75 | { |
76 | /** @var float|int $value */ |
77 | if ($offset === null) { |
78 | $this->result[] = $value; |
79 | } else { |
80 | /** @var int $offset */ |
81 | $this->result[$offset] = $value; |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * {@inheritdoc} |
87 | */ |
88 | public function offsetUnset($offset) : void |
89 | { |
90 | unset($this->result[$offset]); |
91 | } |
92 | |
93 | /** |
94 | * {@inheritdoc} |
95 | */ |
96 | public function jsonSerialize() : array |
97 | { |
98 | return $this->result; |
99 | } |
100 | |
101 | /** |
102 | * Stringify |
103 | * |
104 | * @return string |
105 | * |
106 | * @since 1.0.0 |
107 | */ |
108 | public function __toString() : string |
109 | { |
110 | return (string) \key($this->result); |
111 | } |
112 | |
113 | /** |
114 | * Only return whitelisted results |
115 | * |
116 | * @param string[] ...$whitelist List of whitelisted languages |
117 | * |
118 | * @return self |
119 | * |
120 | * @since 1.0.0 |
121 | */ |
122 | public function whitelist(string ...$whitelist) : self |
123 | { |
124 | return new self(\array_intersect_key($this->result, \array_flip($whitelist))); |
125 | } |
126 | |
127 | /** |
128 | * Remove blacklisted languages |
129 | * |
130 | * @param string[] ...$blacklist List of blacklist languages |
131 | * |
132 | * @return self |
133 | * |
134 | * @since 1.0.0 |
135 | */ |
136 | public function blacklist(string ...$blacklist) : self |
137 | { |
138 | return new self(\array_diff_key($this->result, \array_flip($blacklist))); |
139 | } |
140 | |
141 | /** |
142 | * Get languages results |
143 | * |
144 | * @return array |
145 | * |
146 | * @since 1.0.0 |
147 | */ |
148 | public function close() : array |
149 | { |
150 | return $this->result; |
151 | } |
152 | |
153 | /** |
154 | * Get results based on internally defined threshold |
155 | * |
156 | * @return self |
157 | * |
158 | * @since 1.0.0 |
159 | */ |
160 | public function bestResults() : self |
161 | { |
162 | if (empty($this->result)) { |
163 | return new self(); |
164 | } |
165 | |
166 | $first = \array_values($this->result)[0]; |
167 | |
168 | return new self(\array_filter($this->result, function ($value) use ($first) { |
169 | return ($first - $value) <= self::THRESHOLD; |
170 | })); |
171 | } |
172 | |
173 | /** |
174 | * {@inheritdoc} |
175 | */ |
176 | public function getIterator() : \ArrayIterator |
177 | { |
178 | return new \ArrayIterator($this->result); |
179 | } |
180 | |
181 | /** |
182 | * Get results A to B |
183 | * |
184 | * @param int $offset Zero indexed start value |
185 | * @param null|int $length Number of results |
186 | * |
187 | * @return self |
188 | * |
189 | * @since 1.0.0 |
190 | */ |
191 | public function limit(int $offset, int $length = null) : self |
192 | { |
193 | return new self(\array_slice($this->result, $offset, $length)); |
194 | } |
195 | } |