-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jordan Hall
committed
Apr 16, 2018
1 parent
0f90dad
commit c06ac87
Showing
4 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |