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

Mapping converter #142

Merged
merged 2 commits into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Documentation
- [StringToObjectConverter](#stringtoobjectconverter)
- [ArrayValueConverterMap](#arrayvalueconvertermap)
- [CallbackValueConverter](#callbackvalueconverter)
- [MappingValueConverter](#mappingvalueconverter)
* [Examples](#examples)
- [Import CSV file and write to database](#import-csv-file-and-write-to-database)
- [Export to CSV file](#export-to-csv-file)
Expand Down Expand Up @@ -330,7 +331,7 @@ $reader = new ExcelReader($file);

###OneToManyReader

Allows for merging of two data sources (using existing readers), for example you have one CSV with orders and another with order items.
Allows for merging of two data sources (using existing readers), for example you have one CSV with orders and another with order items.

Imagine two CSV's like the following:

Expand All @@ -348,7 +349,7 @@ OrderId,Name
```

You want to associate the items to the order. Using the OneToMany reader we can nest these rows in the order using a key
which you specify in the OneToManyReader.
which you specify in the OneToManyReader.

The code would look something like:

Expand All @@ -364,9 +365,9 @@ $orderItemReader->setHeaderRowNumber(0);
$oneToManyReader = new OneToManyReader($orderReader, $orderItemReader, 'items', 'OrderId', 'OrderId');
```

The third parameter is the key which the order item data will be nested under. This will be an array of order items.
The third parameter is the key which the order item data will be nested under. This will be an array of order items.
The fourth and fifth parameters are "primary" and "foreign" keys of the data. The OneToMany reader will try to match the data using these keys.
Take for example the CSV's given above, you would expect that Order "1" has the first 2 Order Items associated to it due to their Order Id's also
Take for example the CSV's given above, you would expect that Order "1" has the first 2 Order Items associated to it due to their Order Id's also
being "1".

Note: You can omit the last parameter, if both files have the same field. Eg if parameter 4 is 'OrderId' and you don't specify
Expand All @@ -377,30 +378,30 @@ The resulting data will look like:
```php
//Row 1
array(
'OrderId' => 1,
'Price' => 30,
'items' => array(
array(
'OrderId' => 1,
'Name' => 'Super Cool Item 1',
),
array(
'OrderId' => 1,
'Name' => 'Super Cool Item 2',
),
),
'OrderId' => 1,
'Price' => 30,
'items' => array(
array(
'OrderId' => 1,
'Name' => 'Super Cool Item 1',
),
array(
'OrderId' => 1,
'Name' => 'Super Cool Item 2',
),
),
);

//Row2
array(
'OrderId' => 2,
'Price' => 15,
'items' => array(
array(
'OrderId' => 2,
'Name' => 'Super Cool Item 1',
),
)
'OrderId' => 2,
'Price' => 15,
'items' => array(
array(
'OrderId' => 2,
'Name' => 'Super Cool Item 1',
),
)
);
```

Expand Down Expand Up @@ -972,6 +973,21 @@ $converter = new CallbackValueConverter($callable);
$output = $converter->convert(array('foo', 'bar')); // $output will be "foo,bar"
```

#### MappingValueConverter

Looks for a key in a hash you must provide in the constructor:

```php
use Ddeboer\DataImport\ValueConverter\MappingValueConverter;

$converter = new MappingValueConverter(array(
'source' => 'destination'
));

$converter->convert('source'); // destination
$converter->convert('unexpected value'); // throws an UnexpectedValueException
```

### Examples

#### Import CSV file and write to database
Expand Down
27 changes: 27 additions & 0 deletions src/Ddeboer/DataImport/ValueConverter/MappingValueConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Ddeboer\DataImport\ValueConverter;

use Ddeboer\DataImport\Exception\UnexpectedValueException;

class MappingValueConverter implements ValueConverterInterface
{
private $mapping;

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

public function convert($input)
{
if (!isset($this->mapping[$input])) {
throw new UnexpectedValueException(sprintf(
'Cannot find mapping for value "%s"',
$input
));
}

return $this->mapping[$input];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Ddeboer\DataImport\Tests\ValueConverter;

use Ddeboer\DataImport\ValueConverter\MappingValueConverter;

class MappingValueConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException Ddeboer\DataImport\Exception\UnexpectedValueException
* @expectedExceptionMessage Cannot find mapping for value "unexpected value"
*/
public function testConvert()
{
$converter = new MappingValueConverter(array(
'source' => 'destination'
));

$this->assertSame('destination', $converter->convert('source'));
$converter->convert('unexpected value');
}
}