Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Rename importClass to importConfiguration (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 committed Jul 22, 2018
1 parent 56e1555 commit 0fd70f3
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 71 deletions.
45 changes: 20 additions & 25 deletions README.md
Expand Up @@ -21,7 +21,7 @@ Documentation
* [Lumen](#lumen)
* [Without Framework](#without-framework)
* [How to use](#how-to-use)
* [ImportClass](#importclass)
* [ImportConfiguration](#importconfiguration)
* [BeforeImport](#beforeimport)
* [ChunkImport](#chunkimport)
* [Import](#import)
Expand Down Expand Up @@ -94,25 +94,23 @@ $importerFactory->addReader(new XlsxReader());

$importer = $importerFactory->getImporter('csv');
$importer->fromString('some,csv,string')
->useImportClass(new YourImportClass())
->useImportConfiguration(new YourImportConfiguration())
->import();
```

> If you want to use excel import, install "phpoffice/phpspreadsheet".
## How to use

### ImportClass
### ImportConfiguration

Import class defines how should the data be mapped and saves the data. They have to implement
`KunicMarko\Importer\Import` interface.
ImportConfiguration defines how should the data be mapped and saves the data.
They have to implement `KunicMarko\Importer\ImportConfiguration` interface.

```php
namespace KunicMarko\Importer\Tests\Fixtures;
use KunicMarko\Importer\ImportConfiguration;

use KunicMarko\Importer\Import;

class ImportClass implements Import
class ImportUserConfiguration implements ImportConfiguration
{
public function map(array $item, array $additionalData)
{
Expand All @@ -133,16 +131,14 @@ class ImportClass implements Import

#### BeforeImport

BeforeImport allows your ImportClass to do something with data before the mapping starts.
BeforeImport allows your ImportConfiguration to do something with data before the mapping starts.

```php
namespace KunicMarko\Importer\Tests\Fixtures;

use KunicMarko\Importer\Import;
use KunicMarko\Importer\ImportConfiguration;
use KunicMarko\Importer\BeforeImport;
use Iterator;

class ImportClass implements Import, BeforeImport
class ImportSomethingConfiguration implements ImportConfiguration, BeforeImport
{
public function before(Iterator $items, array $additionalData): Iterator
{
Expand All @@ -156,16 +152,15 @@ class ImportClass implements Import, BeforeImport

#### ChunkImport

ChunkImport allows your class to define a number of items that the save method will receive,
instead of receiving all at once.
ChunkImport allows your configuration to define a number of items that the save method will
receive, instead of receiving all at once.

```php
namespace KunicMarko\Importer\Tests\Fixtures;

use KunicMarko\Importer\Import;
use KunicMarko\Importer\ImportConfiguration;
use KunicMarko\Importer\ChunkImport;

class ImportClass implements Import, ChunkImport
class ImportChunkSomethingConfiguration implements ImportConfiguration, ChunkImport
{
public function chunkSize(): int
{
Expand All @@ -181,8 +176,8 @@ class ImportClass implements Import, ChunkImport

### Import

After you have defined your import class, you can import from a file or from a string. You HAVE to
provide one of those 2 options and your import class.
After you have defined your import configuration, you can import from a file or from a string.
You HAVE to provide one of those 2 options and your import configuration.

#### Import From File

Expand All @@ -203,7 +198,7 @@ class UserImport
$importer = $importerFactory->getImporter('csv');

$importer->fromFile('path/to/file.csv')
->useImportClass(new YourImportClass())
->useImportConfiguration(new YourImportConfiguration())
->import();
}
}
Expand All @@ -228,7 +223,7 @@ class UserImport
$importer = $importerFactory->getImporter('csv');

$importer->fromString('some,csv,string')
->useImportClass(new YourImportClass())
->useImportConfiguration(new YourImportConfiguration())
->import();
}
}
Expand All @@ -238,7 +233,7 @@ class UserImport
### Pass Additional Data

Sometimes you may want to pass additional data to your import class.
Sometimes you may want to pass additional data to your import configuration.

```php
use KunicMarko\Importer\ImporterFactory;
Expand All @@ -257,7 +252,7 @@ class UserImport
$importer = $importerFactory->getImporter('csv');

$importer->fromString('some,csv,string')
->useIamportClass(new YourImportClass())
->useImportConfiguration(new YourImportConfiguration())
->withAdditionalData(['user' => 'kunicmarko20'])
->import();
}
Expand Down
11 changes: 9 additions & 2 deletions UPGRADE.md
Expand Up @@ -3,9 +3,16 @@
UPGRADE FROM 0.1.0 to 0.2.0
===========================

`KunicMarko\Importer\Import` has been renamed to `KunicMarko\Importer\ImportConfiguration`.

`Importer::useImportClass` has been renamed to `Importer::useImportConfiguration`.

`Importer::import` now throws exception if ImportConfiguration is not provided.

`BeforeImport::before` now accepts second parameter `$additionalData`.
`Import::map` now accepts second parameter `$additionalData`.
`Import::save` now accepts second parameter `$additionalData`.
`ImportConfiguration::map` now accepts second parameter `$additionalData`.
`ImportConfiguration::save` now accepts second parameter `$additionalData`.

New method `Importer::withAdditionalData` that allows you to pass any additional data
you need to get into your import class.

2 changes: 1 addition & 1 deletion src/Import.php → src/ImportConfiguration.php
Expand Up @@ -5,7 +5,7 @@
/**
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
interface Import
interface ImportConfiguration
{
public function map(array $item, array $additionalData);
public function save(array $items, array $additionalData): void;
Expand Down
31 changes: 18 additions & 13 deletions src/Importer.php
Expand Up @@ -2,6 +2,7 @@

namespace KunicMarko\Importer;

use KunicMarko\Importer\Exception\InvalidArgumentException;
use KunicMarko\Importer\Reader\Reader;
use Iterator;

Expand All @@ -16,9 +17,9 @@ final class Importer
private $reader;

/**
* @var Import
* @var ImportConfiguration
*/
private $importClass;
private $importConfiguration;

/**
* @var array
Expand Down Expand Up @@ -51,25 +52,29 @@ public function withAdditionalData(array $additionalData): self
return $this;
}

public function useImportClass(Import $importClass): self
public function useImportConfiguration(ImportConfiguration $importConfiguration): self
{
$this->importClass = $importClass;
$this->importConfiguration = $importConfiguration;

return $this;
}

public function import(): void
{
if (!$this->importConfiguration) {
throw new InvalidArgumentException('You must provide ImportConfiguration.');
}

$items = $this->reader->getItems();

if ($this->importClass instanceof BeforeImport) {
$items = $this->importClass->before($items, $this->additionalData);
if ($this->importConfiguration instanceof BeforeImport) {
$items = $this->importConfiguration->before($items, $this->additionalData);
}

if ($this->importClass instanceof ChunkImport) {
if ($this->importConfiguration instanceof ChunkImport) {
$this->importChunkItems(
$items,
$this->importClass->chunkSize()
$this->importConfiguration->chunkSize()
);
return;
}
Expand All @@ -84,17 +89,17 @@ private function importChunkItems(Iterator $items, int $chunkSize): void
$i = 0;

for (; $items->valid(); $items->next()) {
$mappedItems[] = $this->importClass->map($items->current(), $this->additionalData);
$mappedItems[] = $this->importConfiguration->map($items->current(), $this->additionalData);

if (++$i === $chunkSize) {
$this->importClass->save($mappedItems, $this->additionalData);
$this->importConfiguration->save($mappedItems, $this->additionalData);
$mappedItems = [];
$i = 0;
}
}

if ($mappedItems) {
$this->importClass->save($mappedItems, $this->additionalData);
$this->importConfiguration->save($mappedItems, $this->additionalData);
}
}

Expand All @@ -103,9 +108,9 @@ private function importItems(Iterator $items): void
$mappedItems = [];

for (; $items->valid(); $items->next()) {
$mappedItems[] = $this->importClass->map($items->current(), $this->additionalData);
$mappedItems[] = $this->importConfiguration->map($items->current(), $this->additionalData);
}

$this->importClass->save($mappedItems, $this->additionalData);
$this->importConfiguration->save($mappedItems, $this->additionalData);
}
}
Expand Up @@ -5,14 +5,14 @@
use Iterator;
use KunicMarko\Importer\BeforeImport;
use KunicMarko\Importer\ChunkImport;
use KunicMarko\Importer\Import;
use KunicMarko\Importer\ImportConfiguration;
use PHPUnit\Framework\TestCase;
use function count;

/**
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
class ChunkImportClass extends TestCase implements Import, ChunkImport, BeforeImport
class ChunkImportConfiguration extends TestCase implements ImportConfiguration, ChunkImport, BeforeImport
{
public function before(Iterator $items, array $additionalData): Iterator
{
Expand Down
Expand Up @@ -4,13 +4,13 @@

use Iterator;
use KunicMarko\Importer\BeforeImport;
use KunicMarko\Importer\Import;
use KunicMarko\Importer\ImportConfiguration as ImportConfigurationInterface;
use PHPUnit\Framework\TestCase;

/**
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
class ImportClass extends TestCase implements Import, BeforeImport
class ImportConfiguration extends TestCase implements ImportConfigurationInterface, BeforeImport
{
public function before(Iterator $items, array $additionalData): Iterator
{
Expand Down
Expand Up @@ -8,7 +8,7 @@
/**
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
class ImportNestedJsonClass extends ImportClass
class ImportNestedJsonConfiguration extends ImportConfiguration
{
/**
* @param ArrayIterator|Iterator $items
Expand Down

0 comments on commit 0fd70f3

Please sign in to comment.