包 | XS.util |
---|---|
继承关系 | class XSJsonDataSource » XSDataSource |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
__construct() | 构造函数 | XSDataSource |
getCharset() | 取得数据源的准确字符集 | XSDataSource |
getData() | 从数据源中提取一条数据 | XSDataSource |
instance() | 取得数据源对象实例 | XSDataSource |
名称 | 描述 | 定义于 |
---|---|---|
deinit() | XSJsonDataSource | |
getDataList() | XSJsonDataSource | |
init() | XSJsonDataSource |
protected void deinit()
|
protected function deinit()
{
if ($this->fd) {
fclose($this->fd);
$this->fd = null;
}
}
protected void getDataList()
|
protected function getDataList()
{
// read line (check to timeout?)
$line = '';
while (true) {
$buf = fgets($this->fd, 8192);
if ($buf === false || strlen($buf) === 0) {
break;
}
$line .= $buf;
if (strlen($buf) < 8191 || substr($buf, - 1, 1) === "\n") {
break;
}
}
// empty line (end of file)
if (empty($line)) {
if ($this->inCli) {
echo "INFO: reach end of the file, total lines: " . $this->line . "\n";
}
return false;
}
// try to decode the line
$this->line++;
$line = rtrim($line, "\r\n");
if (strlen($line) === 0) {
if ($this->inCli) {
echo "WARNING: empty line #" . $this->line . "\n";
}
$this->invalidLines++;
return $this->getDataList();
}
$item = json_decode($line, true);
if (!is_array($item) || count($item) === 0) {
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$error = ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_CTRL_CHAR:
$error = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = ' - Syntax error, malformed JSON';
break;
default :
$error = (count($item) === 0 ? ' - Empty array' : '');
break;
}
if ($this->inCli) {
echo "WARNING: invalid line #" . $this->line . $error . "\n";
}
$this->invalidLines++;
return $this->getDataList();
}
return array($item);
}
protected void init()
|
protected function init()
{
$file = $this->arg;
if (empty($file) && $this->inCli) {
echo "WARNING: input file not specified, read data from <STDIN>\n";
$file = 'php://stdin';
}
if (!($this->fd = fopen($file, 'r'))) {
throw new XSException("Can not open input file: '$file'");
}
$this->line = 0;
}
包 | XS.util |
---|---|
继承关系 | class XSJsonDataSource » XSDataSource |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
__construct() | 构造函数 | XSDataSource |
getCharset() | 取得数据源的准确字符集 | XSDataSource |
getData() | 从数据源中提取一条数据 | XSDataSource |
instance() | 取得数据源对象实例 | XSDataSource |
名称 | 描述 | 定义于 |
---|---|---|
deinit() | XSJsonDataSource | |
getDataList() | XSJsonDataSource | |
init() | XSJsonDataSource |
protected void deinit()
|
protected function deinit()
{
if ($this->fd) {
fclose($this->fd);
$this->fd = null;
}
}
protected void getDataList()
|
protected function getDataList()
{
// read line (check to timeout?)
$line = '';
while (true) {
$buf = fgets($this->fd, 8192);
if ($buf === false || strlen($buf) === 0) {
break;
}
$line .= $buf;
if (strlen($buf) < 8191 || substr($buf, - 1, 1) === "\n") {
break;
}
}
// empty line (end of file)
if (empty($line)) {
if ($this->inCli) {
echo "INFO: reach end of the file, total lines: " . $this->line . "\n";
}
return false;
}
// try to decode the line
$this->line++;
$line = rtrim($line, "\r\n");
if (strlen($line) === 0) {
if ($this->inCli) {
echo "WARNING: empty line #" . $this->line . "\n";
}
$this->invalidLines++;
return $this->getDataList();
}
$item = json_decode($line, true);
if (!is_array($item) || count($item) === 0) {
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$error = ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_CTRL_CHAR:
$error = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = ' - Syntax error, malformed JSON';
break;
default :
$error = (count($item) === 0 ? ' - Empty array' : '');
break;
}
if ($this->inCli) {
echo "WARNING: invalid line #" . $this->line . $error . "\n";
}
$this->invalidLines++;
return $this->getDataList();
}
return array($item);
}
protected void init()
|
protected function init()
{
$file = $this->arg;
if (empty($file) && $this->inCli) {
echo "WARNING: input file not specified, read data from <STDIN>\n";
$file = 'php://stdin';
}
if (!($this->fd = fopen($file, 'r'))) {
throw new XSException("Can not open input file: '$file'");
}
$this->line = 0;
}
留下一条评论吧!
请到论坛 登录 后刷新本页面!