POMO_StringReader
云策文档标注
概述
POMO_StringReader 是一个 WordPress 核心类,继承自 POMO_Reader,提供类似文件操作的方法来处理字符串而非物理文件。它主要用于国际化(i18n)和本地化(l10n)场景中的字符串读取。
关键要点
- 继承自 POMO_Reader,用于模拟文件流操作,但操作对象是字符串。
- 提供核心方法如 read()、seekto()、length() 和 read_all(),支持按字节读取、定位和获取字符串长度。
- 包含 PHP5 构造函数 __construct() 和已弃用的 PHP4 构造函数 POMO_StringReader(),后者在 WordPress 5.4.0 后不建议使用。
- 内部使用 _str 属性存储字符串,_pos 属性跟踪当前位置,确保读取和定位操作的安全边界。
注意事项
- PHP4 构造函数 POMO_StringReader() 已弃用,应使用 __construct() 替代。
- 该类主要用于 POMO(PHP Object Model for i18n)系统,常见于翻译文件处理。
原文内容
Provides file-like methods for manipulating a string instead of a physical file.
Methods
| Name | Description |
|---|---|
| POMO_StringReader::__construct | PHP5 constructor. |
| POMO_StringReader::length | – |
| POMO_StringReader::POMO_StringReader | PHP4 constructor. — deprecated |
| POMO_StringReader::read | – |
| POMO_StringReader::read_all | – |
| POMO_StringReader::seekto | – |
Source
class POMO_StringReader extends POMO_Reader {
public $_str = '';
/**
* PHP5 constructor.
*/
public function __construct( $str = '' ) {
parent::__construct();
$this->_str = $str;
$this->_pos = 0;
}
/**
* PHP4 constructor.
*
* @deprecated 5.4.0 Use __construct() instead.
*
* @see POMO_StringReader::__construct()
*/
public function POMO_StringReader( $str = '' ) {
_deprecated_constructor( self::class, '5.4.0', static::class );
self::__construct( $str );
}
/**
* @param string $bytes
* @return string
*/
public function read( $bytes ) {
$data = $this->substr( $this->_str, $this->_pos, $bytes );
$this->_pos += $bytes;
if ( $this->strlen( $this->_str ) < $this->_pos ) {
$this->_pos = $this->strlen( $this->_str );
}
return $data;
}
/**
* @param int $pos
* @return int
*/
public function seekto( $pos ) {
$this->_pos = $pos;
if ( $this->strlen( $this->_str ) < $this->_pos ) {
$this->_pos = $this->strlen( $this->_str );
}
return $this->_pos;
}
/**
* @return int
*/
public function length() {
return $this->strlen( $this->_str );
}
/**
* @return string
*/
public function read_all() {
return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) );
}
}