本文整理汇总了PHP中SplFileObject::setCsvControl方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::setCsvControl方法的具体用法?PHP SplFileObject::setCsvControl怎么用?PHP SplFileObject::setCsvControl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::setCsvControl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: open
/**
* Open stream from a source
* A source can be anything: for instance a db, a file, or a socket
*
* @param String $path
* @return void
* @throws \Palmabit\Library\Exceptions\CannotOpenFileException
*/
public function open($path)
{
$this->convertToLF($path);
$this->spl_file_object = new SplFileObject($path);
$this->spl_file_object->setCsvControl($this->delimiter);
$this->columns_name = $this->spl_file_object->fgetcsv();
}
示例2: __construct
/**
* @param \SplFileObject $file
* @param string $delimiter
* @param string $enclosure
* @param string $escape
*/
public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
ini_set('auto_detect_line_endings', true);
$this->file = $file;
$this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD | \SplFileObject::DROP_NEW_LINE);
$this->file->setCsvControl($delimiter, $enclosure, $escape);
}
示例3: getFile
/**
* @return \SplFileObject
*/
protected function getFile()
{
if (!$this->file instanceof \SplFileObject) {
$this->file = $this->fileInfo->openFile();
$this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::DROP_NEW_LINE);
$this->file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
if ($this->firstLineIsHeader && !$this->header) {
$this->header = $this->file->fgetcsv();
}
}
return $this->file;
}
示例4: __construct
public function __construct($fileName)
{
parent::__construct();
$this->fileName = $fileName;
try {
$this->fileHandle = new \SplFileObject($fileName, 'c+');
$this->fileHandle->setFlags(\SplFileObject::READ_CSV);
$this->fileHandle->setCsvControl(self::CSV_DELIMITER, self::CSV_SEPARATOR);
} catch (\Exception $exception) {
throw new PinqException('Invalid cache file: %s is not readable with the message, "%s"', $fileName, $exception->getMessage());
}
$this->fileName = $fileName;
}
示例5: loadResource
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
{
$messages = array();
try {
$file = new \SplFileObject($resource, 'rb');
} catch (\RuntimeException $e) {
throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
}
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
foreach ($file as $data) {
if (substr($data[0], 0, 1) === '#') {
continue;
}
if (!isset($data[1])) {
continue;
}
if (count($data) == 2) {
$messages[$data[0]] = $data[1];
} else {
continue;
}
}
return $messages;
}
示例6: load
/**
* {@inheritdoc}
*
* @api
*/
public function load($resource, $locale, $domain = 'messages')
{
$messages = array();
try {
$file = new \SplFileObject($resource, 'rb');
} catch (\RuntimeException $e) {
throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource));
}
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
foreach ($file as $data) {
if (substr($data[0], 0, 1) === '#') {
continue;
}
if (!isset($data[1])) {
continue;
}
if (count($data) == 2) {
$messages[$data[0]] = $data[1];
} else {
continue;
}
}
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例7: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = array();
try {
$file = new \SplFileObject($resource, 'rb');
} catch (\RuntimeException $e) {
throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
}
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
foreach ($file as $data) {
if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === count($data)) {
$messages[$data[0]] = $data[1];
}
}
$catalogue = parent::load($messages, $locale, $domain);
if (class_exists('Symfony\\Component\\Config\\Resource\\FileResource')) {
$catalogue->addResource(new FileResource($resource));
}
return $catalogue;
}
示例8: read
/**
* {@inheritdoc}
*/
public function read()
{
if (null === $this->csv) {
if (mime_content_type($this->filePath) === 'application/zip') {
$this->extractZipArchive();
}
$this->csv = new \SplFileObject($this->filePath);
$this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
$this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$this->fieldNames = $this->csv->fgetcsv();
}
$data = $this->csv->fgetcsv();
if (false !== $data) {
if ($data === array(null) || $data === null) {
return null;
}
if ($this->stepExecution) {
$this->stepExecution->incrementSummaryInfo('read');
}
if (count($this->fieldNames) !== count($data)) {
throw new InvalidItemException('pim_base_connector.steps.csv_reader.invalid_item_columns_count', $data, array('%totalColumnsCount%' => count($this->fieldNames), '%itemColumnsCount%' => count($data), '%csvPath%' => $this->csv->getRealPath(), '%lineno%' => $this->csv->key()));
}
$data = array_combine($this->fieldNames, $data);
} else {
throw new \RuntimeException('An error occured while reading the csv.');
}
return $data;
}
示例9: insert
public function insert($file, array $callback, $scan_info)
{
$class = $callback[0];
$method = $callback[1];
$class = Model::factory($class);
$this->_handle = fopen($file, 'r');
$headers = fgetcsv($this->_handle, $file);
$scan_data = array();
$file = new SplFileObject($file);
$file->setFlags(SplFileObject::SKIP_EMPTY);
$file->setFlags(SplFileObject::READ_AHEAD);
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(",", '"', "\"");
$c = 0;
foreach ($file as $row) {
$c++;
if (count($row) === count($headers)) {
$scan_data[] = array_combine($headers, $row);
$row = array();
}
if ($c % $this->insert_threshold == 0) {
Logger::msg('info', array('message' => 'flushing ' . $this->insert_threshold . ' rows', "class" => $callback[0], "method" => $callback[1], 'rows_inserted' => $c));
Logger::msg('info', array('memory_usage' => $this->file_size(memory_get_usage())));
$flush = $class->{$method}($scan_data, $scan_info);
$scan_data = array();
}
}
$flush = $class->{$method}($scan_data, $scan_info);
$scan_data = array();
Logger::msg('info', array('memory_usage' => $this->file_size(memory_get_usage())));
return $c;
}
示例10: exportedFileOfShouldContain
/**
* @param string $code
* @param PyStringNode $csv
*
* @Then /^exported file of "([^"]*)" should contain:$/
*
* @throws ExpectationException
* @throws \Exception
*/
public function exportedFileOfShouldContain($code, PyStringNode $csv)
{
$config = $this->getFixturesContext()->getJobInstance($code)->getRawConfiguration();
$path = $this->getMainContext()->getSubcontext('job')->getJobInstancePath($code);
if (!is_file($path)) {
throw $this->getMainContext()->createExpectationException(sprintf('File "%s" doesn\'t exist', $path));
}
$delimiter = isset($config['delimiter']) ? $config['delimiter'] : ';';
$enclosure = isset($config['enclosure']) ? $config['enclosure'] : '"';
$escape = isset($config['escape']) ? $config['escape'] : '\\';
$csvFile = new \SplFileObject($path);
$csvFile->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
$csvFile->setCsvControl($delimiter, $enclosure, $escape);
$expectedLines = [];
foreach ($csv->getLines() as $line) {
if (!empty($line)) {
$expectedLines[] = explode($delimiter, str_replace($enclosure, '', $line));
}
}
$actualLines = [];
while ($data = $csvFile->fgetcsv()) {
if (!empty($data)) {
$actualLines[] = array_map(function ($item) use($enclosure) {
return str_replace($enclosure, '', $item);
}, $data);
}
}
$expectedCount = count($expectedLines);
$actualCount = count($actualLines);
assertSame($expectedCount, $actualCount, sprintf('Expecting to see %d rows, found %d', $expectedCount, $actualCount));
if (md5(json_encode($actualLines[0])) !== md5(json_encode($expectedLines[0]))) {
throw new \Exception(sprintf('Header in the file %s does not match expected one: %s', $path, implode(' | ', $actualLines[0])));
}
unset($actualLines[0]);
unset($expectedLines[0]);
foreach ($expectedLines as $expectedLine) {
$originalExpectedLine = $expectedLine;
$found = false;
foreach ($actualLines as $index => $actualLine) {
// Order of columns is not ensured
// Sorting the line values allows to have two identical lines
// with values in different orders
sort($expectedLine);
sort($actualLine);
// Same thing for the rows
// Order of the rows is not reliable
// So we generate a hash for the current line and ensured that
// the generated file contains a line with the same hash
if (md5(json_encode($actualLine)) === md5(json_encode($expectedLine))) {
$found = true;
// Unset line to prevent comparing it twice
unset($actualLines[$index]);
break;
}
}
if (!$found) {
throw new \Exception(sprintf('Could not find a line containing "%s" in %s', implode(' | ', $originalExpectedLine), $path));
}
}
}
示例11: readRecords
/**
* Reads csv records
*
* @param string $fileName
* @param int $position
* @param int $step
* @return array
* @throws \Exception
*/
public function readRecords($fileName, $position, $step)
{
$tempFileName = '';
if (file_exists($fileName)) {
$tempFileName = $this->uploadPathProvider->getRealPath(md5(microtime() . '.csv'));
file_put_contents($tempFileName, file_get_contents($fileName));
$fileName = $tempFileName;
}
$file = new \SplFileObject($fileName);
$file->setCsvControl(";", '"');
$file->setFlags(\SplFileObject::READ_CSV);
$columnNames = $this->getColumnNames($file);
//moves the file pointer to a certain line
// +1 to ignore the first line of the file
$file->seek($position + 1);
$readRows = [];
$data = [];
for ($i = 1; $i <= $step; $i++) {
$row = $file->current();
if ($this->isInvalidRecord($row)) {
break;
}
foreach ($columnNames as $key => $name) {
$data[$name] = isset($row[$key]) ? $row[$key] : '';
}
$readRows[] = $data;
// Move the pointer to the next line
$file->next();
}
unlink($tempFileName);
return $this->toUtf8($readRows);
}
示例12: initializeRead
/**
* Initialize read process by extracting zip if needed, setting CSV options
* and settings field names.
*/
protected function initializeRead()
{
if (mime_content_type($this->filePath) === 'application/zip') {
$this->extractZipArchive();
}
$this->csv = new \SplFileObject($this->filePath);
$this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
$this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$this->fieldNames = $this->csv->fgetcsv();
}
示例13: initializeRead
/**
* Initialize read process by extracting zip if needed, setting CSV options
* and settings field names.
*/
protected function initializeRead()
{
// TODO mime_content_type is deprecated, use Symfony\Component\HttpFoundation\File\MimeTypeMimeTypeGuesser?
if ('application/zip' === mime_content_type($this->filePath)) {
$this->extractZipArchive();
}
$this->csv = new \SplFileObject($this->filePath);
$this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
$this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$this->fieldNames = $this->csv->fgetcsv();
}
示例14: getRows
public function getRows($chemin)
{
//jLog::dump($chemin);
$csv = new SplFileObject($chemin, 'r');
$csv->setFlags(SplFileObject::READ_CSV);
$csv->setCsvControl(',');
$rows = array();
$i = 0;
foreach ($csv as $ligne) {
$rows[$i] = $ligne;
$i++;
}
return $rows;
}
示例15: all
/**
*
* @return array
*/
private function all($resource)
{
try {
$file = new \SplFileObject($resource, 'rb');
} catch (\RuntimeException $e) {
throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource));
}
$file->setFlags(\SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV);
$file->setCsvControl(';');
$lines = array();
// iterate over the file's rows
// fgets increments file descriptor to next line
while ($data = $file->fgetcsv()) {
$lines[] = $data;
}
return $lines;
}