Download the PHP package vkr/csv-bundle without Composer
On this page you can find all versions of the php package vkr/csv-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vkr/csv-bundle
More information about vkr/csv-bundle
Files in vkr/csv-bundle
Package csv-bundle
Short Description A bundle for CSV file creation in Symfony2/3
License MIT
Homepage https://github.com/wladislavk/CSVBundle
Informations about the package csv-bundle
About
This is a simple bundle for CSV creation in Symfony. It doesn't have any configuration and no dependencies except for Symfony, however it works best if you use Doctrine and want to parse a query result into CSV format.
Installation
Nothing to install except enabling the bundle in .
Usage
Basic usage
First, you need to have some kind of data source. This bundle is very flexible about how your data source looks - basically, it has to be an array or array-like object (anything that works with ), and its elements can be either arrays or objects. In other words, it will work with any non-null value that is returned by Doctrine's or .
You also need to define fields that will make it into your CSV. If your data source consists of arrays, those fields are just keys of those arrays. If you work with objects, field names must comply with the following rule with respect to the object's public getters:
Once you have a data source and field names, you can form the CSV data in your controller:
The variable will hold the following string:
If there are any commas in the data source values, those will be swapped for whitespaces.
Field labels
If you want to create 'nice' titles for the first row of your CSV, you can use the third argument of , array of field labels. If it is not empty, it should contain the same number of elements as . For example:
The resulting data will look as follows:
Note that neither nor can normally contain separator symbols, otherwise will be thrown. However, if you define a field with a separator symbol and a corresponding label without the separator, the script will work as usual:
Custom separators
If you want to use something else than comma as a separator, you can specify the fourth argument of . You can also change the separator replacement string by specifying the fifth argument. Note that separator replacement cannot occur in both field names and field labels.
The resulting data will look as follows:
Handling dates inside data
If an element of your data source cannot be implicitly converted to string, the bundle will throw . The only exception to this are objects that are frequently returned by Doctrine. If you expect to receive a object, you should specify the sixth argument of which should be a format string that is accepted by method.
The resulting data will look as follows:
If the format string contains separator symbols, those will be replaced as well.
If you did not specify the sixth argument or it is malformed, the bundle will not give you any errors, rather you will get a standard result of calling without arguments, that will depend on your PHP settings.
Fillers
The seventh, and final argument of determines what to do if the field value on one or more of elements of your data source is null or undefined. If this argument is not specified, an empty string is returned. For example:
The resulting data without look as follows:
If you specify that
you will get this:
Separator symbols inside fillers will be replaced.
Forming CSV file from data
The variable returned by is not yet a CSV file, it is just a string. To make Symfony return a file, you need to define headers and return a response. This bundle includes a simple helper function for header definition, . It accepts an optional argument which is a file name without extension. If this argument is not specified, the file will be called .
Here is how you return a file from the controller:
The resulting file will be named .
API
string CSVCreator::parseDataToCSV(array|Traversable $dataSource, string[] $fields, string[] $fieldLabels = [], string $separator = ',', string $replacement = ' ', string $dateFormat = '', string $filler = '')
string[] CSVCreator::setHeaders(string $filename = '')