Repository navigation
Output Text
We have provided 3 standard output formats: JSON, CSV and XML.
First we need to create the factory:
$factory = new TextFactory(new AdapterFactory);
Next we need to set the adapter using the make method and passing through which adapter we would like.
$text = $factory->make(TextEnum::JSON);
Now, just call output and pass through the data you need.
$text->output($data);
So this is all we have:
$factory = new TextFactory(new AdapterFactory);
$text = $factory->make(TextEnum::JSON);
$text->output($data);
The $data variable would be an array you wish to make into JSON.
We need to do the exact same as above, except for this, we need to change the TextEnum::JSON to TextEnum::CSV. Like so:
$text = $factory->make(TextEnum::CSV);
So all the code is:
$factory = new TextFactory(new AdapterFactory);
$text = $factory->make(TextEnum::CSV);
$text->output($data);
THe $data variable expects an array of arrays. Each array represents a row, and each item in the array represents a column.
Again, do the same as above and just change the TextEnum::CSV to TextEnum::XML.
So all the code is:
$factory = new TextFactory(new AdapterFactory);
$text = $factory->make(TextEnum::XML);
$text->output($data);
Here, the $data variable will convert an array of objects into XML. Something like this:
$xml = [
'folders' => [
['name' => 'Folder 1', 'id' => '1', 'files' => [
['name' => 'File 1', 'id' => '1', 'type' => 'psd'],
['name' => 'File 2', 'id' => '2', 'type' => 'csv'],
['name' => 'File 3', 'id' => '3', 'type' => 'pdf'],
]],
['name' => 'Folder 2', 'id' => '2'],
['name' => 'Folder 3', 'id' => '3', 'files' => [
['name' => 'File 1', 'id' => '1', 'type' => 'txt'],
['name' => 'File 2', 'id' => '2', 'type' => 'csv'],
]],
],
'files' => [
['name' => 'My public file']
]];