Exemplo n.º 1
0
 /**
  * @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);
 }
Exemplo n.º 2
0
 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;
 }
 /**
  * @inheritDoc
  */
 public function openFile($filePath = "/", $openMode = "a+")
 {
     try {
         $this->file = new \SplFileObject($filePath, $openMode);
         $this->file->setFlags(\SplFileObject::READ_AHEAD);
         $this->file->setFlags(\SplFileObject::SKIP_EMPTY);
     } catch (\Exception $e) {
         throw new FileException($e->getMessage(), $e->getCode(), $e);
     }
 }
 /**
  * @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;
 }
Exemplo n.º 5
0
 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;
 }
 /**
  * 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);
 }
Exemplo n.º 7
0
 /**
  * {@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;
 }
Exemplo n.º 8
0
function buildQuadTree($filename)
{
    //  Set the centrepoint of our QuadTree at 0.0 Longitude, 0.0 Latitude
    $centrePoint = new \QuadTrees\QuadTreeXYPoint(0.0, 0.0);
    //  Set the bounding box to the entire globe
    $quadTreeBoundingBox = new \QuadTrees\QuadTreeBoundingBox($centrePoint, 360, 180);
    //  Create our QuadTree
    $quadTree = new \QuadTrees\QuadTree($quadTreeBoundingBox);
    echo "Loading cities: ";
    $cityFile = new \SplFileObject($filename);
    $cityFile->setFlags(\SplFileObject::READ_CSV | \SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
    //  Populate our new QuadTree with cities from around the world
    $cityCount = 0;
    foreach ($cityFile as $cityData) {
        if (!empty($cityData[0])) {
            if ($cityCount % 1000 == 0) {
                echo '.';
            }
            $city = new cityPoint($cityData[0], $cityData[1], $cityData[3], $cityData[2]);
            $quadTree->insert($city);
            ++$cityCount;
        }
    }
    echo PHP_EOL, "Added {$cityCount} cities to QuadTree", PHP_EOL;
    return $quadTree;
}
Exemplo n.º 9
0
 /**
  * {@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;
 }
Exemplo n.º 10
0
 /**
  * {@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;
 }
Exemplo n.º 11
0
 /**
  * {@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;
 }
 /**
  * @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));
         }
     }
 }
Exemplo n.º 13
0
function buildQuadTree($filename)
{
    //  Set the centrepoint of our QuadTree at 0.0 Longitude, 0.0 Latitude
    $centrePoint = new \QuadTrees\QuadTreeXYPoint(0.0, 0.0);
    //  Set the bounding box to the entire globe
    $quadTreeBoundingBox = new \QuadTrees\QuadTreeBoundingBox($centrePoint, 360, 180);
    //  Create our QuadTree
    $quadTree = new \QuadTrees\QuadTree($quadTreeBoundingBox);
    echo "Loading lunarLandings: ";
    $landingFile = new \SplFileObject($filename);
    $landingFile->setFlags(\SplFileObject::READ_CSV | \SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
    //  Populate our new QuadTree with lunarLandings from around the world
    $lunarLandingCount = 0;
    foreach ($landingFile as $lunarLandingData) {
        if (!empty($lunarLandingData[0])) {
            if ($lunarLandingCount % 10 == 0) {
                echo '.';
            }
            $lunarLanding = new lunarLandingPoint(trim($lunarLandingData[0]), trim($lunarLandingData[1]), trim($lunarLandingData[2]), trim($lunarLandingData[4]), trim($lunarLandingData[3]));
            $quadTree->insert($lunarLanding);
            ++$lunarLandingCount;
        }
    }
    echo PHP_EOL, "Added {$lunarLandingCount} lunar landing details to QuadTree", PHP_EOL;
    return $quadTree;
}
Exemplo n.º 14
0
 /**
  * Import action
  *
  * @return Response
  *
  * @throws AccessDeniedException If access is not granted
  */
 public function importAction(Request $request)
 {
     if (false === $this->admin->isGranted('IMPORT')) {
         throw new AccessDeniedException();
     }
     $defaultData = array();
     $form = $this->createFormBuilder($defaultData)->add('file', 'file')->getForm();
     $form->handleRequest($request);
     $data = array();
     if ($form->isValid()) {
         $fs = new Filesystem();
         try {
             $fs->mkdir('/tmp/import');
         } catch (IOExceptionInterface $e) {
             echo "An error occurred while creating your directory at " . $e->getPath();
         }
         $form['file']->getData()->move('/tmp/import', 'data.csv');
         $file = new \SplFileObject('/tmp/import/data.csv');
         // this must be done to import CSVs where one of the data-field has CRs within!
         $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD);
         $reader = new Reader\CsvReader($file, ';');
         $reader->setHeaderRowNumber(0);
         if ($request->request->has('btn_preview')) {
             foreach ($reader as $row) {
                 $data[] = $row;
             }
         } else {
             // Set Database into "nonchecking Foreign Keys"
             $em = $this->getDoctrine()->getManager();
             $em->getConnection()->exec("SET FOREIGN_KEY_CHECKS=0;");
             $writer = new DoctrineWriter($em, "AppBundle:Partner");
             $writer->setTruncate(false);
             $metadata = $em->getClassMetaData("AppBundle:Partner");
             $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
             $mapping = new Step\MappingStep();
             $mapping->map('[country_code]', '[country]');
             $mapping->map('[category]', '[partnerCategory]');
             $converter = new StringToObjectConverter($em->getRepository("AppBundle:PartnerCategory"), 'name');
             $valueConverterStep = new Step\ValueConverterStep();
             $valueConverterStep->add('[partnerCategory]', $converter);
             //                $converter = new CustomValueConverter();
             //                $converterStep = new Step\ConverterStep();
             //                $converterStep->add($converter);
             $workflow = new Workflow\StepAggregator($reader);
             $workflow->addStep($mapping);
             $workflow->addStep($valueConverterStep);
             //                $workflow->addStep($converterStep);
             $workflow->addWriter($writer);
             $workflow->process();
             // Resetting Database Check Status
             $em->getConnection()->exec("SET FOREIGN_KEY_CHECKS=1;");
         }
     }
     $formView = $form->createView();
     // set the theme for the current Admin Form
     $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
     return $this->render('CRUD/import_partner.html.twig', array('action' => 'import', 'form' => $formView, 'data' => $data));
 }
Exemplo n.º 15
0
 /**
  * @param $tmp_file
  * @return \SplFileObject
  */
 public function getFileObject($tmp_file)
 {
     if (!$tmp_file) {
         return array();
     }
     $file = new \SplFileObject($tmp_file);
     $file->setFlags(\SplFileObject::READ_CSV);
     return $file;
 }
Exemplo n.º 16
0
 /**
  * @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;
 }
 public function actionIndex()
 {
     Yii::import("application.models.*");
     //get the latest idle cron job
     /* @var $latestidle JobQueue*/
     $latestidle = JobQueue::model()->findByAttributes(array("status" => JobQueue::$JOBQUEUE_STATUS_IDLE));
     if (!$latestidle) {
         echo "No file queued";
         die;
     }
     //set status to on-going
     $latestidle->status = JobQueue::$JOBQUEUE_STATUS_ON_GOING;
     $latestidle->save(false);
     //retrieve file
     $queueFile = new SplFileObject($latestidle->filename);
     //read file
     $queueFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
     $queueFile->next();
     // Total_records ,
     $queueFile->seek(PHP_INT_MAX);
     $linesTotal = $queueFile->key();
     $latestidle->total_records = $linesTotal;
     $latestidle->save(false);
     $index = 0;
     foreach ($queueFile as $currentLine) {
         //iterate content
         if ($queueFile->key() === 0) {
             continue;
         }
         //TODO: processed_record
         $latestidle->processed_record = ++$index;
         $latestidle->save(false);
         $currentMobile = $currentLine[0];
         $newBlackListedmobile = new BlackListedMobile();
         //cleaning time
         $currentMobile = trim($currentMobile);
         $currentMobile = rtrim($currentMobile);
         $currentMobile = ltrim($currentMobile);
         $newBlackListedmobile->mobile_number = $currentMobile;
         $newBlackListedmobile->queue_id = $latestidle->queue_id;
         //set queueid
         if ($newBlackListedmobile->save()) {
             //save content
             echo "{$newBlackListedmobile->mobile_number} : Saved \n";
         } else {
             echo "{$newBlackListedmobile->mobile_number} : Failed \n";
         }
     }
     //when done
     //set status to done
     $latestidle->status = JobQueue::$JOBQUEUE_STATUS_DONE;
     $latestidle->date_done = date("Y-m-d H:i:s");
     //set done datetime to now()
     $latestidle->save();
     echo "Queue DONE \n";
 }
Exemplo n.º 18
0
 /**
  * Load a set of words and their stems and check if the stemmer
  * produces the correct stems
  *
  * @group Slow
  */
 public function testStemmer()
 {
     $words = new \SplFileObject(TEST_DATA_DIR . '/Stemmers/PorterStemmerTest/words.txt');
     $stems = new \SplFileObject(TEST_DATA_DIR . '/Stemmers/PorterStemmerTest/stems.txt');
     $words->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
     $stems->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
     $stems->rewind();
     $stemmer = new PorterStemmer();
     $this->checkStemmer($stemmer, $words, $stems);
 }
Exemplo n.º 19
0
 /**
  * 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();
 }
Exemplo n.º 20
0
 /**
  * Test the words found in Appendix A from Mr.
  * Ntais's thesis.
  *
  * The words are not tested against the stem reported in the appendix
  * but against the results of Mr. Ntais's canonical implementation in js
  * found here http://people.dsv.su.se/~hercules/greek_stemmer.gr.html
  */
 public function testFromAppendixA()
 {
     $words = new \SplFileObject(TEST_DATA_DIR . '/Stemmers/GreekStemmerTest/appendix-a-words');
     $stems = new \SplFileObject(TEST_DATA_DIR . '/Stemmers/GreekStemmerTest/appendix-a-stems');
     $words->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
     $stems->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
     $stems->rewind();
     $stemmer = new GreekStemmer();
     $this->checkStemmer($stemmer, $words, $stems);
 }
Exemplo n.º 21
0
 /**
  * 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();
 }
Exemplo n.º 22
0
 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()));
     }
 }
Exemplo n.º 23
0
 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;
 }
 public function testAgainstOriginalSedImplementation()
 {
     $tokenizer = new PennTreeBankTokenizer();
     $tokenized = new \SplFileObject(TEST_DATA_DIR . "/Tokenizers/PennTreeBankTokenizerTest/tokenized");
     $tokenized->setFlags(\SplFileObject::DROP_NEW_LINE);
     $sentences = new \SplFileObject(TEST_DATA_DIR . "/Tokenizers/PennTreeBankTokenizerTest/test.txt");
     $sentences->setFlags(\SplFileObject::DROP_NEW_LINE);
     $tokenized->rewind();
     foreach ($sentences as $sentence) {
         if ($sentence) {
             $this->assertEquals($tokenized->current(), implode(" ", $tokenizer->tokenize($sentence)), "Sentence: '{$sentence}' was not tokenized correctly");
         }
         $tokenized->next();
     }
 }
Exemplo n.º 25
0
 public function read()
 {
     $current_locale = setlocale(LC_ALL, '0');
     setlocale(LC_ALL, 'ja_JP.UTF-8');
     $temp = tmpfile();
     $fileObject = new SplFileObject($this->_file);
     foreach ($fileObject as $data) {
         $data = mb_convert_encoding($data, 'UTF-8', 'sjis-win');
         fwrite($temp, $data);
     }
     $meta = stream_get_meta_data($temp);
     $file = new SplFileObject($meta['uri']);
     $file->setFlags(SplFileObject::READ_CSV);
     return new Teamlab_Batch_Importer_ResultDataSet($file);
 }
Exemplo n.º 26
0
 /**
  * Homework5Model constructor.
  */
 public function __construct()
 {
     $file_path = "sales.csv";
     $file = new SplFileObject($file_path);
     $file->setFlags(SplFileObject::READ_CSV);
     $this->records = array();
     $ignore_first_flag = true;
     foreach ($file as $line) {
         if ($ignore_first_flag) {
             $ignore_first_flag = false;
             continue;
         }
         $this->records[] = $line;
     }
 }
Exemplo n.º 27
0
 /**
  * インポートを試み、エラーの場合は適切なExceptionをthrowする
  * @param DnsUserImportDto $dto
  * @throws InvalidArgumentException
  * @throws FormValidationException
  * @todo メソッドが肥大化してきたので分割したい
  */
 public function tryImportAndGetResultCount(DnsUserImportDto $dto)
 {
     if (is_null($dto)) {
         throw \InvalidArgumentException('DtoオブジェクトがNullです。');
     }
     $filePath = $dto->getImportFile()->getRealPath();
     if (!file_exists($filePath)) {
         throw \InvalidArgumentException('インポートファイルが見つかりません。');
     }
     // 登録されているusernameデータを取得
     $userNames = $this->em->getRepository(DnsUser::class)->findUserNameAll();
     $file = new \SplFileObject($filePath);
     $file->setFlags(\SplFileObject::READ_CSV);
     $updateCount = 0;
     $this->em->transactional(function () use($file, $dto, $userNames, &$updateCount) {
         $idx = 0;
         $updateCount = 0;
         foreach ($file as $row) {
             $idx++;
             // ヘッダ行を無視するオプションを選択かつヘッダ行の場合スキップ
             if ($dto->getIsIgnoreHeaderLine() === DnsUserImportDto::IS_IGNORE_HEADER_YES && $idx === 1) {
                 continue;
             }
             // 列数が想定通りでない場合スキップ
             if (count($row) !== DnsUserImportDto::FILE_COLUMN_COUNT) {
                 continue;
             }
             list(, $userName, $password, $controlNo, $comment, $encryptType) = $row;
             mb_convert_variables("UTF-8", "SJIS", $userName, $password, $controlNo, $comment, $encryptType);
             if (in_array($userName, $userNames)) {
                 // 既存のデータは上書きしないオプション選択の場合スキップ
                 if ($dto->getIsUpdated() === DnsUserImportDto::IS_UPDATED_NO) {
                     continue;
                 }
                 $entity = $this->em->getRepository(DnsUser::class)->findOneByUserName($userName);
                 $passwordBackup = $entity->getPassword();
                 $entity->setPassword($password)->recoveryPasswordIfNull($passwordBackup);
             } else {
                 $entity = (new DnsUser())->setUserName($userName)->setPassword($password);
             }
             $entity->setControlNo($controlNo)->setComment($comment)->setEncryptType(intval($encryptType));
             $this->tryValidate($entity);
             $this->em->persist($entity);
             $updateCount++;
         }
     });
     return $updateCount;
 }
Exemplo n.º 28
0
    public function testCastFileObject()
    {
        $var = new \SplFileObject(__FILE__);
        $var->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
        $dump = <<<'EOTXT'
SplFileObject {
%Apath: "%sCaster"
  filename: "SplCasterTest.php"
  basename: "SplCasterTest.php"
  pathname: "%sSplCasterTest.php"
  extension: "php"
  realPath: "%sSplCasterTest.php"
  aTime: %s-%s-%d %d:%d:%d
  mTime: %s-%s-%d %d:%d:%d
  cTime: %s-%s-%d %d:%d:%d
  inode: %d
  size: %d
  perms: 0%d
  owner: %d
  group: %d
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
%AcsvControl: array:2 [
    0 => ","
    1 => """
  ]
  flags: DROP_NEW_LINE|SKIP_EMPTY
  maxLineLen: 0
  fstat: array:26 [
    "dev" => %d
    "ino" => %d
    "nlink" => %d
    "rdev" => 0
    "blksize" => %i
    "blocks" => %i
     …20
  ]
  eof: false
  key: 0
}
EOTXT;
        $this->assertDumpMatchesFormat($dump, $var);
    }
Exemplo n.º 29
0
 /**
  *
  * @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;
 }
Exemplo n.º 30
0
 public function process()
 {
     // import csv file and create new rules
     // check in the var/import directory for file called urls.csv
     $import_file = sprintf('%s/import/urls.csv', Mage::getBaseDir('var'));
     if (file_exists($import_file)) {
         // open file, read each line and then createRule
         $file = new SplFileObject($import_file);
         $file->setFlags(SplFileObject::READ_CSV);
         foreach ($file as $row) {
             list($fromUrl, $toUrl) = $row;
             //   echo '<br />',$fromUrl,' ',$toUrl;
             if ($fromUrl && $toUrl) {
                 // both must be set...
                 $this->createRule($fromUrl, $toUrl);
             }
         }
     }
 }