-
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.
[Array] Adding array on import (#99)
* allow with fixed separator add simple array import + custom separator use form_help + default help message fix help on all type + test fix double check array space * add tests * separator placeholder in translation file * update test with translate param * add tests --------- Co-authored-by: Jarosław Grygierek <jaroslawgrygierek@gmail.com>
- Loading branch information
1 parent
c8f0e75
commit f3902f4
Showing
17 changed files
with
570 additions
and
34 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
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JG\BatchEntityImportBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use UnexpectedValueException; | ||
|
||
class ArrayTextType extends AbstractType implements DataTransformerInterface | ||
{ | ||
public const DEFAULT_SEPARATOR = '|'; | ||
private string $separator = self::DEFAULT_SEPARATOR; | ||
|
||
public function __construct(private readonly TranslatorInterface $translator) | ||
{ | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'compound' => false, | ||
'separator' => self::DEFAULT_SEPARATOR, | ||
]); | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$this->separator = $options['separator'] ?? self::DEFAULT_SEPARATOR; | ||
} | ||
|
||
public function transform(mixed $value): string | ||
{ | ||
if (!\is_array($value)) { | ||
throw new UnexpectedValueException('Only arrays are allowed'); | ||
} | ||
|
||
return implode($this->separator, $value); | ||
} | ||
|
||
public function reverseTransform(mixed $value): array | ||
{ | ||
if (!is_string($value)) { | ||
throw new UnexpectedValueException('Only strings are allowed'); | ||
} | ||
|
||
return $value | ||
? explode($this->separator ?: self::DEFAULT_SEPARATOR, $value) | ||
: []; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'array_text'; | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$view->vars['help'] = $this->translator->trans( | ||
'form.separator', | ||
['%separator%' => $options['separator']], | ||
'BatchEntityImportBundle', | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.