protected function render() { $fileObject = new \SplFileObject($this->getPath()); $reader = Reader::createFromFileObject($fileObject); $reader->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); return $reader; }
/** * Parses a csv file into a DataTable. * * Pass in a filepath to a csv file and an array of column types: * ['date', 'number', 'number', 'number'] for example and a DataTable * will be built. * * @access public * @since 1.0.0 * @param string $filepath Path location to a csv file * @param array $columnTypes Array of column types to apply to the csv values * @throws \Khill\Lavacharts\Exceptions\InvalidFunctionParam * @return \Khill\Lavacharts\DataTable */ public function parseCsvFile($filepath, $columnTypes = null) { if (Utils::nonEmptyString($filepath) === false) { throw new InvalidFunctionParam($filepath, __FUNCTION__, 'string'); } $this->addNewColumns($columnTypes); $this->setReader(Reader::createFromPath($filepath)); $this->reader->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); $csvColumns = $this->reader->fetchOne(); foreach ($this->newColumns as $index => $column) { if (in_array($column, $this->columnTypes, true) === false) { throw new InvalidColumnType($column, Utils::arrayToPipedString($this->columnTypes)); } $this->addColumnFromStrings($columnTypes[$index], $csvColumns[$index]); } $csvRows = $this->reader->setOffset(1)->fetchAll(function ($row) { return array_map(function ($cell) { if (is_numeric($cell)) { return $cell + 0; } else { return $cell; } }, $row); }); return $this->addRows($csvRows); }
protected function execute(InputInterface $input, OutputInterface $output) { $this->promptForCredentials($input, $output); $dstPath = $input->getArgument('dst_path'); $csvPath = $input->getArgument('csv_path'); $reader = new Reader($csvPath); $csv = $reader->setOffset(1)->fetchAll(); // Create destination folder. mkdir($dstPath, 0777, true); if (!is_writeable($dstPath)) { die("OMERGERD COULDN'T WRITE TO DIRECTORY!!!"); } foreach ($csv as $row) { list($sslId, $cn, $status) = array($row[0], $row[2], $row[4]); if ($status != 'Issued') { $output->writeln(sprintf("<info>%s is %s. Skipping...</info>", $cn, $status)); continue; } $output->writeln(sprintf("%s (%s): %s", $cn, $sslId, $status)); $response = $this->incommon->certs->collect($this->authData, $sslId, 1); // Get cert contents. $certData = $response->SSL->certificate; // Clean * out of CN $filename = str_replace('*', 'star', $cn); // Write file $filepath = "{$dstPath}/{$filename}.crt"; $fp = fopen($filepath, "w"); fwrite($fp, $certData); fclose($fp); } }
/** * @param Reader $reader */ public function __construct(Reader $reader) { $tweets = []; foreach ($reader->fetchAssoc() as $tweet) { $tweets[] = new Tweet($this->getFrom($tweet, 'tweet_id'), $this->getFrom($tweet, 'text'), $this->getFrom($tweet, 'source'), $this->getFrom($tweet, 'timestamp'), $this->getFrom($tweet, 'in_reply_to_status_id'), $this->getFrom($tweet, 'retweeted_status_id'), $this->getFrom($tweet, 'expanded_urls')); } $this->tweetCollection = new TweetCollection($tweets); }
/** * Set the metadata from a file. * * @param string $file */ public function setDefaultsFromFile($file) { $file = new Reader($file); // Fetch columns $rows = $file->fetchOne(); $file->setOffset(1); // Fetch entries and set defaults $entries = $file->fetchAssoc($rows); foreach ($entries as $entry) { if (strpos(URL::current(), $entry['url']) !== false) { $this->defaults = $entry; } } }
/** * Main parse method */ private function parse() { $inputCsv = Reader::createFromPath($this->fileName); $inputCsv->setDelimiter(';'); /** * get keys from first line */ $this->characteristicKeys = $inputCsv->fetchOne(); try { switch ($this->translatorClass) { case 'TowerTranslator': $enemyTrans = new TowerTranslator($this->characteristicKeys); break; case 'EnemyTranslator': $enemyTrans = new EnemyTranslator($this->characteristicKeys); break; default: $enemyTrans = new EnemyTranslator($this->characteristicKeys); } $this->characteristicKeys = $enemyTrans->getCharacteristicKeys(); } catch (\Exception $e) { print_r($e); } /** * get other lines from file to array */ $this->characteristic = $inputCsv->fetchAssoc($this->characteristicKeys); $this->badDataFilter(); }
/** * Run the actual import * * @return Collection */ public function createImportEntries() : Collection { $config = $this->job->configuration; $content = $this->job->uploadFileContents(); // create CSV reader. $reader = Reader::createFromString($content); $reader->setDelimiter($config['delimiter']); $start = $config['has-headers'] ? 1 : 0; $results = $reader->fetch(); Log::notice('Building importable objects from CSV file.'); foreach ($results as $index => $row) { if ($index >= $start) { $line = $index + 1; Log::debug('----- import entry build start --'); Log::debug(sprintf('Now going to import row %d.', $index)); $importEntry = $this->importSingleRow($index, $row); $this->collection->put($line, $importEntry); /** * 1. Build import entry. * 2. Validate import entry. * 3. Store journal. * 4. Run rules. */ $this->job->addTotalSteps(4); $this->job->addStepsDone(1); } } Log::debug(sprintf('Import collection contains %d entries', $this->collection->count())); Log::notice(sprintf('Built %d importable object(s) from your CSV file.', $this->collection->count())); return $this->collection; }
/** * Execute the job. * * @return void */ public function handle() { $reader = Reader::createFromPath($this->path); $read = 0; $saved = 0; foreach ($reader->fetchAssoc() as $row) { $read++; $artist = Artist::firstOrNew(['artist_id' => $row['artist_id']]); $artist->artist_id = $row['artist_id']; $artist->slug = $row['slug']; $artist->name = $row['name']; $artist->PID = $row['PID']; $artist->year_birth = $row['year_birth']; $artist->year_death = $row['year_death']; $artist->copyright = $row['copyright']; if ($artist->save()) { $saved++; } } $report = ['event' => 'artists.imported', 'data' => ['read' => $read, 'saved' => $saved]]; $message = json_encode($report); $context = new \ZMQContext(); $socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); $socket->connect("tcp://localhost:5555"); $socket->send($message); }
/** * Execute the console command. * * @author Bertrand Kintanar */ public function handle() { $csv = Reader::createFromPath(storage_path() . '/employee.csv'); $csv->setOffset(2); $data = $csv->query(); foreach ($data as $lineIndex => $row) { $data = []; $data['employee_id'] = $row[0]; $data['joined_date'] = Carbon::parse($row[1])->toDateString(); $data['birth_date'] = Carbon::parse($row[23])->toDateString() ?: null; $data['first_name'] = utf8_encode($row[4]); $data['middle_name'] = utf8_encode($row[5]) ?: null; $data['last_name'] = utf8_encode($row[6]); $data['suffix_name'] = utf8_encode($row[7]) ?: null; $data['address_1'] = utf8_encode($row[9]); $data['address_city_id'] = City::whereName($row[10])->pluck('id') ?: null; $data['address_province_id'] = 25; $data['address_country_id'] = 185; $data['address_postal_code'] = $row[11] ?: null; $data['social_security'] = $row[15] ?: null; $data['tax_identification'] = $row[16] ?: null; $data['philhealth'] = $row[17] ?: null; $data['hdmf_pagibig'] = $row[18] ?: null; $data['mid_rtn'] = $row[19] ?: null; $new_employee = Employee::create($data); $components = SalaryComponent::all(); foreach ($components as $value) { $salary_components = ['employee_id' => $new_employee->id, 'component_id' => $value->id, 'value' => 0]; EmployeeSalaryComponent::create($salary_components); } } }
/** * Execute the job. * * @return void */ public function handle() { $reader = Reader::createFromPath($this->path); $read = 0; $saved = 0; foreach ($reader->fetchAssoc() as $row) { $read++; $object_number = $row['object_number']; unset($row['object_number']); foreach ($row as $key => $value) { $document = Document::firstOrNew(['url' => $value]); $document->object_number = $object_number; $document->url = $value; if ($key == "data") { $document->type = "data"; $document->order = ""; } else { list($type, $order) = explode("_", $key); $document->type = $type; $document->order = isset($order) ? $order : ""; } if ($document->save()) { $saved++; } } } $report = ['event' => 'documents.imported', 'data' => ['read' => $read, 'saved' => $saved]]; $message = json_encode($report); $context = new \ZMQContext(); $socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); $socket->connect("tcp://localhost:5555"); $socket->send($message); }
/** * @param $fields * @param $file * @param $table * @return mixed */ private function import($fields, $file, $table, $insert = true) { $this->table = $table; $this->fields = $fields; $sql = $this->generateSQL($insert); $this->sth = $this->dbh->prepare($sql); $csv = Reader::createFromPath($file); $header = $csv->fetchOne(); $csv->setOffset(1); //because we don't want to insert the header $justDoIT = $csv->each(function ($row) use($header, $sql) { $this->attachBinds($row, $header); try { $exec = $this->sth->execute(); } catch (\ErrorException $e) { echo $e->getMessage(); } if (!$exec) { echo "--DEBUG: ", $sql, $this->lastParams, PHP_EOL; } return true; }); if ($justDoIT) { return true; } //debug -- echo sql and params; echo "--DEBUG: ", $sql, $this->lastParams, PHP_EOL; return false; }
/** * Execute the console command. * * @return mixed */ public function handle() { // read file $csv = Reader::createFromPath(storage_path($this->argument('file'))); //get the first row, usually the CSV header $this->headers = collect($csv->fetchOne()); // grab all rows $rows = $csv->setOffset(1)->fetchAll(); // remove the last row if null if (!$rows[count($rows) - 1][0]) { array_pop($rows); } // loop them foreach ($rows as $row) { // map $school = School::firstOrNew(['name' => $row[$this->column('SchoolName')]]); $this->mapData($school, $row); // save if ($this->changes($school)) { $school->save(); } $this->processed++; } $this->comment('Finished Schools Bulk Update'); $this->comment('Processed: ' . $this->processed); $this->comment('Changed: ' . $this->changes); }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $teachUpload = new Upload(); if (Request::hasFile('upload')) { $file = Request::file('upload'); $file = $file->move(public_path() . '/uploads/dates', time() . '-' . $file->getClientOriginalName()); $teachUpload->file_path = $file->getRealPath(); $teachUpload->save(); $csvPath = $teachUpload->file_path; $reader = Reader::createFromPath($csvPath); $reader->setFlags(SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); $reader->setOffset(1); $pattern = '/([(A-Z)|A-Z]* - .+)/'; $courses = DB::table('courses')->get(); foreach ($reader as $index => $row) { if ($index != 0) { $courseNo = $row[1]; $teachDate = $row[5]; $startTime = $row[6]; $endTime = $row[7]; foreach ($courses as $course) { if ($courseNo == $course->course_no) { DB::table('dates')->insert(array('course_id' => $course->id, 'teach_date' => $teachDate, 'session_start' => $startTime, 'session_end' => $endTime)); } } } } } $teachUpload->save(); return redirect()->route('admin_dates_index')->with('status', 'Teach dates uploaded'); }
/** * @param \Seat\Web\Http\Validation\CsvImport $request * * @return \Illuminate\Http\RedirectResponse */ public function postCsv(CsvImport $request) { $csv = Reader::createFromFileObject($request->file('csv')->openFile()); // Keep tabs on the amount of keys that have // been inserted, and how many have been // considered erroneous $updated = 0; $errored = 0; // Loop the CSV, validating the lines // and inserting into the database foreach ($csv as $k => $data) { // Assign the $data to readable names $key_id = $data[0]; $v_code = $data[1]; // Validate the keys. We check that we dont // already have this key in the database to ensure // that we dont mess up the ownership by accident. $validator = Validator::make(['key_id' => $key_id, 'v_code' => $v_code], ['key_id' => 'required|numeric|unique:eve_api_keys,key_id', 'v_code' => 'required|size:64|alpha_num']); // Ensure the format was ok if ($validator->fails()) { $errored++; continue; } // Add the API Key ApiKey::create(['key_id' => $key_id, 'v_code' => $v_code, 'user_id' => auth()->user()->id, 'enabled' => true]); $updated++; } return redirect()->back()->with('success', 'Import complete! Success: ' . $updated . '. Error: ' . $errored); }
protected function readCSV() { $csvObj = Reader::createFromString($this->readFile(static::IMPORT_FILE)); $csvObj->setDelimiter(','); $results = $csvObj->fetchAssoc(); return $results; }
public function import(Request $request) { if (Request::hasFile('import-file')) { $file = Request::file('import-file'); $subscribers = \League\Csv\Reader::createFromPath($file); $subscribers = $subscribers->fetchAssoc(); if (count($subscribers) > 0) { $mailchimp = new MailChimp(); foreach ($subscribers as $subscriber) { // create the signup $signup = new Signup(); $signup->fname = $subscriber['fname']; $signup->lname = $subscriber['lname']; $signup->email = $subscriber['email']; $signup->zip = $subscriber['zip']; $signup->ip = $_SERVER['REMOTE_ADDR']; try { // save the signup $signup->save(); // add it to mailchimp $mailchimp->addSubscriber(['email' => $signup->email, 'fname' => $signup->fname, 'lname' => $signup->lname, 'zip' => $signup->zip]); } catch (\Exception $e) { } } return redirect('admin/subscribers')->withSuccess('Subscribers import completed.'); } } else { echo 'no file uploaded'; } }
/** * */ public function import($data, Event $event) { $csv = Reader::createFromString(trim($data)); $csv->setDelimiter(';'); // get startgroups at first $groups = new Map(); foreach ($csv->fetchColumn(0) as $name) { if (!$groups->has($name)) { $startgroup = $this->getStartgroup($name); $startgroup->setEvent($event); $groups->set($name, $startgroup); } } $id = 0; foreach ($csv as $row) { $id++; $routine = $this->getRoutine($id); $group = $groups->get($row[0]); $judges = (count($row) - 1) / 3; for ($j = 1; $j <= $judges; $j++) { $score = new PerformanceScore(); $score->setRoutine($routine); $score->setJudge($group->getPerformanceJudge($j)); $score->setExecution($row[($j - 1) * 3 + 1]); $score->setChoreography($row[($j - 1) * 3 + 2]); $score->setMusicAndTiming($row[($j - 1) * 3 + 3]); $score->setTotal($row[($j - 1) * 3 + 1] + $row[($j - 1) * 3 + 2] + $row[($j - 1) * 3 + 3]); $routine->addPerformanceScore($score); } $group->addRoutine($routine); $group->save(); } $event->save(); }
/** * Return a CSV reader with the provided document. * * @return \League\Csv\Reader */ public function getCSV($path) { $csv = Reader::createFromPath($path); $csv->setDelimiter(','); $csv->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); return $csv; }
protected function execute(InputInterface $input, OutputInterface $output) { $csvsPath = APP_CWD . '/../csv'; $csvsDatesRegexp = 'd-m-Y'; $menusDatesAlreadyLoaded = array_map(function ($date) use($csvsDatesRegexp) { return Carbon::parse($date['start_date'])->format($csvsDatesRegexp); }, Menu::all()->toArray()); $finder = new \Symfony\Component\Finder\Finder(); $finder = $finder->files()->name('*.csv'); foreach ($menusDatesAlreadyLoaded as $date) { $finder->notPath($date); } $files = $finder->in($csvsPath); foreach ($files as $file) { list($d, $m, $Y) = explode('-', explode('_', $file->getFilename())[0]); $menuDate = Carbon::parse("{$Y}-{$m}-{$d}"); $reader = \League\Csv\Reader::createFromPath($file->getPathname()); $results = $reader->setDelimiter(';')->fetchAssoc(); //creazione Menu ed aggiunta del prodotto $menu = Menu::write($menuDate, $menuDate->copy()->addHours(14)); foreach ($results as $row) { if (empty($row['Prezzo'])) { continue; } $price = floatval(ltrim(str_replace([','], '.', $row['Prezzo']), '€')); $category = Product::findCategory($row['Categoria'], false); //creazione Prodotto $product = Product::firstOrCreate(['code' => $row['Codice'], 'description' => $row['Descrizione'], 'category' => $category]); $product->price = $price; $product->update(); $menu->addProduct($product); } } }
/** * Return an array of records. * * @param $limit int * The number of records to get. * * @return object The records. */ public function getRecords($limit = null) { // Use a static cache to avoid reading the CSV file multiple times. static $filtered_records; if (!isset($filtered_records)) { $inputCsv = Reader::createFromPath($this->input_file); $inputCsv->setDelimiter($this->field_delimiter); if (is_null($limit)) { // Get all records. $limit = -1; } $records = $inputCsv->addFilter(function ($row, $index) { // Skip header row. return $index > 0; })->setLimit($limit)->fetchAssoc(); foreach ($records as $index => &$record) { if (!is_null($record[$this->record_key]) || strlen($record[$this->record_key])) { $record = (object) $record; $record->key = $record->{$this->record_key}; } else { unset($records[$index]); } } if ($this->fetchermanipulators) { $filtered_records = $this->applyFetchermanipulators($records); } else { $filtered_records = $records; } } return $filtered_records; }
public function __construct($data) { if (is_string($data)) { $this->csv = Reader::createFromString($data); } else { throw new InvalidArgumentException('CsvParser only accepts (string) [csv] for $data.'); } }
public function __construct($file, $helper_column) { $this->helper = new \stdClass(); $this->input = $file; $this->csv = Reader::createFromPath($this->input); $this->helper->name = $helper_column; $this->get_offset(); }
function __construct($pathToCSV, $column = 0) { // @todo handle exception (try catch) $this->mailColumn = $column; // @see http://csv.thephpleague.com/ // @todo $this->csv = Reader::createFromPath($pathToCSV); }
public function fetch(Connector $connector, EncapsulatedOptions $options = null) { $csv = $connector->fetch('http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv'); $reader = Reader::createFromString($csv); foreach ($reader->fetchAssoc() as $row) { (yield $row); } }
/** * Get headers from path * * @param string $path Path to file * @return array */ public function getHeadersFromPath($path) { $result = []; $this->validatePath($path); $reader = Reader::createFromPath($path, $this->open_mode); $result = $reader->fetchOne(); return $result; }
/** * Initialises the League CSV Reader if not initialised yet. * @return Boolean return TRUE if gets initialised or * FALSE if already initialised */ private function initParser() { if ($this->csv_reader !== NULL) { return FALSE; } $this->csv_reader = Reader::createFromPath($this->getDatasource()); return TRUE; }
/** * @param string $data * @return Reader */ protected function getReader($data) { $csv = Reader::createFromString($data); $delimiters = $csv->fetchDelimitersOccurrence([' ', '|', ',', ';'], 10); if (sizeof($delimiters) > 0) { $csv->setDelimiter(array_keys($delimiters)[0]); } return $csv; }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $upload = new Course(); if (Request::hasFile('upload')) { $file = Request::file('upload'); //$file = $file->move(public_path() . '/uploads/courses', time() .'-' . $file->getClientOriginalName()); $upload->file_path = $file->getRealPath(); //$upload->save(); $csvPath = $upload->file_path; $reader = Reader::createFromPath($csvPath); $reader->setFlags(SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); $reader->setOffset(1); $pattern = '/([(A-Z)|A-Z]* - .+)/'; $paperPattern = '/\\b(ACCA|CIMA)\\b/'; $centres = DB::table('centres')->get(); $papers = DB::table('papers')->get(); foreach ($reader as $index => $row) { if ($index != 0) { $centreCode = $row[0]; $courseNo = $row[1]; $courseName = $row[2]; $startDate = $row[5]; $status = $row[8]; $price = $row[9]; /*foreach ($centres as $centre) { if($centreCode == $centre->centre_code) { $centreId = $centre->id; preg_match($pattern, $courseName, $matches); DB::table('courses')->insert( array( 'course_no' => $courseNo, 'name' => $matches[0], 'start_date' => $startDate, 'price' => $price, 'status' => $status, 'centre_id' => $centreId, ) ); } } */ foreach ($papers as $paper) { preg_match($paperPattern, $courseName, $paperMatch); dd($paperMatch); } } } } $upload->save(); return redirect('courses')->with('status', 'Courses Uploaded'); }
private function getMappingsArray($mappingCSVpath, $numOfFields = 3) { $filename = $mappingCSVpath; $reader = Reader::createFromPath($filename); $collectionMappingArray = array(); foreach ($reader as $index => $row) { $collectionMappingArray[$row[0]] = $row; } return $collectionMappingArray; }
protected function execute(InputInterface $input, OutputInterface $output) { $this->writeSection($output, 'Csv to Po converter'); $this->cwd = getcwd() . DIRECTORY_SEPARATOR; $output->writeln('<info>Using CWD</info> ' . $this->cwd); $csvFile = $this->getInputCsvFile($input, $output); $poFiles = $this->getInputPoFiles($input, $output); $outputFolder = $this->getOutputFolder($input, $output); $useDefaults = $input->getOption('use-defaults'); if ($useDefaults) { $output->writeln('<info>Csv file</info>:' . "\n" . $csvFile . "\n"); $output->writeln('<info>Po files</info>:'); $this->writeList($output, $poFiles); $output->writeln("\n" . '<info>Output folder</info>: ' . "\n" . $outputFolder . "\n"); } $writeHandles = []; foreach ($poFiles as $poFile) { $key = basename($poFile, '.po'); $output->writeln('<info>loading ' . $key . '</info>...'); $writeHandles[$key] = Translations::fromPoFile($poFile); } $header = null; $output->writeln('<info>reading from csv</info>...'); $reader = Reader::createFromPath($csvFile); foreach ($reader as $i => $row) { if ($header == null) { $header = $row; continue; } $original = null; foreach ($row as $j => $string) { if ($original == null) { $original = $string; continue; } if (empty($string)) { continue; } $writeHandle = $writeHandles[$header[$j]]; $translation = $writeHandle->find(null, $original); if ($translation != false && $translation->translation != $string) { if (!empty($translation->translation)) { $this->handleConflict($input, $output, $original, $translation->translation, $string); } else { $translation->setTranslation($string); } } else { $translation = new Translation(null, $original); $translation->setTranslation($string); $writeHandle[] = $translation; } } } foreach ($writeHandles as $key => $writeHandle) { $output->writeln('<info>writing ' . $key . '</info>...'); $content = Po::toString($writeHandle); file_put_contents($this->outputDir . DIRECTORY_SEPARATOR . $key . '.po', $content); } $output->writeln('<info>done</info>'); }