2017 © Pedro Peláez
 

library stream-parser

PHP Multiformat Streaming Parser

image

rodenastyle/stream-parser

PHP Multiformat Streaming Parser

  • Sunday, July 29, 2018
  • by Rodenastyle
  • Repository
  • 4 Watchers
  • 199 Stars
  • 103 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 1 Open issues
  • 9 Versions
  • 10200 % Grown

The README.md

⚡ PHP7 / Laravel Multi-format Streaming Parser

Build Status Latest Version on Packagist Quality Score Code Coverage License, (*1)

When it comes to parsing XML/CSV/JSON/... documents, there are 2 approaches to consider:, (*2)

DOM loading: loads all the document, making it easy to navigate and parse, and as such provides maximum flexibility for developers., (*3)

Streaming: implies iterating through the document, acts like a cursor and stops at each element in its way, thus avoiding memory overkill., (*4)

https://www.linkedin.com/pulse/processing-xml-documents-dom-vs-streaming-marius-ilina/, (*5)

Thus, when it comes to big files, callbacks will be executed meanwhile file is downloading and will be much more efficient as far as memory is concerned., (*6)

Installation

composer require rodenastyle/stream-parser

Delegate as possible the callback execution so it doesn't blocks the document reading:, (*7)

(Laravel Queue based example), (*8)

use Illuminate\Support\Collection;

StreamParser::xml("https://example.com/users.xml")->each(function(Collection $user){
    dispatch(new App\Jobs\SendEmail($user));
});

Practical Input/Code/Output demos

XML

<bookstore>
    <book ISBN="10-000000-001">
        <title>The Iliad and The Odyssey</title>
        <price>12.95</price>
        <comments>
            <userComment rating="4">
                Best translation I've read.
            </userComment>
            <userComment rating="2">
                I like other versions better.
            </userComment>
        </comments>
    </book>
    [...]
</bookstore>
use Illuminate\Support\Collection;

StreamParser::xml("https://example.com/books.xml")->each(function(Collection $book){
    var_dump($book);
    var_dump($book->get('comments')->toArray());
});
class Tightenco\Collect\Support\Collection#19 (1) {
  protected $items =>
  array(4) {
    'ISBN' =>
    string(13) "10-000000-001"
    'title' =>
    string(25) "The Iliad and The Odyssey"
    'price' =>
    string(5) "12.95"
    'comments' =>
    class Tightenco\Collect\Support\Collection#17 (1) {
      protected $items =>
      array(2) {
        ...
      }
    }
  }
}
array(2) {
  [0] =>
  array(2) {
    'rating' =>
    string(1) "4"
    'userComment' =>
    string(27) "Best translation I've read."
  }
  [1] =>
  array(2) {
    'rating' =>
    string(1) "2"
    'userComment' =>
    string(29) "I like other versions better."
  }
}

Additionally, you could make use of ->withSeparatedParametersList() to get the params of each element separated on the __params property. Also, ->withoutSkippingFirstElement() could be of help to parse the very first item (usually the element that contains the elements)., (*9)

JSON

[
  {
    "title": "The Iliad and The Odyssey",
    "price": 12.95,
    "comments": [
      {"comment": "Best translation I've read."},
      {"comment": "I like other versions better."}
    ]
  },
  {
    "title": "Anthology of World Literature",
    "price": 24.95,
    "comments": [
      {"comment": "Needs more modern literature."},
      {"comment": "Excellent overview of world literature."}
    ]
  }
]
use Illuminate\Support\Collection;

StreamParser::json("https://example.com/books.json")->each(function(Collection $book){
    var_dump($book->get('comments')->count());
});
int(2)
int(2)

CSV

title,price,comments
The Iliad and The Odyssey,12.95,"Best translation I've read.,I like other versions better."
Anthology of World Literature,24.95,"Needs more modern literature.,Excellent overview of world literature."
use Illuminate\Support\Collection;

StreamParser::csv("https://example.com/books.csv")->each(function(Collection $book){
    var_dump($book->get('comments')->last());
});
string(29) "I like other versions better."
string(39) "Excellent overview of world literature."

License

This library is released under MIT license., (*10)

The Versions

29/07 2018

dev-csvEmptyLinesFix

dev-csvEmptyLinesFix

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

29/07 2018

dev-master

9999999-dev

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser php json xml collections php7 stream pull

29/07 2018

2.0.x-dev

2.0.9999999.9999999-dev

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

29/07 2018

1.0.5

1.0.5.0

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

18/07 2018

1.0.4

1.0.4.0

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

15/07 2018

1.0.3

1.0.3.0

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

13/07 2018

1.0.2

1.0.2.0

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

03/06 2018

1.0.1

1.0.1.0

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser json xml php7 stream pull

16/05 2018

1.0.0

1.0.0.0

PHP Multiformat Streaming Parser

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel csv parser php json xml collections stream pull