Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
4 / 8 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
Bcp47CodeValue | |
50.00% |
4 / 8 |
|
33.33% |
2 / 6 |
19.12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toBcp47Code | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSameCodeAs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
__toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fromBcp47Code | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
isSameCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
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 | namespace Wikimedia\Bcp47Code; |
22 | |
23 | /** |
24 | * This is a simple value object demonstrating the simplest possible |
25 | * implementation of the Bcp47Code interface. |
26 | */ |
27 | class Bcp47CodeValue implements Bcp47Code { |
28 | |
29 | /** |
30 | * The BCP 47 code corresponding to this language. |
31 | * @var string |
32 | */ |
33 | private $code; |
34 | |
35 | /** |
36 | * Create a new instance of this value object representing a language with |
37 | * the given BCP 47 code. |
38 | * @param string $bcp47code the BCP 47 code for the language |
39 | */ |
40 | public function __construct( string $bcp47code ) { |
41 | $this->code = $bcp47code; |
42 | } |
43 | |
44 | /** @inheritDoc */ |
45 | public function toBcp47Code(): string { |
46 | return $this->code; |
47 | } |
48 | |
49 | /** @inheritDoc */ |
50 | public function isSameCodeAs( Bcp47Code $other ): bool { |
51 | return ( $this === $other ) || self::isSameCode( $this, $other ); |
52 | } |
53 | |
54 | public function __toString() { |
55 | return $this->toBcp47Code(); |
56 | } |
57 | |
58 | /** |
59 | * Simple helper to coerce any Bcp47Code into a Bcp47CodeValue. |
60 | * @param Bcp47Code $language an object representing a language |
61 | * @return Bcp47CodeValue a simple value object representing a language. |
62 | */ |
63 | public static function fromBcp47Code( Bcp47Code $language ): Bcp47CodeValue { |
64 | if ( $language instanceof Bcp47CodeValue ) { |
65 | return $language; |
66 | } |
67 | return new Bcp47CodeValue( $language->toBcp47Code() ); |
68 | } |
69 | |
70 | /** |
71 | * Simple helper to compare Bcp47Code in the proper case-insensitive |
72 | * manner. |
73 | * @param Bcp47Code $a |
74 | * @param Bcp47Code $b |
75 | * @return bool True if the bcp-47 codes should be considered equal |
76 | */ |
77 | public static function isSameCode( Bcp47Code $a, Bcp47Code $b ) { |
78 | return ( $a === $b ) || strcasecmp( $a->toBcp47Code(), $b->toBcp47Code() ) === 0; |
79 | } |
80 | } |