1. Go to this page and download the library: Download survos/jsonl-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
declare(strict_types=1);
namespace App\Command;
use Survos\JsonlBundle\IO\JsonlReader;
use Survos\JsonlBundle\IO\JsonlWriter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpClient\HttpClient;
#[AsCommand('products:list', 'List products using a JSONL cache')]
final class ProductsListCommand
{
public function __invoke(SymfonyStyle $io): int
{
$file = 'var/products.jsonl';
if (!file_exists($file)) {
$client = HttpClient::create();
$data = $client->request('GET', 'https://dummyjson.com/products')->toArray();
$writer = JsonlWriter::open($file);
foreach ($data['products'] as $product) {
$writer->write($product);
}
$writer->close();
}
$reader = JsonlReader::open($file);
foreach ($reader as $product) {
$io->title($product['title']);
$io->listing([
'Price: ' . $product['price'],
'Category: ' . $product['category'],
]);
break; // first product only
}
return Command::SUCCESS;
}
}
$file = 'var/products.jsonl.gz';
$reader = JsonlReader::openArchive('/cache/raw.bagit.tar', 'data/raw/obj.jsonl.gz');
foreach ($reader as $row) {
// Same decoded rows as an ordinary local JSONL file.
}