Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleState.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use InvalidArgumentException;
7use JsonSerializable;
8
15final 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}
Stores and validates possible translation states for translatable bundles.