Skip to content

Commit f613de4

Browse files
Merge pull request #60 from cristoforocervino/fix-text-transformer
Fix bug with `DataTransformers` and `AbstractTextInputBasedTransformer`
2 parents 4a68a18 + 39cd3b8 commit f613de4

10 files changed

+133
-15
lines changed

features/interactive.feature

+20
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,23 @@ Feature: It is possible to interactively fill in a form from the CLI
399399
[0] => blue
400400
)
401401
"""
402+
403+
Scenario: Default value with transformers
404+
Given I run the command "form:default_value_with_data_transformers" and I provide as input "Rue du Faubourg Saint-Honoré" with parameters
405+
| Parameter | Value |
406+
| --street | pennsylvania-ave-nw |
407+
Then the command has finished successfully
408+
And the output should contain
409+
"""
410+
Street [pennsylvania-ave-nw]:
411+
"""
412+
And the output should contain
413+
"""
414+
Array
415+
(
416+
[street] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Street Object
417+
(
418+
[value] => Rue du Faubourg Saint-Honoré
419+
)
420+
)
421+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Bridge\Transformer;
4+
5+
use Matthias\SymfonyConsoleForm\Console\Formatter\Format;
6+
use Matthias\SymfonyConsoleForm\Form\FormUtil;
7+
use Symfony\Component\Form\FormInterface;
8+
use Symfony\Contracts\Translation\TranslatorInterface;
9+
10+
abstract class AbstractChoiceTransformer extends AbstractTransformer
11+
{
12+
protected function defaultValueFrom(FormInterface $form)
13+
{
14+
$defaultValue = $form->getData();
15+
if (is_array($defaultValue)) {
16+
$defaultValue = implode(',', $defaultValue);
17+
}
18+
19+
return $defaultValue;
20+
}
21+
}

src/Bridge/Transformer/AbstractTextInputBasedTransformer.php

+5
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public function transform(FormInterface $form): Question
1313
{
1414
return new Question($this->questionFrom($form), $this->defaultValueFrom($form));
1515
}
16+
17+
protected function defaultValueFrom(FormInterface $form)
18+
{
19+
return $form->getViewData();
20+
}
1621
}

src/Bridge/Transformer/AbstractTransformer.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function __construct(TranslatorInterface $translator)
2222
$this->translator = $translator;
2323
}
2424

25+
abstract protected function defaultValueFrom(FormInterface $form);
26+
2527
protected function questionFrom(FormInterface $form): string
2628
{
2729
$translationDomain = $this->translationDomainFrom($form);
@@ -34,19 +36,6 @@ protected function questionFrom(FormInterface $form): string
3436
return $this->formattedQuestion($question, $this->defaultValueFrom($form), $help);
3537
}
3638

37-
/**
38-
* @return mixed
39-
*/
40-
protected function defaultValueFrom(FormInterface $form)
41-
{
42-
$defaultValue = $form->getData();
43-
if (is_array($defaultValue)) {
44-
$defaultValue = implode(',', $defaultValue);
45-
}
46-
47-
return $defaultValue;
48-
}
49-
5039
protected function translationDomainFrom(FormInterface $form): ?string
5140
{
5241
while ((null === $domain = $form->getConfig()->getOption('translation_domain')) && $form->getParent()) {

src/Bridge/Transformer/CheckboxTransformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\Console\Question\Question;
77
use Symfony\Component\Form\FormInterface;
88

9-
final class CheckboxTransformer extends AbstractTransformer
9+
final class CheckboxTransformer extends AbstractChoiceTransformer
1010
{
1111
public function transform(FormInterface $form): Question
1212
{

src/Bridge/Transformer/ChoiceTransformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
88
use Symfony\Component\Form\FormInterface;
99

10-
final class ChoiceTransformer extends AbstractTransformer
10+
final class ChoiceTransformer extends AbstractChoiceTransformer
1111
{
1212
public function transform(FormInterface $form): Question
1313
{

test/Form/Data/Street.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form\Data;
4+
5+
class Street implements \Stringable
6+
{
7+
public string $value;
8+
9+
public function __construct(string $street)
10+
{
11+
$this->value = $street;
12+
}
13+
14+
public function __toString(): string
15+
{
16+
return $this->value;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
6+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Street;
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\CallbackTransformer;
9+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
13+
class DefaultValueWithDataTransformersType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add('street', StreetType::class, [
19+
'label' => 'Street'
20+
]);
21+
}
22+
}

test/Form/StreetType.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
6+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Street;
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\DataTransformerInterface;
9+
use Symfony\Component\Form\Exception\TransformationFailedException;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
use Symfony\Component\OptionsResolver\OptionsResolver;
13+
14+
class StreetType extends AbstractType implements DataTransformerInterface
15+
{
16+
public function buildForm(FormBuilderInterface $builder, array $options)
17+
{
18+
$builder->addModelTransformer($this);
19+
}
20+
21+
public function getParent()
22+
{
23+
return TextType::class;
24+
}
25+
26+
public function transform(mixed $value)
27+
{
28+
return (string)$value;
29+
}
30+
31+
public function reverseTransform(mixed $value)
32+
{
33+
return new Street((string)$value);
34+
}
35+
}

test/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ services:
154154
tags:
155155
- { name: console.command }
156156

157+
default_value_with_data_transformers:
158+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
159+
arguments:
160+
- Matthias\SymfonyConsoleForm\Tests\Form\DefaultValueWithDataTransformersType
161+
- default_value_with_data_transformers
162+
tags:
163+
- { name: console.command }
164+
157165
nested_command:
158166
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
159167
arguments:

0 commit comments

Comments
 (0)