Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DTO objects disconnected from Doctrine entities #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/Form/EventListener/TranslationsFormsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace A2lix\TranslationFormBundle\Form\EventListener;

use Doctrine\Common\Collections\Collection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -41,6 +42,25 @@ public function preSetData(FormEvent $event): void
}
}

private function translationIsEmpty($translation): bool
{
// default
if (empty($translation)) {
return true;
}

// knp
if (
\is_object($translation)
&& method_exists($translation, 'isEmpty')
&& $translation->isEmpty()
) {
return true;
}

return false;
}

public function submit(FormEvent $event): void
{
$form = $event->getForm();
Expand All @@ -50,10 +70,16 @@ public function submit(FormEvent $event): void

foreach ($data as $locale => $translation) {
// Remove useless Translation object
if ((method_exists($translation, 'isEmpty') && $translation->isEmpty() && !\in_array($locale, $formOptions['required_locales'], true)) // Knp
|| empty($translation) // Default
) {
$data->removeElement($translation);
if ($this->translationIsEmpty($translation) && !\in_array($locale, $formOptions['required_locales'], true)) {
if (\is_array($data) && \array_key_exists($locale, $data)) {
unset($data[$locale]);
$event->setData($data);
}

if ($data instanceof Collection) {
$data->removeElement($translation);
}

continue;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Fixtures/DTO/Form/ProductTranslationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TranslationFormBundle package.
*
* (c) David ALLIX <http://a2lix.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace A2lix\TranslationFormBundle\Tests\Fixtures\DTO\Form;

use A2lix\TranslationFormBundle\Tests\Fixtures\DTO\ProductTranslation;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductTranslationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title')
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => ProductTranslation::class,
]);
}
}
32 changes: 32 additions & 0 deletions tests/Fixtures/DTO/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TranslationFormBundle package.
*
* (c) David ALLIX <http://a2lix.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace A2lix\TranslationFormBundle\Tests\Fixtures\DTO;

class Product
{
/**
* @var int
*/
public $id;

/**
* @var array<string, ProductTranslation>
*/
public $translations = [];

/**
* @var string
*/
public $url;
}
61 changes: 61 additions & 0 deletions tests/Fixtures/DTO/ProductTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TranslationFormBundle package.
*
* (c) David ALLIX <http://a2lix.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace A2lix\TranslationFormBundle\Tests\Fixtures\DTO;

class ProductTranslation
{
/**
* @var int
*/
protected $id;

/**
* @var string
*/
protected $locale;

/**
* @var string|null
*/
protected $title;

public function getId(): ?int
{
return $this->id;
}

public function getLocale(): string
{
return $this->locale;
}

public function setLocale(string $locale): self
{
$this->locale = $locale;

return $this;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): self
{
$this->title = $title;

return $this;
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/Entity/MediaLocalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
namespace A2lix\TranslationFormBundle\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

/**
* @ORM\Entity
*/
class MediaLocalize
{
use TranslationTrait;

/**
* @ORM\Id
* @ORM\Column(type="integer")
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Product
protected $medias;

/**
* @ORM\OneToMany(targetEntity="ProductTranslation", mappedBy="object", indexBy="locale", cascade={"all"}, orphanRemoval=true)
* @ORM\OneToMany(targetEntity="ProductTranslation", mappedBy="translatable", indexBy="locale", cascade={"all"}, orphanRemoval=true)
*/
protected $translations;

Expand Down
Loading