Skip to content

Commit

Permalink
Markdown destination (closes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed Apr 16, 2018
1 parent 0f90dad commit c06ac87
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
33 changes: 33 additions & 0 deletions docs/destinations/MarkdownDestination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Markdown Destination

The UXDM Markdown destination allows you to migrate data into a Markdown table.

## Creating

To create a new Markdown destination, you must provide it with the file path of Markdown file you wish to export data to. The
first line of the Markdown table will contain the field names.

The following example creates a Markdown destination object, using a Markdown file called `users.md` in the same directory.

```php
$markdownFile = __DIR__.'/users.md';
$markdownDestination = new MarkdownDestination($markdownFile);
```

## Assigning to migrator

To use the Markdown destination as part of a UXDM migration, you must assign it to the migrator. This process is the same for most destinations.

```php
$migrator = new Migrator;
$migrator->setDestination($markdownDestination);
```

Alternatively, you can add multiple destinations, as shown below. You can also specify the fields you wish to send to each destination by
passing an array of field names as the second parameter.

```php
$migrator = new Migrator;
$migrator->addDestination($markdownDestination, ['field1', 'field2']);
$migrator->addDestination($otherDestination, ['field3', 'field2']);
```
3 changes: 2 additions & 1 deletion docs/uxdm-sources-and-destinations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ For general documentation and usage examples, see the main [README](../README.md
* [XML Destination](destinations/XMLDestination.md)
* [JSON Files Destination](destinations/JSONFilesDestination.md)
* [Associative Array Destination](destinations/AssociativeArrayDestination.md)
* [Markdown Destination](destinations/MarkdownDestination.md)
* [Debug Output Destination](destinations/DebugOutputDestination.md)
* [Null Destination](destinations/NullDestination.md)
* [Null Destination](destinations/NullDestination.md)
46 changes: 46 additions & 0 deletions src/Objects/Destinations/MarkdownDestination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace RapidWeb\uxdm\Objects\Destinations;

use RapidWeb\uxdm\Interfaces\DestinationInterface;

class MarkdownDestination implements DestinationInterface
{
private $file;
private $rowNum = 0;

public function __construct($file)
{
$this->file = $file;
}

public function putDataRows(array $dataRows)
{
if ($this->rowNum === 0) {
$fh = fopen($this->file, 'w');
} else {
$fh = fopen($this->file, 'a');
}

foreach ($dataRows as $dataRow) {
$dataItems = $dataRow->getDataItems();

if ($this->rowNum === 0) {
$fieldNames = [];
foreach ($dataItems as $dataItem) {
$fieldNames[] = $dataItem->fieldName;
}
fwrite($fh, implode(' | ', $fieldNames).PHP_EOL);
fwrite($fh, substr(str_repeat('--- | ', count($fieldNames)), 0, -2).PHP_EOL);
}

$values = [];
foreach ($dataItems as $dataItem) {
$values[] = str_replace('|', '\\|', $dataItem->value);
}
fwrite($fh, implode(' | ', $values).PHP_EOL);

$this->rowNum++;
}
}
}
67 changes: 67 additions & 0 deletions tests/Unit/MarkdownDestinationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use PHPUnit\Framework\TestCase;
use RapidWeb\uxdm\Objects\DataItem;
use RapidWeb\uxdm\Objects\DataRow;
use RapidWeb\uxdm\Objects\Destinations\MarkdownDestination;

final class MarkdownDestinationTest extends TestCase
{
private function createDataRows()
{
$faker = Faker\Factory::create();

$dataRows = [];

$dataRow = new DataRow();
$dataRow->addDataItem(new DataItem('name', $faker->word));
$dataRow->addDataItem(new DataItem('value', $faker->randomNumber));
$dataRows[] = $dataRow;

$dataRow = new DataRow();
$dataRow->addDataItem(new DataItem('name', $faker->word));
$dataRow->addDataItem(new DataItem('value', $faker->randomNumber));
$dataRows[] = $dataRow;

$dataRow = new DataRow();
$dataRow->addDataItem(new DataItem('name', $faker->word.' | '.$faker->word));
$dataRow->addDataItem(new DataItem('value', $faker->randomNumber));
$dataRows[] = $dataRow;

$dataRow = new DataRow();
$dataRow->addDataItem(new DataItem('name', $faker->word));
$dataRow->addDataItem(new DataItem('value', $faker->randomNumber.' | '.$faker->randomNumber));
$dataRows[] = $dataRow;

return $dataRows;
}

private function getExpectedFileContent(array $dataRows)
{
$expectedFileContent = 'name | value'.PHP_EOL;
$expectedFileContent .= '--- | --- '.PHP_EOL;

foreach ($dataRows as $dataRow) {
$expectedFileContent .= str_replace('|', '\|', $dataRow->getDataItemByFieldName('name')->value);
$expectedFileContent .= ' | ';
$expectedFileContent .= str_replace('|', '\|', $dataRow->getDataItemByFieldName('value')->value);
$expectedFileContent .= PHP_EOL;
}

return $expectedFileContent;
}

public function testPutDataRows()
{
$dataRows = $this->createDataRows();

$file = __DIR__.'/Data/destination.md';

$destination = new MarkdownDestination($file);
$destination->putDataRows($dataRows);

$fileContent = file_get_contents($file);

$this->assertEquals($this->getExpectedFileContent($dataRows), $fileContent);
}
}

0 comments on commit c06ac87

Please sign in to comment.