-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support specifying type of data_class in forms
- Loading branch information
Showing
9 changed files
with
159 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Form; | ||
|
||
/** | ||
* @template TData | ||
* | ||
* @implements FormTypeInterface<TData> | ||
*/ | ||
abstract class AbstractType implements FormTypeInterface | ||
{ | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Form; | ||
|
||
use Symfony\Component\Form\Extension\Core\Type\FormType; | ||
|
||
interface FormFactoryInterface | ||
{ | ||
/** | ||
* @template TFormType of FormTypeInterface<TData> | ||
* @template TData | ||
* | ||
* @param class-string<TFormType> $type | ||
* @param TData $data | ||
* @param array<string, mixed> $options | ||
* | ||
* @phpstan-return ($data is null ? FormInterface<null|TData> : FormInterface<TData>) | ||
* | ||
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
*/ | ||
public function create(string $type = FormType::class, $data = null, array $options = []): FormInterface; | ||
|
||
/** | ||
* @template TFormType of FormTypeInterface<TData> | ||
* @template TData | ||
* | ||
* @param class-string<TFormType> $type | ||
* @param TData $data | ||
* @param array<string, mixed> $options | ||
* | ||
* @phpstan-return ($data is null ? FormInterface<null|TData> : FormInterface<TData>) | ||
* | ||
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
*/ | ||
public function createNamed(string $name, string $type = FormType::class, $data = null, array $options = []): FormInterface; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
stubs/Symfony/Component/OptionsResolver/Exception/InvalidOptionsException.stub
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,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\OptionsResolver\Exception; | ||
|
||
class InvalidOptionsException extends \InvalidArgumentException | ||
{ | ||
} |
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,72 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace GenericFormDataType; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormFactoryInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class DataClass | ||
{ | ||
|
||
/** @var int */ | ||
public $foo; | ||
|
||
/** @var string */ | ||
public $bar; | ||
|
||
} | ||
|
||
/** | ||
* @extends AbstractType<DataClass> | ||
*/ | ||
class DataClassType extends AbstractType | ||
{ | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('foo', NumberType::class) | ||
->add('bar', TextType::class) | ||
; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver | ||
->setDefaults([ | ||
'data_class' => DataClass::class, | ||
]) | ||
; | ||
} | ||
|
||
} | ||
|
||
class FormFactoryAwareClass | ||
{ | ||
|
||
/** @var FormFactoryInterface */ | ||
private $formFactory; | ||
|
||
public function __construct(FormFactoryInterface $formFactory) | ||
{ | ||
$this->formFactory = $formFactory; | ||
} | ||
|
||
public function doSomething(): void | ||
{ | ||
$form = $this->formFactory->create(DataClassType::class, new DataClass()); | ||
assertType('GenericFormDataType\DataClass', $form->getData()); | ||
} | ||
|
||
public function doSomethingNullable(): void | ||
{ | ||
$form = $this->formFactory->create(DataClassType::class); | ||
assertType('GenericFormDataType\DataClass|null', $form->getData()); | ||
} | ||
|
||
} |