Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate feed in JSON format #14

Open
budrea opened this issue Jul 16, 2020 · 0 comments
Open

Generate feed in JSON format #14

budrea opened this issue Jul 16, 2020 · 0 comments

Comments

@budrea
Copy link
Contributor

budrea commented Jul 16, 2020

Please start by reading the documentation of the project, that explains the context.

What needs to be done

We want to allow the feed generator to output the feed in JSON format. The library, as it currently exists, already supports :

  • the XML format (the default option)
  • the CSV format

In order to add the new JSON format, we need to

$generator = new ProductGenerator('file://my-feed.json', 'json');

In order to keep things simple, the output is expected to contain 1 json-encoded product per line.
So, with an input product like this :

[
    'reference' => 123,
    'name' => 'test product',
    'price' => 10.99,
    'quantity' => 300,
]

We would have an output JSON file that looks like this :

{"reference": 123, "name": "test product", "price": 10.00, "quantity": 300}

With multiple products, the file will look like this :

{"reference": 1, "name": "test product 1", "price": 10.00, "quantity": 300}
{"reference": 2, "name": "test product 2", "price": 10.00, "quantity": 300}
{"reference": 3, "name": "test product 3", "price": 10.00, "quantity": 300}

How to test your code

Please use the following script to test your code :

<?php
namespace ShoppingFeed\Feed;

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

$generator = new ProductGenerator('php://output', 'json');

// in order for the generator to work, we need at least one mapper
// see the documentation for more details
$generator->addMapper(function(array $item, Product\Product $product) {
    $product
        ->setName($item['title'])
        ->setReference($item['sku'])
        ->setPrice($item['price'])
        ->setQuantity($item['quantity']);
});

// hardcode the data of your products here (or you can import it from an external file, if you wish)
$items[0] = ['sku' => 1, 'title' => 'Product 1', 'price' => 5.99, 'quantity' => 3];
$items[1] = ['sku' => 2, 'title' => 'Product 2', 'price' => 12.99, 'quantity' => 6];

// now generate the feed, which will output the file
// in the specified format and at the specified location
$generator->write($items);

If you place the above code, at the root of the project, in a file named test.php then, in the CLI you can position yourself at the root of the project as well and launch the following command :

/the/local/path/to/php-feed-generator$ php./test.php

(so, just to be clear, the test command is just php./test.php)

This should output at the specified location (php://output), the file, in the required format.

You can already test that the project works as expected, by replacing json with xml in this line

$generator = new ProductGenerator('php://output', 'xml');

Without any new code, this should already output the XML file. What we want is that, when using the new json format, we obtain the desired result.

How to propose a solution for this issue

In order to propose a solution for this issue, you can :

  • fork this project
  • recover the forked project locally
  • create a new branch
  • commit your code on this new branch
  • push it up to your forked repository
  • on this repository (the ShoppingFeed, central repository, which you forked before), you can open a Pull Request

More details here and here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant