7 Stream is a package by Sergio Ródenas for 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 AppJobsSendEmail($user));
});

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

<bookstore>
    <book ISBN="-000000-00">
        <title>The Iliad and The Odyssey</title>
        <price>2.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 IlluminateSupportCollection#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 IlluminateSupportCollection#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() "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  Literature,24.95,"Needs more modern literature.,Excellent overview of  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.



Source link
thanks you RSS link
( https://laravel-news.com/php-7-multi-format--parser)

LEAVE A REPLY

Please enter your comment!
Please enter your name here