Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
TranslatableBundleState | |
0.00% |
0 / 19 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
newFromText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getStateId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getStateText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
fromJson | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageGroupProcessing; |
5 | |
6 | use InvalidArgumentException; |
7 | use JsonSerializable; |
8 | |
9 | /** |
10 | * Stores and validates possible translation states for translatable bundles |
11 | * @author Abijeet Patro |
12 | * @since 2024.07 |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | final class TranslatableBundleState implements JsonSerializable { |
16 | public const UNSTABLE = 0; |
17 | public const PROPOSE = 1; |
18 | public const IGNORE = 2; |
19 | |
20 | private const STATE_MAP = [ |
21 | 'unstable' => self::UNSTABLE, |
22 | 'proposed' => self::PROPOSE, |
23 | 'ignored' => self::IGNORE, |
24 | ]; |
25 | private int $state; |
26 | |
27 | public function __construct( int $state ) { |
28 | if ( !in_array( $state, self::STATE_MAP ) ) { |
29 | throw new InvalidArgumentException( "Invalid translatable bundle state: $state" ); |
30 | } |
31 | $this->state = $state; |
32 | } |
33 | |
34 | public static function newFromText( string $stateName ): self { |
35 | $state = self::STATE_MAP[ $stateName ] ?? null; |
36 | if ( $state === null ) { |
37 | throw new InvalidArgumentException( "Invalid translatable bundle state: $stateName" ); |
38 | } |
39 | return new TranslatableBundleState( $state ); |
40 | } |
41 | |
42 | public function getStateId(): int { |
43 | return $this->state; |
44 | } |
45 | |
46 | public function getStateText(): string { |
47 | return array_flip( self::STATE_MAP )[ $this->state ]; |
48 | } |
49 | |
50 | public function jsonSerialize(): array { |
51 | return [ |
52 | 'stateId' => $this->state |
53 | ]; |
54 | } |
55 | |
56 | public static function fromJson( string $json ): self { |
57 | $parsedJson = json_decode( $json, true ); |
58 | if ( !is_array( $parsedJson ) ) { |
59 | throw new InvalidArgumentException( "Unexpected JSON value '$json'" ); |
60 | } |
61 | |
62 | $stateId = $parsedJson[ 'stateId' ] ?? null; |
63 | if ( $stateId === null ) { |
64 | throw new InvalidArgumentException( 'Provided JSON is missing required field stateId' ); |
65 | } |
66 | |
67 | return new TranslatableBundleState( (int)$stateId ); |
68 | } |
69 | } |