PHP 7 Multi-format Streaming Parser

PHP 7 Multi-format Streaming Parser

PHP 7 Stream Parser is a package by Sergio Ródenas for PHP multi-format stream parsing. Using streams with a pull parser is more efficient in large documents than DOM loading.

Here’s an example of how you might use the parser with a Laravel queue:

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

Given the following XML data example, it becomes clear how this stream parser works with Collections:

<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>

And an example of the PHP code and output:

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

// Output
class Illuminate\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 Illuminate\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."
  }
}

Here’s the same example using CSV data:

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."

And an example of the stream parser code:

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

// Output
string(29) "I like other versions better."
string(39) "Excellent overview of world literature."

Learn More

You can learn more about this package on GitHub and install it with composer require Rodenastyle/stream-parser. The README also mentions this article, which explains processing XML files via DOM vs. Streaming.

Filed in: News
Laravel News Partners

Laravel Jobs

PHP Developer - Full time w/ benefits, US Only
Northern Virginia or Remote (US Only)
Guidance Residential
Full Stack Developer
Remote
Wehaa.com
Laravel API Developer
Las Vegas, NV (Must work on-site)
V Shred
Laravel Developer (Vue, SQL Server)
Remote
World Trade Organization
Web Application Developer (Vue.js & Laravel)
Remote
Apero Creative Development

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.