本文整理汇总了PHP中SplFileObject::valid方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::valid方法的具体用法?PHP SplFileObject::valid怎么用?PHP SplFileObject::valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: __construct
/**
* File constructor.
* @param $uri String File path or URL
* @throws \ErrorException
* @throws \Exception
*/
public function __construct($uri)
{
try {
$this->messageFactory = new MessageFactory();
$file = new \SplFileObject($uri, 'rb');
while ($file->valid()) {
$raw_time = $file->fread(8);
if (!$raw_time) {
break;
}
$header = new Header($file->fread(6));
if ($header->magic !== 0xfe) {
throw new \ErrorException("Unexpected magic number ({$header->magic})");
}
$payload = $file->fread($header->length);
$raw_crc = $file->fread(2);
$entry = new Entry($raw_time, $header, $payload, $raw_crc);
$this->entries[] = $entry;
$this->messages[] = $this->messageFactory->build($entry);
}
if (!$this->hasEntries()) {
throw new \ErrorException('No entries found in file.');
}
$this->startTime = $this->entries[0]->time;
$this->endTime = end($this->entries)->time;
} catch (\Exception $ex) {
// echo $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine();
die;
}
}
示例3: valid
public function valid()
{
$current = $this->current();
if ($this->names) {
return count($current) == count($this->names);
}
return parent::valid();
}
示例4: valid
/**
* valid SplFileObjectでCSVフィルを読ませると最終行の空配列で返るのでそれの抑止
*
* @return bool
*/
public function valid()
{
$parentValid = parent::valid();
$var = parent::current();
if ($var === array(null)) {
return false;
}
return $parentValid;
}
示例5: generate
function generate($nb, $fixture = __DIR__ . '/fixtures/lorem.txt')
{
$lorems = new SplFileObject($fixture, 'a+');
$txt = '';
while ($nb > 0 && $lorems->valid()) {
$txt .= $lorems->current();
$lorems->next();
$nb--;
}
return $txt;
}
示例6: getData
public function getData()
{
try {
$file = new SplFileObject($this->tmp_name, 'rb');
$data = null;
for ($file; $file->valid(); $file->next()) {
$data .= $file->current();
}
return $data;
} catch (RuntimeException $e) {
}
return null;
}
示例7: getRaw
/**
* Get raw image data from the SplFileObject.
*
* @return string
* String representation of the raw image binary data
*/
public function getRaw()
{
if (!is_null($this->raw)) {
return $this->raw;
}
// Save our current position
$position = $this->object->ftell();
$this->object->rewind();
$raw = '';
while ($this->object->valid()) {
$raw .= $this->object->fgets();
}
// Go back to our original position
$this->object->fseek($position);
return $this->raw = $raw;
}
示例8: parseFileObject
/**
* @param \SplFileObject $fileObject
*
* @return Statement
*/
protected function parseFileObject(\SplFileObject $fileObject)
{
$this->statement = $this->getStatementClass();
foreach ($fileObject as $line) {
if ($fileObject->valid()) {
switch ($this->getLineType($line)) {
case self::LINE_TYPE_STATEMENT:
$this->parseStatementLine($line);
break;
case self::LINE_TYPE_TRANSACTION:
$transaction = $this->parseTransactionLine($line);
$this->statement->addTransaction($transaction);
break;
}
}
}
return $this->statement;
}
示例9: next
/**
* Move forward to next element
*/
public function next()
{
if (null === $this->_file) {
$this->_openFile();
}
if ($this->_file) {
$this->_key = $this->_key + 1;
while ($this->_file->valid()) {
$this->_file->next();
$this->_filepos = $this->_file->ftell();
if ($this->_accept()) {
$this->_valid = true;
return;
}
}
}
$this->_valid = false;
}
示例10: parseFile
public function parseFile($logFile, &$startId, $finish = false)
{
if (!$logFile || !file_exists($logFile)) {
return $this->setError('Log file error');
}
try {
$file = new SplFileObject($logFile);
$file->seek(!$startId ? 0 : $startId - 1);
$counter = 0;
$items = array();
$item = null;
while ($file->valid()) {
$row = trim($file->current());
$file->next();
if (!$row) {
continue;
}
$item = Mage::getModel('mpbackup/backup_progress_item')->parse($row, $item);
$items[] = $item;
$counter += $item->getLength();
if (!$finish && $counter > Mageplace_Backup_Helper_Const::BACKUP_PROCESS_REQUEST_PERIOD * Mageplace_Backup_Helper_Const::BACKUP_PROCESS_RESPONSE_SIZE) {
break;
}
}
$startId = $file->key();
} catch (Exception $e) {
Mage::logException($e);
return $this->setError($e->getMessage());
}
if (empty($items)) {
if ($finish) {
return $this->setError('Log is empty (' . print_r($items, true) . ') and log process is finished');
} else {
return $this->setData(self::TEXT, self::TEXT_TREE_POINT)->setError(print_r($items, true));
}
}
return $this->setData(self::ITEMS, $items);
}
示例11: read
/**
* Read a key from the cache
*
* @param string $key Identifier for the data
* @return mixed The cached data, or false if the data doesn't exist, has
* expired, or if there was an error fetching it
*/
public function read($key)
{
$key = $this->_key($key);
if (!$this->_init || $this->_setKey($key) === false) {
return false;
}
if ($this->_config['lock']) {
$this->_File->flock(LOCK_SH);
}
$this->_File->rewind();
$time = time();
$cachetime = (int) $this->_File->current();
if ($cachetime !== false && ($cachetime < $time || $time + $this->_config['duration'] < $cachetime)) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}
return false;
}
$data = '';
$this->_File->next();
while ($this->_File->valid()) {
$data .= $this->_File->current();
$this->_File->next();
}
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}
$data = trim($data);
if ($data !== '' && !empty($this->_config['serialize'])) {
if ($this->_config['isWindows']) {
$data = str_replace('\\\\\\\\', '\\', $data);
}
$data = unserialize((string) $data);
}
return $data;
}
示例12: __invoke
public function __invoke($csvFilePath, $checkLines = 2)
{
$file = new \SplFileObject($csvFilePath);
$delimiters = [',', '\\t', ';', '|', ':'];
$results = array();
$i = 0;
while ($file->valid() && $i <= $checkLines) {
$line = $file->fgets();
foreach ($delimiters as $delimiter) {
$regExp = '/[' . $delimiter . ']/';
$fields = preg_split($regExp, $line);
if (count($fields) > 1) {
if (!empty($results[$delimiter])) {
$results[$delimiter]++;
} else {
$results[$delimiter] = 1;
}
}
}
$i++;
}
$results = array_keys($results, max($results));
return $results[0];
}
示例13: read
public function read($key)
{
if (!$this->is_init || $this->_openFile($key) === false) {
return false;
}
if ($this->settings['lock']) {
$this->file->flock(LOCK_SH);
}
$this->file->rewind();
$time = time();
$cachetime = intval($this->file->current());
//check for expiry
if ($cachetime !== false && ($cachetime < $time || $time + $this->settings['cache_duration'] < $cachetime)) {
if ($this->settings['lock']) {
$this->file->flock(LOCK_UN);
}
return false;
}
$data = '';
$this->file->next();
while ($this->file->valid()) {
$data .= $this->file->current();
$this->file->next();
}
if ($this->settings['lock']) {
$this->file->flock(LOCK_UN);
}
$data = trim($data);
if ($data !== '' && !empty($this->settings['serialize'])) {
if ($this->settings['is_windows']) {
$data = str_replace('\\\\\\\\', '\\', $data);
}
$data = json_decode((string) $data, TRUE);
}
return $data;
}
示例14: valid
/**
* {@inheritdoc}
*/
public function valid()
{
return $this->file->valid();
}
示例15: valid
/**
* (PHP 5 >= 5.0.0)<br/>
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
if (!is_null($this->line_end_position)) {
$line = $this->key();
if ($line > $this->line_end_position) {
return false;
}
}
return parent::valid();
}