本文整理汇总了PHP中SplFileObject::current方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::current方法的具体用法?PHP SplFileObject::current怎么用?PHP SplFileObject::current使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::current方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: readLine
/**
* @inheritDoc
*/
public function readLine()
{
if (!$this->file->eof()) {
$line = $this->file->current();
$this->file->next();
return json_decode($line);
}
return false;
}
示例2: read
/**
* {@inheritdoc}
*/
public function read()
{
if ($this->file->valid()) {
$this->file->seek($this->getContext()->getReadCount());
$line = rtrim($this->file->current(), PHP_EOL);
$this->getContext()->incrementReadCount();
return json_decode($line, true);
}
return null;
}
示例3: testCheckAnswerTask3
public function testCheckAnswerTask3()
{
$this->assertFileExists('data/export.csv');
$filecsv = new \SplFileObject('data/export.csv');
$filecsv->setFlags(\SplFileObject::READ_CSV);
$header = $filecsv->current();
$processor = new YieldProcessor(ReaderFactory::create('Db\\Product', include 'config/database.php'), WriterFactory::create('Stub'), function () {
});
foreach ($processor->processing() as $item) {
$filecsv->next();
$this->assertEquals($item, array_combine($header, $filecsv->current()));
}
}
示例4: export
protected function export($var, $return = false)
{
if ($var instanceof Closure) {
/* dump anonymous function in to plain code.*/
$ref = new ReflectionFunction($var);
$file = new SplFileObject($ref->getFileName());
$file->seek($ref->getStartLine() - 1);
$result = '';
while ($file->key() < $ref->getEndLine()) {
$result .= $file->current();
$file->next();
}
$begin = strpos($result, 'function');
$end = strrpos($result, '}');
$result = substr($result, $begin, $end - $begin + 1);
} elseif (is_object($var)) {
/* dump object with construct function. */
$result = 'new ' . get_class($var) . '(' . $this->export(get_object_vars($var), true) . ')';
} elseif (is_array($var)) {
/* dump array in plain array.*/
$array = array();
foreach ($var as $k => $v) {
$array[] = var_export($k, true) . ' => ' . $this->export($v, true);
}
$result = 'array(' . implode(', ', $array) . ')';
} else {
$result = var_export($var, true);
}
if (!$return) {
print $result;
}
return $result;
}
示例5: readFileByLines
/**
* 返回文件从X行到Y行的内容(支持php5、php4)
* @param String $filename 文件名
* @param integer $startLine 开始行
* @param integer $endLine 结束行
* @param string $method 方法
* @return array() 返回数组
*/
function readFileByLines($filename, $startLine = 1, $endLine = 50, $method = 'rb')
{
$content = array();
$count = $endLine - $startLine;
// 判断php版本(因为要用到SplFileObject,PHP>=5.1.0)
if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
$fp = new SplFileObject($filename, $method);
// 转到第N行, seek方法参数从0开始计数
$fp->seek($startLine - 1);
for ($i = 0; $i <= $count; ++$i) {
// current()获取当前行内容
$content[] = $fp->current();
// 下一行
$fp->next();
}
} else {
//PHP<5.1
$fp = fopen($filename, $method);
if (!$fp) {
return 'error:can not read file';
}
// 跳过前$startLine行
for ($i = 1; $i < $startLine; ++$i) {
fgets($fp);
}
// 读取文件行内容
for ($i; $i <= $endLine; ++$i) {
$content[] = fgets($fp);
}
fclose($fp);
}
// array_filter过滤:false,null,''
return array_filter($content);
}
示例6: getCode
/**
* Get Code from file
* @param $path
* @param $line
* @param $numLines
* @return array|null
*/
private function getCode($path, $line, $numLines)
{
if (empty($path) || empty($line) || !file_exists($path)) {
return NULL;
}
try {
// Get the number of lines in the file
$file = new \SplFileObject($path);
$file->seek(PHP_INT_MAX);
$totalLines = $file->key() + 1;
// Work out which lines we should fetch
$start = max($line - floor($numLines / 2), 1);
$end = $start + ($numLines - 1);
if ($end > $totalLines) {
$end = $totalLines;
$start = max($end - ($numLines - 1), 1);
}
// Get the code for this range
$code = array();
$file->seek($start - 1);
while ($file->key() < $end) {
$code[$file->key() + 1] = rtrim($file->current());
$file->next();
}
return $code;
} catch (RuntimeException $ex) {
return null;
}
}
示例7: modifyFileLine
function modifyFileLine($filename, $linenum, $lineText)
{
$fp = new SplFileObject($filename);
$fp->seek($linenum);
$line = $fp->current();
$fp->fseek($linenum, SEEK_CUR);
$fp->fwrite($lineText);
}
示例8: prepare
/**
* Write the compiled csv to disk and return the file name
*
* @param array $sortedHeaders An array of sorted headers
*
* @return string The csv filename where the data was written
*/
public function prepare($cacheFile, $sortedHeaders)
{
$csvFile = $cacheFile . '.csv';
$handle = fopen($csvFile, 'w');
// Generate a csv version of the multi-row headers to write to disk
$headerRows = [[], []];
foreach ($sortedHeaders as $idx => $header) {
if (!is_array($header)) {
$headerRows[0][] = $header;
$headerRows[1][] = '';
} else {
foreach ($header as $headerName => $subHeaders) {
$headerRows[0][] = $headerName;
$headerRows[1] = array_merge($headerRows[1], $subHeaders);
if (count($subHeaders) > 1) {
/**
* We need to insert empty cells for the first row of headers to account for the second row
* this acts as a faux horizontal cell merge in a csv file
* | Header 1 | <---- 2 extra cells ----> |
* | Sub 1 | Subheader 2 | Subheader 3 |
*/
$headerRows[0] = array_merge($headerRows[0], array_fill(0, count($subHeaders) - 1, ''));
}
}
}
}
fputcsv($handle, $headerRows[0]);
fputcsv($handle, $headerRows[1]);
// TODO: Track memory usage
$file = new \SplFileObject($cacheFile);
while (!$file->eof()) {
$csvRow = [];
$row = json_decode($file->current(), true);
if (!is_array($row)) {
// Invalid json data -- don't process this row
continue;
}
foreach ($sortedHeaders as $idx => $header) {
if (!is_array($header)) {
$csvRow[] = isset($row[$header]) ? $row[$header] : '';
} else {
// Multi-row header, so we need to set all values
$nestedHeaderName = array_keys($header)[0];
$nestedHeaders = $header[$nestedHeaderName];
foreach ($nestedHeaders as $nestedHeader) {
$csvRow[] = isset($row[$nestedHeaderName][$nestedHeader]) ? $row[$nestedHeaderName][$nestedHeader] : '';
}
}
}
fputcsv($handle, $csvRow);
$file->next();
}
$file = null;
// Get rid of the file handle that SplFileObject has on cache file
unlink($cacheFile);
fclose($handle);
return $csvFile;
}
示例9: valid
/**
* valid SplFileObjectでCSVフィルを読ませると最終行の空配列で返るのでそれの抑止
*
* @return bool
*/
public function valid()
{
$parentValid = parent::valid();
$var = parent::current();
if ($var === array(null)) {
return false;
}
return $parentValid;
}
示例10: assertSameContentAtLine
/**
* @param string $expectedContent
* @param integer $zeroBasedLineNumber
* @param boolean $trimLine
* @return void
*/
private function assertSameContentAtLine($expectedContent, $zeroBasedLineNumber, $trimLine = true)
{
$this->fileObject->seek($zeroBasedLineNumber);
$actualContent = $this->fileObject->current();
if ($trimLine) {
$actualContent = trim($actualContent);
}
$this->assertSame($expectedContent, $actualContent);
}
示例11: readFile
/**
* @param \SplFileObject $stream
*
* Reads the file to parse its lines
*/
public function readFile(\SplFileObject $stream)
{
$stream->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
while (!$stream->eof()) {
$this->parseLine($stream->current());
$stream->next();
}
$stream = null;
}
示例12: current
/**
* Fetch the current line as an associative array
*
* @return array
*/
public function current()
{
$data = trim(parent::current());
$parts = parse_url($data);
if (isset($parts['query'])) {
$parts['query'] = $this->_parseQuery($parts['query']);
}
return $parts;
}
示例13: evaluate
/**
* Evaluates the closure and the code in the reset of the file. This sets
* the code for the closure, the namespace it is declared in (if any) and
* any applicable namespace imports
* @param ReflectionFunction The reflected function of the closure
* @return String The code the closure runs
*/
private function evaluate(\ReflectionFunction $reflection)
{
$code = '';
$full = '';
$file = new \SplFileObject($reflection->getFileName());
while (!$file->eof()) {
if ($file->key() >= $reflection->getStartLine() - 1 && $file->key() < $reflection->getEndLine()) {
$code .= $file->current();
}
$full .= $file->current();
$file->next();
}
//@todo this assumes the function will be the only one on that line
$begin = strpos($code, 'function');
//@todo this assumes the } will be the only one on that line
$end = strrpos($code, '}');
$this->code = substr($code, $begin, $end - $begin + 1);
$this->extractDetail($full);
}
示例14: getValue
/**
* This method returns the element at the the specified index.
*
* @access public
* @param integer $index the index of the element
* @return mixed the element at the specified index
* @throws Throwable\InvalidArgument\Exception indicates that an index must be an integer
* @throws Throwable\OutOfBounds\Exception indicates that the index is out of bounds
*/
public function getValue($index)
{
if (!$this->hasIndex($index)) {
throw new Throwable\OutOfBounds\Exception('Unable to get element. Undefined index at ":index" specified', array(':index' => $index));
}
$this->reader->seek($index);
$line = $this->reader->current();
$value = unserialize($line);
return $value;
}
示例15: current
public function current()
{
if ($this->_columns) {
if (false === ($combined = @array_combine($this->_columns, parent::current()))) {
throw new Exception(sprintf("Column count did not match expected on line %d", parent::key()));
}
return $combined;
}
return parent::current();
}