/** * @since 1.17 * @param string|string[]|MessageSpecifier $key Message key, or array of * message keys to try and use the first non-empty message for, or a * MessageSpecifier to copy from. * @param array $params Message parameters. * @param Language $language [optional] Language to use (defaults to current user language). * @throws InvalidArgumentException */ public function __construct($key, $params = [], Language $language = null) { if ($key instanceof MessageSpecifier) { if ($params) { throw new InvalidArgumentException('$params must be empty if $key is a MessageSpecifier'); } $params = $key->getParams(); $key = $key->getKey(); } if (!is_string($key) && !is_array($key)) { throw new InvalidArgumentException('$key must be a string or an array'); } $this->keysToTry = (array) $key; if (empty($this->keysToTry)) { throw new InvalidArgumentException('$key must not be an empty list'); } $this->key = reset($this->keysToTry); $this->parameters = array_values($params); // User language is only resolved in getLanguage(). This helps preserve the // semantic intent of "user language" across serialize() and unserialize(). $this->language = $language ?: false; }
/** * @since 1.17 * * @param string|string[]|MessageSpecifier $key Message key, or array of * message keys to try and use the first non-empty message for, or a * MessageSpecifier to copy from. * @param array $params Message parameters. * @param Language $language Optional language of the message, defaults to $wgLang. * * @throws InvalidArgumentException */ public function __construct($key, $params = array(), Language $language = null) { global $wgLang; if ($key instanceof MessageSpecifier) { if ($params) { throw new InvalidArgumentException('$params must be empty if $key is a MessageSpecifier'); } $params = $key->getParams(); $key = $key->getKey(); } if (!is_string($key) && !is_array($key)) { throw new InvalidArgumentException('$key must be a string or an array'); } $this->keysToTry = (array) $key; if (empty($this->keysToTry)) { throw new InvalidArgumentException('$key must not be an empty list'); } $this->key = reset($this->keysToTry); $this->parameters = array_values($params); $this->language = $language ?: $wgLang; }
/** * Transform a MessageSpecifier or a primitive value used interchangeably with * specifiers (a message key string, or a key + params array) into a proper Message * @param string|array|MessageSpecifier $value * @return Message * @throws InvalidArgumentException * @since 1.27 */ public static function newFromSpecifier($value) { if ($value instanceof RawMessage) { $message = new RawMessage($value->getKey(), $value->getParams()); } elseif ($value instanceof MessageSpecifier) { $message = new Message($value); } elseif (is_array($value)) { $key = array_shift($value); $message = new Message($key, $value); } elseif (is_string($value)) { $message = new Message($value); } else { throw new InvalidArgumentException(__METHOD__ . ': invalid argument type ' . gettype($value)); } return $message; }