Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.59% covered (success)
94.59%
35 / 37
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Converter
94.59% covered (success)
94.59%
35 / 37
83.33% covered (warning)
83.33%
5 / 6
21.07
0.00% covered (danger)
0.00%
0 / 1
 getTypes
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
 createMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convertMessage
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 convertParam
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
9
 convertMessageValue
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 convertMessageParam
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace MediaWiki\Message;
4
5use InvalidArgumentException;
6use MessageSpecifier;
7use ReflectionClass;
8use Wikimedia\Message\ListParam;
9use Wikimedia\Message\MessageParam;
10use Wikimedia\Message\MessageValue;
11use Wikimedia\Message\ParamType;
12use Wikimedia\Message\ScalarParam;
13
14/**
15 * Converter between Message and MessageValue
16 * @since 1.35
17 */
18class Converter {
19
20    /** @var string[]|null ParamType constants */
21    private static $constants = null;
22
23    /**
24     * Return the ParamType constants
25     * @return string[]
26     */
27    private static function getTypes() {
28        if ( self::$constants === null ) {
29            $rc = new ReflectionClass( ParamType::class );
30            self::$constants = array_values( $rc->getConstants() );
31        }
32
33        return self::$constants;
34    }
35
36    /**
37     * Allow the Message class to be mocked in tests by constructing objects in
38     * a protected method.
39     *
40     * @internal
41     * @param string $key
42     * @return Message
43     */
44    public function createMessage( $key ) {
45        return new Message( $key );
46    }
47
48    /**
49     * Convert a Message to a MessageValue
50     * @param MessageSpecifier $m
51     * @return MessageValue
52     */
53    public function convertMessage( MessageSpecifier $m ) {
54        $mv = new MessageValue( $m->getKey() );
55        foreach ( $m->getParams() as $param ) {
56            $mv->params( $this->convertParam( $param ) );
57        }
58        return $mv;
59    }
60
61    /**
62     * Convert a Message parameter to a MessageParam
63     * @param array|string|int $param
64     * @return MessageParam
65     */
66    private function convertParam( $param ) {
67        if ( $param instanceof MessageSpecifier ) {
68            return new ScalarParam( ParamType::TEXT, $this->convertMessage( $param ) );
69        }
70        if ( !is_array( $param ) ) {
71            return new ScalarParam( ParamType::TEXT, $param );
72        }
73
74        if ( isset( $param['list'] ) && isset( $param['type'] ) ) {
75            $convertedElements = [];
76            foreach ( $param['list'] as $element ) {
77                $convertedElements[] = $this->convertParam( $element );
78            }
79            return new ListParam( $param['type'], $convertedElements );
80        }
81
82        foreach ( self::getTypes() as $type ) {
83            if ( $type !== ParamType::LIST && isset( $param[$type] ) ) {
84                return new ScalarParam( $type, $param[$type] );
85            }
86        }
87
88        throw new InvalidArgumentException( "Unrecognized Message param: " . json_encode( $param ) );
89    }
90
91    /**
92     * Convert a MessageValue to a Message
93     * @param MessageValue $mv
94     * @return Message
95     */
96    public function convertMessageValue( MessageValue $mv ) {
97        $m = $this->createMessage( $mv->getKey() );
98        foreach ( $mv->getParams() as $param ) {
99            $m->params( $this->convertMessageParam( $param ) );
100        }
101        return $m;
102    }
103
104    /**
105     * Convert a MessageParam to a Message parameter
106     * @param MessageParam $param
107     * @return array|string|int
108     */
109    private function convertMessageParam( MessageParam $param ) {
110        if ( $param instanceof ListParam ) {
111            $convertedElements = [];
112            foreach ( $param->getValue() as $element ) {
113                $convertedElements[] = $this->convertMessageParam( $element );
114            }
115            return Message::listParam( $convertedElements, $param->getListType() );
116        }
117        $value = $param->getValue();
118        if ( $value instanceof MessageValue ) {
119            $value = $this->convertMessageValue( $value );
120        }
121
122        if ( $param->getType() === ParamType::TEXT ) {
123            return $value;
124        }
125        return [ $param->getType() => $value ];
126    }
127
128}