16 public const UNSTABLE = 0;
17 public const PROPOSE = 1;
18 public const IGNORE = 2;
20 private const STATE_MAP = [
21 'unstable' => self::UNSTABLE,
22 'proposed' => self::PROPOSE,
23 'ignored' => self::IGNORE,
27 public function __construct(
int $state ) {
28 if ( !in_array( $state, self::STATE_MAP ) ) {
29 throw new InvalidArgumentException(
"Invalid translatable bundle state: $state" );
31 $this->state = $state;
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" );
42 public function getStateId():
int {
46 public function getStateText():
string {
47 return array_flip( self::STATE_MAP )[ $this->state ];
50 public function jsonSerialize(): array {
52 'stateId' => $this->state
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'" );
62 $stateId = $parsedJson[
'stateId' ] ??
null;
63 if ( $stateId ===
null ) {
64 throw new InvalidArgumentException(
'Provided JSON is missing required field stateId' );