Download the PHP package oshomo/csv-utils without Composer

On this page you can find all versions of the php package oshomo/csv-utils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package csv-utils

Build Status codecov

CSVUtils

Make sure you use a tagged version when requiring this package.

Table of Content

Current Stable Versions

How to Run

I have added a sample index.php file for a quick test of how to use the package. To run the sample; from the package root, run composer install then using php built in server run php -S localhost:8000, this would start the server at localhost:8000. Visit the URL from your browser and you should see the generated files in the sample folder at the root of the package.

Implementation

The Validator expects a valid file path, the CSV delimiter, an array of validation rule(s) and an optional message(s) array to over-write the default messages of the validator.

Documentation

Initializing a Validator

Set a valid csv file path, pass the CSV delimiter and pass in your validation rules.

Validating the CSV

Now we are ready to validate the CSV. The validator provides a validate method that can be called like so: $validator->validate();. The validate method returns an array of the invalid rows if validation fails. If the validation passes the validate method returns the CSV data as an array

A better implementation:

Error messages

To get the rows with validation errors and their errors. The validator expose errors method that can be used like so $validator->errors().

You can also customize the error messages for different validation rules and different attributes by passing a message array to the validator like so:

In this above example:

The :attribute place-holder will be replaced by the actual name of the field under validation. The :value place-holder will be replaced with value being validated. The :line place-holder will also be replaced with the row/line number in the CSV in which the error happened.

You may also utilize other place-holders in validation messages. For example the between rule exposes two other placeholder min and max. Find more about this in the available rules section

Available rules

between:min,max:

ascii_only:

url:

Writing CSV Output Data

The output of the CSV file can be written into any format. The currently suported format is xml and json. The validator exposes a write method to write the output data into the same folder as the CSV. Find example implementation below:

The JsonConverter simply writes the output data as JSON. The XmlConverter converter writes the data as XML. XmlConverter takes an optional parameter for setting the XML records element. If non is supplied it defaults to item e.g $validator->write(new XmlConverter("hotel")); would write the below:

NOTE: Either validation passes or fails, you can always write the CSV output data to the available formats. In cases where validation fails there would be an extra error property in the written data.

Passing Custom Rules to Validator Using Rule Object

Passing a custom rule to the validator is easy. Create a CustomRule class the implements Oshomo\CsvUtils\Contracts\ValidationRuleInterface interface. And pass that class to the rule array, easy. E.g:

The class definition for UppercaseRule. Follow the same approach if you want to create your own rule.

If the CustomRule accepts parameters like the between rule, then your CustomRule class must implement both Oshomo\CsvUtils\Contracts\ValidationRuleInterface and Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface. See Oshomo\CsvUtils\Rules\Between as an example.

Passing Custom Rules to Validator Using Closure

If you only need the functionality of a custom rule once throughout your application, you may use a Closure instead of a rule object. The Closure receives the attribute's value, and a $fail callback that should be called if validation fails:

Writing CSV Output Data to Other Formats

Writing the CSV output data to other format is also very easy. Create a CustomConverter class the implements Oshomo\CsvUtils\Contracts\ConverterHandlerInterface interface. And pass that class to the write method of the validator, easy. Below is an sample implementation of a JSON converter

Running Tests

Run composer test from the root of the Package.

Contributing to this Repo

Feel free to submit a pull request for a feature or bug fix. However, do note that before your pull request can be merged it must have test written or updated as the case maybe. The project run's automatic checks to make sure that the Symfony code standards are met using php-cs-fixer.

So, before pushing or making any pull request run the below command:


All versions of csv-utils with dependencies

PHP Build Version
Package Version
Requires php Version >7.1.3
ext-mbstring Version *
ext-json Version *
ext-simplexml Version *
ext-dom Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package oshomo/csv-utils contains the following files

Loading the files please wait ....