diff --git a/README.md b/README.md index 13af1d1..616f5bf 100644 --- a/README.md +++ b/README.md @@ -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) @@ -94,7 +94,7 @@ $importerFactory->addReader(new XlsxReader()); $importer = $importerFactory->getImporter('csv'); $importer->fromString('some,csv,string') - ->useImportClass(new YourImportClass()) + ->useImportConfiguration(new YourImportConfiguration()) ->import(); ``` @@ -102,17 +102,15 @@ $importer->fromString('some,csv,string') ## 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) { @@ -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 { @@ -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 { @@ -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 @@ -203,7 +198,7 @@ class UserImport $importer = $importerFactory->getImporter('csv'); $importer->fromFile('path/to/file.csv') - ->useImportClass(new YourImportClass()) + ->useImportConfiguration(new YourImportConfiguration()) ->import(); } } @@ -228,7 +223,7 @@ class UserImport $importer = $importerFactory->getImporter('csv'); $importer->fromString('some,csv,string') - ->useImportClass(new YourImportClass()) + ->useImportConfiguration(new YourImportConfiguration()) ->import(); } } @@ -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; @@ -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(); } diff --git a/UPGRADE.md b/UPGRADE.md index 35fdfe0..ace699c 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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. + diff --git a/src/Import.php b/src/ImportConfiguration.php similarity index 88% rename from src/Import.php rename to src/ImportConfiguration.php index 6f3d102..d0ce3d8 100644 --- a/src/Import.php +++ b/src/ImportConfiguration.php @@ -5,7 +5,7 @@ /** * @author Marko Kunic */ -interface Import +interface ImportConfiguration { public function map(array $item, array $additionalData); public function save(array $items, array $additionalData): void; diff --git a/src/Importer.php b/src/Importer.php index a6ad0fa..563829e 100644 --- a/src/Importer.php +++ b/src/Importer.php @@ -2,6 +2,7 @@ namespace KunicMarko\Importer; +use KunicMarko\Importer\Exception\InvalidArgumentException; use KunicMarko\Importer\Reader\Reader; use Iterator; @@ -16,9 +17,9 @@ final class Importer private $reader; /** - * @var Import + * @var ImportConfiguration */ - private $importClass; + private $importConfiguration; /** * @var array @@ -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; } @@ -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); } } @@ -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); } } diff --git a/tests/Fixtures/ChunkImportClass.php b/tests/Fixtures/ChunkImportConfiguration.php similarity index 82% rename from tests/Fixtures/ChunkImportClass.php rename to tests/Fixtures/ChunkImportConfiguration.php index b711d38..738b656 100644 --- a/tests/Fixtures/ChunkImportClass.php +++ b/tests/Fixtures/ChunkImportConfiguration.php @@ -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 */ -class ChunkImportClass extends TestCase implements Import, ChunkImport, BeforeImport +class ChunkImportConfiguration extends TestCase implements ImportConfiguration, ChunkImport, BeforeImport { public function before(Iterator $items, array $additionalData): Iterator { diff --git a/tests/Fixtures/ImportClass.php b/tests/Fixtures/ImportConfiguration.php similarity index 81% rename from tests/Fixtures/ImportClass.php rename to tests/Fixtures/ImportConfiguration.php index fbe4239..43e804a 100644 --- a/tests/Fixtures/ImportClass.php +++ b/tests/Fixtures/ImportConfiguration.php @@ -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 */ -class ImportClass extends TestCase implements Import, BeforeImport +class ImportConfiguration extends TestCase implements ImportConfigurationInterface, BeforeImport { public function before(Iterator $items, array $additionalData): Iterator { diff --git a/tests/Fixtures/ImportNestedJsonClass.php b/tests/Fixtures/ImportNestedJsonConfiguration.php similarity index 86% rename from tests/Fixtures/ImportNestedJsonClass.php rename to tests/Fixtures/ImportNestedJsonConfiguration.php index 882e951..5fbfa63 100644 --- a/tests/Fixtures/ImportNestedJsonClass.php +++ b/tests/Fixtures/ImportNestedJsonConfiguration.php @@ -8,7 +8,7 @@ /** * @author Marko Kunic */ -class ImportNestedJsonClass extends ImportClass +class ImportNestedJsonConfiguration extends ImportConfiguration { /** * @param ArrayIterator|Iterator $items diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index cc1ebdc..c8536bc 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -2,15 +2,15 @@ namespace KunicMarko\Importer\Tests; -use KunicMarko\Importer\Import; +use KunicMarko\Importer\ImportConfiguration as ImportConfigurationInterface; use KunicMarko\Importer\ImporterFactory; use KunicMarko\Importer\Reader\CsvReader; use KunicMarko\Importer\Reader\XlsxReader; use KunicMarko\Importer\Reader\JsonReader; use KunicMarko\Importer\Reader\XmlReader; -use KunicMarko\Importer\Tests\Fixtures\ChunkImportClass; -use KunicMarko\Importer\Tests\Fixtures\ImportClass; -use KunicMarko\Importer\Tests\Fixtures\ImportNestedJsonClass; +use KunicMarko\Importer\Tests\Fixtures\ChunkImportConfiguration; +use KunicMarko\Importer\Tests\Fixtures\ImportConfiguration; +use KunicMarko\Importer\Tests\Fixtures\ImportNestedJsonConfiguration; use PHPUnit\Framework\TestCase; /** @@ -24,9 +24,9 @@ class ImporterTest extends TestCase private $importerFactory; /** - * @var Import + * @var ImportConfigurationInterface */ - private $importClass; + private $importConfiguration; public function setUp() { @@ -36,7 +36,7 @@ public function setUp() $this->importerFactory->addReader(new XlsxReader()); $this->importerFactory->addReader(new XmlReader()); - $this->importClass = new ImportClass(); + $this->importConfiguration = new ImportConfiguration(); } public function testCsvImportFromFile(): void @@ -44,7 +44,7 @@ public function testCsvImportFromFile(): void $importer = $this->importerFactory->getImporter('csv'); $importer->fromFile(__DIR__ . '/Fixtures/fake.csv') - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -53,7 +53,7 @@ public function testCsvImportFromString(): void $importer = $this->importerFactory->getImporter('csv'); $importer->fromString(file_get_contents(__DIR__ . '/Fixtures/fake.csv')) - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -62,7 +62,7 @@ public function testXmlImportFromFile(): void $importer = $this->importerFactory->getImporter('xml'); $importer->fromFile(__DIR__ . '/Fixtures/fake.xml') - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -71,7 +71,7 @@ public function testXmlImportFromString(): void $importer = $this->importerFactory->getImporter('xml'); $importer->fromString(file_get_contents(__DIR__ . '/Fixtures/fake.xml')) - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -80,7 +80,7 @@ public function testJsonImportFromFile(): void $importer = $this->importerFactory->getImporter('json'); $importer->fromFile(__DIR__ . '/Fixtures/fake.json') - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -89,7 +89,7 @@ public function testJsonImportFromString(): void $importer = $this->importerFactory->getImporter('json'); $importer->fromString(file_get_contents(__DIR__ . '/Fixtures/fake.json')) - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -98,7 +98,7 @@ public function testNestedJsonImportFromFile(): void $importer = $this->importerFactory->getImporter('json'); $importer->fromFile(__DIR__ . '/Fixtures/fake_nested.json') - ->useImportClass(new ImportNestedJsonClass()) + ->useImportConfiguration(new ImportNestedJsonConfiguration()) ->import(); } @@ -107,7 +107,7 @@ public function testNestedJsonImportFromString(): void $importer = $this->importerFactory->getImporter('json'); $importer->fromString(file_get_contents(__DIR__ . '/Fixtures/fake_nested.json')) - ->useImportClass(new ImportNestedJsonClass()) + ->useImportConfiguration(new ImportNestedJsonConfiguration()) ->import(); } @@ -116,7 +116,7 @@ public function testExcelImportFromFile(): void $importer = $this->importerFactory->getImporter('xlsx'); $importer->fromFile(__DIR__ . '/Fixtures/fake.xlsx') - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -125,7 +125,7 @@ public function testWithAdditionalData(): void $importer = $this->importerFactory->getImporter('xlsx'); $importer->fromFile(__DIR__ . '/Fixtures/fake.xlsx') - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->withAdditionalData(['test' => 'testing']) ->import(); } @@ -138,7 +138,7 @@ public function testExcelImportFromString(): void $importer = $this->importerFactory->getImporter('xlsx'); $importer->fromString('nop') - ->useImportClass($this->importClass) + ->useImportConfiguration($this->importConfiguration) ->import(); } @@ -147,7 +147,7 @@ public function testChunkImport(): void $importer = $this->importerFactory->getImporter('csv'); $importer->fromFile(__DIR__ . '/Fixtures/fake.csv') - ->useImportClass(new ChunkImportClass()) + ->useImportConfiguration(new ChunkImportConfiguration()) ->import(); } @@ -159,6 +159,16 @@ public function testReaderNotFound(): void $this->importerFactory->getImporter('fake'); } + /** + * @expectedException \KunicMarko\Importer\Exception\InvalidArgumentException + */ + public function testNoImportConfiguration(): void + { + $importer = $this->importerFactory->getImporter('csv'); + + $importer->import(); + } + /** * @expectedException \KunicMarko\Importer\Exception\InvalidArgumentException */ @@ -166,7 +176,9 @@ public function testFileNotFound(): void { $importer = $this->importerFactory->getImporter('csv'); - $importer->fromFile('fake'); + $importer + ->useImportConfiguration(new ChunkImportConfiguration()) + ->fromFile('fake'); } /** @@ -176,7 +188,9 @@ public function testContentCantBeEmpty(): void { $importer = $this->importerFactory->getImporter('csv'); - $importer->fromString(''); + $importer + ->useImportConfiguration(new ChunkImportConfiguration()) + ->fromString(''); } /** @@ -186,7 +200,9 @@ public function testJsonEmptyImport(): void { $importer = $this->importerFactory->getImporter('json'); - $importer->import(); + $importer + ->useImportConfiguration(new ChunkImportConfiguration()) + ->import(); } /** @@ -196,7 +212,9 @@ public function testXlsxEmptyImport(): void { $importer = $this->importerFactory->getImporter('xlsx'); - $importer->import(); + $importer + ->useImportConfiguration(new ChunkImportConfiguration()) + ->import(); } /** @@ -206,7 +224,9 @@ public function testCsvEmptyImport(): void { $importer = $this->importerFactory->getImporter('csv'); - $importer->import(); + $importer + ->useImportConfiguration(new ChunkImportConfiguration()) + ->import(); } /** @@ -216,6 +236,8 @@ public function testXmlEmptyImport(): void { $importer = $this->importerFactory->getImporter('xml'); - $importer->import(); + $importer + ->useImportConfiguration(new ChunkImportConfiguration()) + ->import(); } }