-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
4abde34
commit ecfbe84
Showing
18 changed files
with
334 additions
and
47 deletions.
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JG\BatchEntityImportBundle\Enums; | ||
|
||
enum CsvDelimiterEnum: string | ||
{ | ||
case SEMICOLON = ';'; | ||
case COMMA = ','; | ||
|
||
public static function asValues(): array | ||
{ | ||
return [ | ||
self::SEMICOLON->value, | ||
self::COMMA->value, | ||
]; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JG\BatchEntityImportBundle\Service; | ||
|
||
use JG\BatchEntityImportBundle\Enums\CsvDelimiterEnum; | ||
|
||
class CsvDelimiterDetector | ||
{ | ||
public function detect(string $csvContent): CsvDelimiterEnum | ||
{ | ||
$delimiter = $this->detectDelimiter($csvContent); | ||
|
||
return ',' === $delimiter | ||
? CsvDelimiterEnum::COMMA | ||
: CsvDelimiterEnum::SEMICOLON; | ||
} | ||
|
||
private function detectDelimiter(string $csvContent): string | ||
{ | ||
$delimiters = CsvDelimiterEnum::asValues(); | ||
$delimiterCount = array_fill_keys($delimiters, 0); | ||
|
||
foreach ($delimiters as $delimiter) { | ||
$delimiterCount[$delimiter] = substr_count($csvContent, $delimiter); | ||
} | ||
|
||
return array_search(max($delimiterCount), $delimiterCount, true); | ||
} | ||
} |
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
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JG\BatchEntityImportBundle\Tests\Enum; | ||
|
||
use JG\BatchEntityImportBundle\Enums\CsvDelimiterEnum; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CsvDelimiterEnumTest extends TestCase | ||
{ | ||
public function testEnum(): void | ||
{ | ||
$this->assertCount(2, CsvDelimiterEnum::cases()); | ||
$this->assertSame([';', ','], CsvDelimiterEnum::asValues()); | ||
$this->assertSame(';', CsvDelimiterEnum::SEMICOLON->value); | ||
$this->assertSame(',', CsvDelimiterEnum::COMMA->value); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
tests/Fixtures/Configuration/TranslatableEntityConfiguration.php
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JG\BatchEntityImportBundle\Tests\Fixtures\Configuration; | ||
|
||
use JG\BatchEntityImportBundle\Model\Configuration\AbstractImportConfiguration; | ||
use JG\BatchEntityImportBundle\Tests\Fixtures\Entity\TranslatableEntity; | ||
use JG\BatchEntityImportBundle\Validator\Constraints\DatabaseEntityUnique; | ||
|
||
class TranslatableEntityConfiguration extends AbstractImportConfiguration | ||
{ | ||
public function getEntityClassName(): string | ||
{ | ||
return TranslatableEntity::class; | ||
} | ||
|
||
public function getEntityTranslationRelationName(): ?string | ||
{ | ||
return 'translations'; | ||
} | ||
|
||
public function getMatrixConstraints(): array | ||
{ | ||
return [ | ||
new DatabaseEntityUnique(['entityClassName' => $this->getEntityClassName(), 'fields' => [ | ||
'test_private_property', | ||
'test_public_property', | ||
]]), | ||
new DatabaseEntityUnique(['entityClassName' => $this->getEntityClassName(), 'fields' => [ | ||
'test-private-property2', | ||
]]), | ||
]; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/Fixtures/Controller/ControllerWithBaseTranslatableConfiguration.php
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JG\BatchEntityImportBundle\Tests\Fixtures\Controller; | ||
|
||
use JG\BatchEntityImportBundle\Controller\ImportConfigurationAutoInjectInterface; | ||
use JG\BatchEntityImportBundle\Controller\ImportConfigurationAutoInjectTrait; | ||
use JG\BatchEntityImportBundle\Controller\ImportControllerTrait; | ||
use JG\BatchEntityImportBundle\Tests\Fixtures\Configuration\TranslatableEntityBaseConfiguration; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class ControllerWithBaseTranslatableConfiguration extends AbstractController implements ImportConfigurationAutoInjectInterface | ||
{ | ||
use ImportConfigurationAutoInjectTrait; | ||
use ImportControllerTrait; | ||
|
||
public function import(Request $request, ValidatorInterface $validator): Response | ||
{ | ||
return $this->doImport($request, $validator); | ||
} | ||
|
||
public function importSave(Request $request, TranslatorInterface $translator): Response | ||
{ | ||
return $this->doImportSave($request, $translator); | ||
} | ||
|
||
protected function redirectToImport(): RedirectResponse | ||
{ | ||
return $this->redirectToRoute('jg.batch_entity_import_bundle.test_controller.base_translatable_config.import'); | ||
} | ||
|
||
protected function getMatrixSaveActionUrl(): string | ||
{ | ||
return $this->generateUrl('jg.batch_entity_import_bundle.test_controller.base_translatable_config.import_save'); | ||
} | ||
|
||
protected function getImportConfigurationClassName(): string | ||
{ | ||
return TranslatableEntityBaseConfiguration::class; | ||
} | ||
} |
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
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,2 @@ | ||
testTranslationProperty:pl | ||
Lorem Ipsum |
Oops, something went wrong.