2017 © Pedro Peláez
 

library file-parser

PHP package for parsing data contained in a file into a php array.

image

nackjicholson/file-parser

PHP package for parsing data contained in a file into a php array.

  • Wednesday, May 31, 2017
  • by nackjicholson
  • Repository
  • 3 Watchers
  • 3 Stars
  • 756 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

file-parser

This is a composer package for parsing files that contain structured data., (*1)

The current supported formats are:, (*2)

  • Csv
  • Json
  • Yaml
  • .ini

WARNING Only works with PHP 5.4 and up due to short array syntax and use of traits., (*3)

Example:

foobar.yml, (*4)

foo: bar

example.php, (*5)

<?php

require_once '../vendor/autoload.php';

$fileParser = new \Nack\FileParser\FileParser();

print_r($fileParser->yaml('foobar.yml'));

Outputs:, (*6)

Array
(
    [foo] => bar
)

Install

Via composer, (*7)

compser require nackjicholson/file-parser=~2.1, (*8)

or add too composer.json, (*9)

"require": {
    "nackjicholson/file-parser": "~2.1"
}

CSV

This library provides three ways to parse a csv file into a php array. There is full support for delimiter, enclosure, and escape options by passing an associative array of options to each csv method. Options default to:, (*10)

['delimiter' => ',', 'enclosure' => '"', 'escape' => '\\']

There is an example of how to set a file to parse with a ; delimiter in example/example.php., (*11)

::csv(mixed $file, array $options = [])

This method provides a literal parse of a file as a csv. Each line is translated to an array of values. Empty lines are not skipped., (*12)

foo,bar
,empty first value

bingo,bango,bongo

$fileParser->csv('file.csv');, (*13)

[
    [ 'foo', 'bar' ],
    [ '', 'empty first value' ],
    [ null ],
    [ 'bingo', 'bango', 'bongo' ]
]

::csvColumnar(mixed $file, array $options = [])

Parses the contents of a csv as data structured columnar. Takes into account the first row of a csv file as column headers, and attaches each column header to its associated row value., (*14)

For example, a csv describing a table of contacts., (*15)

name,email,phone
will,willieviseoae@gmail.com,555-2242
bill,,
,,

The first row is the headers of the table 'name', 'email', and 'phone'. The second row is a complete set of data. The third has a name, but empty email and phone. The fourth is not a row, it's a blank line., (*16)

$fileParser->csvColumnar('contacts.csv');, (*17)

[
    [
        'name' => 'will',
        'email' => 'willieviseoae@gmail.com',
        'phone' => '555-2242'
    ],
    [
        'name' => 'bill',
        'email' => '',
        'phone' => ''
    ]
]

::csvRows(mixed $file, array $options = [])

Parses the contents of a csv where each row uses the first value as a key, which is set with the subsequent values. This is ideal for a csv which describes a set of key => value pairs, or key => [ values... ]., (*18)

foo,bar
bingo,bango,bongo
,,
,nope,not,a,chance
emptyValue,

$fileParser->csvRows('rows.csv');, (*19)

[
    'foo' => 'bar',
    'bingo' => [ 'bango', 'bongo' ],
    'emptyValue' => ''
]

As you can see it ignores blank lines, or lines where the key would be empty., (*20)

INI

::ini(mixed $file)

This method will parse a php ini configuration file into an array. It delegates directly to PHP's built in function parse_ini_file., (*21)

$fileParse->ini('/etc/php55/php.ini');, (*22)

JSON

::json(mixed $file)

Parses a json file into a php array. This parsing strategy delegates directly to PHP's built in json_decode., (*23)

{
    "foo": "bar"
}

$fileParser->json('foobar.json');, (*24)

[ 'foo' => 'bar' ]

YAML

::yaml(mixed $file)

Parses a yaml file into a php array. This parsing strategy delegates directly to symfony/Yaml, (*25)

foo: bar

$fileParser->yaml('foobar.yml');, (*26)

[ 'foo' => 'bar' ]

SplFileInfo and SplFileObject Support

Instead of passing a path to any of the file-parser methods. You can supply either a SplFileInfo or SplFileObject object., (*27)

$fileParser = new FileParser();

$splFileInfo = new \SplFileInfo('path/to/file.yml');
$fileParser->yaml($splFileInfo);

$splFileObject = new \SplFileObject('path/to/file.json');
$fileParser->json($splFileObject);

Contributing

Report issues, and feel free to make requests there. Tag github issues with the best label you can. If this library doesn't do something you want, it's not difficult to extend. The library is built on the strategy pattern, new strategies can parse data differently. Write tests, and make pull requests. If you do not test your code with 100% coverage, your PR will be rejected., (*28)

Contact me

Will Vaughn, (*29)

email: willieviseoae@gmail.com, (*30)

twitter: @nackjicholsonn, (*31)

The Versions

31/05 2017

dev-master

9999999-dev

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

11/10 2014

v2.1.0

2.1.0.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

24/09 2014

v2.0.0

2.0.0.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

23/09 2014

v1.2.0

1.2.0.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

23/09 2014

v1.1.0

1.1.0.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

23/09 2014

v1.0.0

1.0.0.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

23/09 2014

v0.1.1

0.1.1.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn

23/09 2014

v0.1.0

0.1.0.0

PHP package for parsing data contained in a file into a php array.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vaughn