/** * 读取消息文本 * * // 读取message/text.php中的username * $username = Message::load('text', 'username'); * * @param string $file 文件名 * @param string $path 键名 * @param mixed $default 键名不存在时返回默认值 * @return string|array 内容,如果$path为空的话,就返回完整数组内容 */ public static function load($file, $path = null, $default = null) { static $messages; if (!isset($messages[$file])) { $messages[$file] = []; $files = []; foreach (self::$_messagePaths as $includePath) { if (is_file($includePath . $file . self::$ext)) { $files[] = $includePath . $file . self::$ext; } } if (!empty($files)) { foreach ($files as $f) { $messages[$file] = Arr::merge($messages[$file], Base::load($f)); } } } if (null === $path) { // 返回完整的数组 return $messages[$file]; } else { // 返回指定的键名 return Arr::path($messages[$file], $path, $default); } }
/** * 检验[Arr::setPath]功能 */ public function testSetPath() { $arr = []; Arr::setPath($arr, 'test1.test1', 'TEST1'); $this->assertEquals('TEST1', Arr::path($arr, 'test1.test1')); $arr = []; Arr::setPath($arr, ['test2', 'test2'], 'TEST2'); $this->assertEquals('TEST2', Arr::path($arr, ['test2', 'test2'])); }
/** * 读取或者设置post数据 * * @param mixed $key 键名 * @param string $value 值 * @return mixed|$this */ public function post($key = null, $value = null) { if (is_array($key)) { $this->_post = $key; return $this; } if (null === $key) { return $this->_post; } elseif (null === $value) { return Arr::path($this->_post, $key); } $this->_post[$key] = $value; return $this; }