/** * @return bool */ public function run() : bool { // create temporary file: $this->tempFile(); // necessary for CSV writer: $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName; $writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w'); $rows = []; // Count the maximum number of sources and destinations each entry has. May need to expand the number of export fields: $maxSourceAccounts = 1; $maxDestAccounts = 1; /** @var Entry $entry */ foreach ($this->getEntries() as $entry) { $sources = $entry->sourceAccounts->count(); $destinations = $entry->destinationAccounts->count(); $maxSourceAccounts = max($maxSourceAccounts, $sources); $maxDestAccounts = max($maxDestAccounts, $destinations); } $rows[] = array_keys($this->getFieldsAndTypes($maxSourceAccounts, $maxDestAccounts)); /** @var Entry $entry */ foreach ($this->getEntries() as $entry) { // order is defined in Entry::getFieldsAndTypes. $current = [$entry->description, $entry->amount, $entry->date]; $sourceData = $this->getAccountData($maxSourceAccounts, $entry->sourceAccounts); $current = array_merge($current, $sourceData); $destData = $this->getAccountData($maxDestAccounts, $entry->destinationAccounts); $current = array_merge($current, $destData); $rest = [$entry->budget->budgetId, $entry->budget->name, $entry->category->categoryId, $entry->category->name, $entry->bill->billId, $entry->bill->name]; $current = array_merge($current, $rest); $rows[] = $current; } $writer->insertAll($rows); return true; }
/** * Create CSV file * */ public function __construct() { if (empty($filepath)) { $this->filepath = realpath(dirname(__FILE__) . '/../') . '/' . $this->filename; } $this->csv = \League\Csv\Writer::createFromPath(new \SplFileObject($this->filepath, 'w+'), 'w'); $this->csv->insertOne($this->columns); }
public function testInsertNormalFile() { $csv = Writer::createFromPath(__DIR__ . '/foo.csv', 'a+'); $csv->insertOne(['jane', 'doe', '*****@*****.**']); $iterator = new LimitIterator($csv->getIterator(), 1, 1); $iterator->rewind(); $this->assertSame(['jane', 'doe', '*****@*****.**'], $iterator->getInnerIterator()->current()); }
/** * @throws \RuntimeException * @param string $file file to write or overwrite * @param array $data */ protected function createFile($file, array $data) { $csv = Writer::createFromPath($file, 'w'); $csv->setDelimiter("\t"); $csv->setEnclosure('"'); // we insert the CSV header $headers = array_keys($data[0]); $csv->insertOne($headers); $csv->insertAll($data); }
/** * Create new csv if needed * and add request data to csv * * @param Reservation $reservation * @return void */ private function insertInCsv(Reservation $reservation) { $writer = Writer::createFromPath(Config::get('reservation.csv_path'), 'a'); $writer->setDelimiter(";"); $writer->setEncodingFrom("utf-8"); if (!$this->checkIfCsvExists()) { // Only add header to first line $headers = ["Naam", "Voornaam", "E-mail", "Bericht", "Toegevoegd op"]; $writer->insertOne($headers); } $writer->insertOne(array($reservation->first_name, $reservation->last_name, $reservation->email, $reservation->message, $reservation->created_at->format('d/m/Y H:i'))); }
/** * Обработка данных и создание нового файла. */ public function handleData() { try { $this->reader = Reader::createFromPath($this->pathToReadFile); $this->writer = Writer::createFromPath($this->pathToWriteFile); $this->setReadableData(); $this->setFormattedData(); $this->writer->insertAll($this->generateInsertData()); echo 'Data was handled and written to file ' . $this->pathToWriteFile, PHP_EOL; } catch (\Exception $exception) { echo $exception->getMessage(), PHP_EOL; } }
/** * Publish applicable redirects * * @return int Number of published redirects */ public function publish() { if (file_exists($this->redirectsFile)) { unlink($this->redirectsFile); } /** @var Collection $redirects */ $redirects = Redirect::query()->where('is_enabled', '=', 1)->orderBy('sort_order')->get(['id', 'match_type', 'target_type', 'from_url', 'to_url', 'cms_page', 'static_page', 'status_code', 'requirements', 'from_date', 'to_date']); // TODO: Throw proper exception try { $writer = Writer::createFromPath($this->redirectsFile, 'w+'); $writer->insertAll($redirects->toArray()); } catch (\Exception $e) { // .. } return $redirects->count(); }
/** * Read excel files to all functions in this model * * @return mixed */ private static function loadXlsFile($typeRead, $type = 'plugin') { if ($type == 'plugin') { $csvReader = Reader::createFromPath(storage_path() . '/recipes.csv'); $csvReader->setDelimiter("\t"); $csvWriter = Writer::createFromPath(storage_path() . '/recipes.csv', 'a'); $csvWriter->setDelimiter("\t"); } if ($type == 'file') { self::setFileCsv(storage_path() . '/recipes.csv'); $csvReader = fopen(self::getFileCsv(), 'r'); //self::setTempFileCsv(tempnam(storage_path(), "tmp") ); self::setTempFileCsv(storage_path() . '/recipes.tmp'); $csvWriter = fopen(self::getTempFileCsv(), 'w'); } $reader = array('reader' => $csvReader, 'writer' => $csvWriter); return $reader[$typeRead]; }
protected function makeJournalTransaction(array $data) { if (empty($data)) { throw new \Exception('Empty data when creating journal Transaction'); } $corpName = strtolower(str_replace(' ', '_', $data[0]->getAccount()->getCorporation()->getCorporationDetails()->getName())); $accountName = strtolower(str_replace(' ', '_', $data[0]->getAccount()->getName())); $fileName = __DIR__ . sprintf('/../../../export/%s_%s_data.%s.csv', $corpName, $accountName, Carbon::now()->toDateTimeString()); if (!file_exists($fileName)) { $fh = fopen($fileName, 'w'); fclose($fh); } $csv = Writer::createFromPath($fileName); $csv->insertOne(['corp', 'account', 'date', 'time', 'ref_id', 'ref_type_id', 'ref_type', 'owner_name1', 'owner_id1', 'owner_id2', 'arg_name1', 'arg_id1', 'amount', 'balance', 'reason', 'owner1_type_id', 'owner2_type_id']); $insertData = []; foreach ($data as $d) { $arr = ['corp' => $d->getAccount()->getCorporation()->getCorporationDetails()->getName(), 'account' => $d->getAccount()->getName(), 'date' => $d->getDate()->format('m/d/Y'), 'time' => $d->getDate()->format('G:m'), 'ref_id' => $d->getRefId(), 'ref_type_id' => $d->getRefTypeId(), 'ref_type' => $d->getRefType()->getRefTypeName(), 'owner_name1' => $d->getOwnerName1(), 'owner_id1' => $d->getOwnerId1(), 'owner_id2' => $d->getOwnerId2(), 'owner_name2' => $d->getOwnerName2(), 'arg_name1' => $d->getArgName1(), 'arg_id1' => $d->getArgId1(), 'amount' => $d->getAmount(), 'balance' => $d->getBalance(), 'reason' => $d->getReason(), 'owner1_type_id' => $d->getOwner1TypeId(), 'owner2_type_id' => $d->getOwner2TypeId()]; $insertData[] = $arr; } $csv->insertAll($insertData); }
protected function execute(InputInterface $input, OutputInterface $output) { $ti = microtime(true); $entity_type = $input->getArgument('entity'); $export_fields = explode(",", $input->getOption('fields')); $max_records = $input->getOption('max'); $skip_records = $input->getOption('skip'); $output_file = $input->getOption('outfile'); $output->writeln(sprintf('<info>Exporting "%s"</info>', $entity_type)); $out_dir = sprintf("%s/output/%s", $this->getApplication()->getAppPath(), $entity_type); if (!file_exists($out_dir)) { $output->writeln(sprintf('<error>Directory not found. [%s]</error>', $out_dir)); return; } $writer = $output_file ? Writer::createFromPath($output_file, 'w') : $output; // process fields and build CSV headers foreach ($export_fields as $i => $field) { @(list($field, $field_as) = explode(" as ", $field)); $headers[] = $field_as ?: $field; $export_fields[$i] = $field; } $this->write($writer, $headers); // process each json file file $idx = 0; foreach (glob($out_dir . '/*.json') as $file_name) { if ($idx++ < $skip_records) { continue; } $data = []; $record = Tools::objectToDotNotationArray(json_decode(file_get_contents($file_name))); foreach ($export_fields as $field) { $data[] = isset($record[$field]) ? $record[$field] : null; } $this->write($writer, $data); if ($idx == $max_records + $skip_records) { break; } } $output->writeln(sprintf("<info>Finished exporting %d \"%s\" records in %01.1f seconds</info>", $idx, $entity_type, microtime(true) - $ti)); }
/** * Merges benchmark * * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $files = $this->resolveFiles($input->getArgument('path')); if (empty($files)) { throw new \InvalidArgumentException('No readable files has been provided'); } foreach ($files as $file) { $reader = Reader::createFromPath($file); $basename = basename($file); $output = Writer::createFromPath(rtrim($input->getOption('output-path'), '/') . $basename); $matrix = []; $samples = []; foreach ($reader->fetchAssoc() as $row) { $samples[$row['sample']] = true; $matrix[$row['operation']][$row['sample']] = $row['total']; } $output->insertOne(array_merge(['Operation'], array_keys($samples))); foreach ($matrix as $code => $row) { $output->insertOne(array_merge([$code], array_values($row))); } } }
use League\Csv\Writer; # Store url $url = "http://centrodosuplemento.com.br/suplementos"; $html = new HtmlDomParser(); $html->loadUrl($url); $page_numbers = []; foreach ($html->find('.toolbar-bottom .pager .pages ol li') as $page) { $page_numbers[] = $page->plaintext; } $max_page = max($page_numbers); # Initialize Arrays $name = []; $price = []; for ($i = 1; $i <= $max_page; $i++) { # Open search results page $url = "http://centrodosuplemento.com.br/suplementos?mode=list?p={$i}"; $product_html = new HtmlDomParser(); $product_html->loadUrl($url); # Store data in Arrays foreach ($product_html->find('.product-name a') as $line) { $name[] = $line->plaintext; } foreach ($product_html->find('.price-box .regular-price .price') as $line) { $price[] = $line->plaintext; } } $writer = Writer::createFromPath('cds_list.csv', 'w'); $writer->insertOne(["Listing Name", "Price"]); for ($p = 0; $p < count($name); $p++) { $writer->insertOne([$name[$p], $price[$p]]); }
protected function getWriter($path = null) { if ($path === null) { $path = $this->getFile(); } return Writer::createFromPath($path, 'a+'); }
/** * Store Requested data. * * @param $request * * @return static */ public function store($request) { $file = $this->checkCsvFile(); $writer = Writer::createFromPath(new SplFileObject($file), 'a'); return $writer->insertOne($request->only($this->allowedFields)); }
/** * {@inheritdoc} */ public function writerTest() { $csv = Writer::createFromPath($this->path, 'w'); $csv->insertAll($this->generateRawData()); }
/** * @param string $filename * @param Type $type * @param array $metadata * @return void */ public function writeDataOfType($filename, Type $type, array &$metadata = []) { $writeHeader = !file_exists($filename); $writer = Writer::createFromPath($filename, 'a'); if (array_key_exists('delimiter', $metadata)) { $writer->setDelimiter($metadata['delimiter']); } if (array_key_exists('enclosure', $metadata)) { $writer->setEnclosure($metadata['enclosure']); } if (array_key_exists('escape', $metadata)) { $writer->setEscape($metadata['escape']); } if (array_key_exists('newline', $metadata)) { $writer->setNewline($metadata['newline']); } if ($type->description()->nativeType() === NativeType::COLLECTION) { foreach ($type as $item) { if ($writeHeader) { if ($item->description()->nativeType() === NativeType::DICTIONARY) { $writer->insertOne(array_keys($item->properties())); } $writeHeader = false; } $writer->insertOne($this->convertToCsvRow($item)); } } else { if ($writeHeader) { if ($type->description()->nativeType() === NativeType::DICTIONARY) { $rows[] = array_keys($type->properties()); } } $writer->insertOne($this->convertToCsvRow($type)); } }
public function export_orders() { $data = get_orders('no_limit=true&order_completed=1'); if (!$data) { return array('error' => 'You do not have any orders'); } $allowed = array('id', 'created_at', 'created_at', 'amount', 'transaction_id', 'shipping', 'currency', 'is_paid', 'user_ip', 'last_name', 'email', 'country', 'city', 'state', 'zip', 'address', 'phone', 'payment_gw', 'order_status', 'taxes_amount', 'cart', 'transaction_id'); $export = array(); foreach ($data as $item) { $cart_items = mw()->shop_manager->order_items($item['id']); $cart_items_str = mw()->format->array_to_ul($cart_items, 'div', 'span'); $cart_items_str = strip_tags($cart_items_str, '<span>'); $cart_items_str = str_replace('</span>', "\r\n", $cart_items_str); $cart_items_str = strip_tags($cart_items_str); if (!empty($cart_items)) { $item['cart'] = $cart_items_str; } foreach ($item as $key => $value) { if (!in_array($key, $allowed)) { unset($item[$key]); } } $export[] = $item; } if (empty($export)) { return; } // dd($export); $filename = 'orders' . '_' . date('Y-m-d_H-i', time()) . uniqid() . '.csv'; $filename_path = userfiles_path() . 'export' . DS . 'orders' . DS; $filename_path_index = userfiles_path() . 'export' . DS . 'orders' . DS . 'index.php'; if (!is_dir($filename_path)) { mkdir_recursive($filename_path); } if (!is_file($filename_path_index)) { @touch($filename_path_index); } $filename_path_full = $filename_path . $filename; // $csv = \League\Csv\Writer::createFromFileObject(new \SplTempFileObject()); $csv = \League\Csv\Writer::createFromPath($filename_path_full, 'w'); //to work make sure you have the write permission $csv->setEncodingFrom('UTF-8'); // this probably is not needed? $csv->setOutputBOM(\League\Csv\Writer::BOM_UTF8); //adding the BOM sequence on output //we insert the CSV header $csv->insertOne(array_keys(reset($export))); $csv->insertAll($export); $download = $this->app->url_manager->link_to_file($filename_path_full); return array('success' => 'Your file has been exported!', 'download' => $download); }
Input text files should be formatted such that the MARC data that you wish to alter is in the first field. Any fields coming afterward will be preserved as-is. */ require_once './vendor/autoload.php'; /* Documentation for League\Csv is available at http://csv.thephpleague.com/ The library is a wrapper for the native PHP CSV functionality */ use League\Csv\Reader; use League\Csv\Writer; $inputfilename = $argv[1]; $outputfilename = $argv[2]; $delimiter = $argv[3]; $reader = Reader::createFromPath($inputfilename); $writer = Writer::createFromPath($outputfilename, 'w'); /* These two lines determine what character delimits each "field" in the input/output file. This could be changed to "," or "|" or any other character. "Special characters", like <tab>, must be within double-quotes. Special characters include \t (tab), \s (space), etc. */ $reader->setDelimiter("\t"); $writer->setDelimiter("\t"); $count = 0; foreach ($reader->fetch() as $row) { $marc = $row[0]; $marc = $marc . '|'; // hack to add a delimiter to end of string $pattern = "/\\|" . $delimiter . ".*(?=[\\|])/"; $newMarc = preg_replace($pattern, '', $marc);
$reader->setOffset(6); $reader->setLimit(3); $res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']); var_dump($res); ?> <h3>Using the Writer class</h3> <p><strong>You can not add/remove/update stream filters between inserts calls</strong> <pre><code>$writer = Writer::createFromPath('/tmp/test.csv', 'w'); if ($writer->isActiveStreamFilter()) { $writer->appendStreamFilter('string.toupper'); } $writer->insertOne('je,suis,toto,le,héros'); </code></pre> <?php $writer = Writer::createFromPath('/tmp/test.csv', 'w'); if ($writer->isActiveStreamFilter()) { $writer->appendStreamFilter('string.toupper'); } $writer->insertOne('je,suis,toto,le,héros'); ?> <p>When the first insert call is done... the stream filter status is frozen and can no longer be updated !! Any added row will be uppercased only no matter what.</p> <?php if ($writer->isActiveStreamFilter()) { $writer->appendStreamFilter('string.rot13'); $writer->removeStreamFilter('string.toupper'); } $writer->insertOne('je,suis,toto,le,héros'); ?>