-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add di, factory, serializer aware, readme
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 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,31 @@ | ||
## Usage | ||
|
||
```php | ||
use WebChemistry\FormSerializer\FormSerializerAwareInterface; | ||
use WebChemistry\FormSerializer\TFormSerializer; | ||
|
||
class Form extends NetteForm implements FormSerializerAwareInterface { | ||
|
||
use TFormSerializer; | ||
|
||
} | ||
``` | ||
|
||
```php | ||
|
||
use Nette\Application\UI\Form; | ||
use WebChemistry\FormSerializer\FormSerializerFactoryInterface; | ||
|
||
class ArticleForm { | ||
|
||
public function __construct(private FormSerializerFactoryInterface $formSerializerFactory) {} | ||
|
||
public function create() { | ||
$serializer = $this->formSerializerFactory->create($form = new Form(), Article::class); | ||
|
||
return $form; | ||
} | ||
|
||
} | ||
|
||
``` |
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,21 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer\DI; | ||
|
||
use Nette\DI\CompilerExtension; | ||
use WebChemistry\FormSerializer\FormSerializerFactory; | ||
use WebChemistry\FormSerializer\FormSerializerFactoryInterface; | ||
|
||
final class FormSerializerExtension extends CompilerExtension | ||
{ | ||
|
||
public function loadConfiguration(): void | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$builder->addDefinition($this->prefix('factory')) | ||
->setType(FormSerializerFactoryInterface::class) | ||
->setFactory(FormSerializerFactory::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer; | ||
|
||
interface FormSerializerAwareInterface | ||
{ | ||
|
||
public function setSerializer(FormSerializer $serializer); | ||
|
||
} |
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,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Nette\Application\UI\Form; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
final class FormSerializerFactory implements FormSerializerFactoryInterface | ||
{ | ||
|
||
public function __construct( | ||
private Serializer $serializer, | ||
private EntityManagerInterface $em, | ||
) | ||
{ | ||
} | ||
|
||
public function create(Form $form, string $class): FormSerializer | ||
{ | ||
$serializer = new FormSerializer($form, $class, $this->serializer, $this->em); | ||
|
||
if ($form instanceof FormSerializerAwareInterface) { | ||
$form->setSerializer($serializer); | ||
} | ||
|
||
return $serializer; | ||
} | ||
|
||
} |
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 declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer; | ||
|
||
use Nette\Application\UI\Form; | ||
|
||
interface FormSerializerFactoryInterface | ||
{ | ||
|
||
public function create(Form $form, string $class): FormSerializer; | ||
|
||
} |
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,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer; | ||
|
||
trait TFormSerializer | ||
{ | ||
|
||
private FormSerializer $serializer; | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function setSerializer(FormSerializer $serializer) | ||
{ | ||
$this->serializer = $serializer; | ||
|
||
return $this; | ||
} | ||
|
||
public function getSerializer(): FormSerializer | ||
{ | ||
return $this->serializer; | ||
} | ||
|
||
} |