diff --git a/README.md b/README.md index dcdc109..4cc7ea6 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ Requirements ------------ * Symfony flex with Symfony 3.4|4.0 * i18n Doctrine strategy of your choice - * [KnpDoctrineExtension](https://github.com/KnpLabs/DoctrineBehaviors#translatable) + * [KnpLabs/DoctrineBehaviors](https://github.com/KnpLabs/DoctrineBehaviors#translatable) with `knplabs/doctrine-behaviors` + * [Atlantic18/DoctrineExtensions](https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/translatable.md) with `stof/doctrine-extensions-bundle` * [A2lixI18nDoctrineBundle](https://github.com/a2lix/I18nDoctrineBundle) [Deprecated - will be removed from bundle in v.4.0.6] * [PrezentDoctrineTranslatableBundle](https://github.com/Prezent/doctrine-translatable-bundle/blob/master/Resources/doc/index.md) [Deprecated - will be removed from bundle in v.4.0.6] diff --git a/composer.json b/composer.json index c456d2c..9e64134 100755 --- a/composer.json +++ b/composer.json @@ -41,8 +41,7 @@ }, "suggest": { "knplabs/doctrine-behaviors": "For Knp strategy", - "a2lix/i18n-doctrine-bundle": "For A2lix strategy", - "prezent/doctrine-translatable-bundle": "For Prezent strategy" + "stof/doctrine-extensions-bundle": "For Atlantic18/DoctrineExtensions" }, "config": { "preferred-install": { diff --git a/src/ObjectInfo/DoctrineInfo.php b/src/Extractor/DoctrineEntityFieldsExtractor.php similarity index 92% rename from src/ObjectInfo/DoctrineInfo.php rename to src/Extractor/DoctrineEntityFieldsExtractor.php index 3ad5be7..5eff337 100644 --- a/src/ObjectInfo/DoctrineInfo.php +++ b/src/Extractor/DoctrineEntityFieldsExtractor.php @@ -1,12 +1,18 @@ + */ +class DoctrineEntityFieldsExtractor implements FieldsExtractorInterface { /** @var EntityManagerInterface */ private $entityManager; diff --git a/src/Extractor/FieldsExtractorInterface.php b/src/Extractor/FieldsExtractorInterface.php new file mode 100644 index 0000000..2621cef --- /dev/null +++ b/src/Extractor/FieldsExtractorInterface.php @@ -0,0 +1,15 @@ + + */ +interface FieldsExtractorInterface +{ + public function getFieldsConfig($class); + + public function getAssociationTargetClass($class, $fieldName); +} diff --git a/src/Form/EventListener/KoffI18nListener.php b/src/Form/EventListener/KoffI18nListener.php index 52ef442..7f281c2 100644 --- a/src/Form/EventListener/KoffI18nListener.php +++ b/src/Form/EventListener/KoffI18nListener.php @@ -6,6 +6,11 @@ use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +/** + * Class KoffI18nListener. + * + * @author Sadicov Vladimir + */ abstract class KoffI18nListener implements EventSubscriberInterface { /** diff --git a/src/Form/EventListener/TranslationsFormsListener.php b/src/Form/EventListener/TranslationsFormsListener.php index 8c004be..7e78dd5 100644 --- a/src/Form/EventListener/TranslationsFormsListener.php +++ b/src/Form/EventListener/TranslationsFormsListener.php @@ -12,21 +12,15 @@ namespace Koff\Bundle\I18nFormBundle\Form\EventListener; use Symfony\Component\Form\FormEvent; -use Symfony\Component\Form\FormEvents; +/** + * Class TranslationsFormsListener. + * + * @author David ALLIX + * @author Sadicov Vladimir + */ class TranslationsFormsListener extends KoffI18nListener { - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() - { - return [ - FormEvents::PRE_SET_DATA => 'preSetData', - FormEvents::SUBMIT => 'submit', - ]; - } - /** * @param FormEvent $event */ diff --git a/src/Form/EventListener/TranslationsListener.php b/src/Form/EventListener/TranslationsListener.php index f497899..e1b4d10 100644 --- a/src/Form/EventListener/TranslationsListener.php +++ b/src/Form/EventListener/TranslationsListener.php @@ -1,22 +1,18 @@ + */ class TranslationsListener extends KoffI18nListener { /** @var FormManipulatorInterface */ @@ -30,17 +26,6 @@ public function __construct(FormManipulatorInterface $formManipulator) $this->formManipulator = $formManipulator; } - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() - { - return [ - FormEvents::PRE_SET_DATA => 'preSetData', - FormEvents::SUBMIT => 'submit', - ]; - } - /** * @param FormEvent $event */ diff --git a/src/Form/Manipulator/DefaultManipulator.php b/src/Form/Manipulator/FormManipulator.php similarity index 72% rename from src/Form/Manipulator/DefaultManipulator.php rename to src/Form/Manipulator/FormManipulator.php index 7030fea..7318d82 100644 --- a/src/Form/Manipulator/DefaultManipulator.php +++ b/src/Form/Manipulator/FormManipulator.php @@ -3,7 +3,7 @@ namespace Koff\Bundle\I18nFormBundle\Form\Manipulator; use Doctrine\Common\Util\ClassUtils; -use Koff\Bundle\I18nFormBundle\ObjectInfo\ObjectInfoInterface; +use Koff\Bundle\I18nFormBundle\Extractor\FieldsExtractorInterface; use Symfony\Component\Form\FormInterface; /** @@ -12,21 +12,21 @@ * @author David ALLIX * @author Sadicov Vladimir */ -class DefaultManipulator implements FormManipulatorInterface +class FormManipulator implements FormManipulatorInterface { - /** @var ObjectInfoInterface */ - private $objectInfo; + /** @var FieldsExtractorInterface */ + private $fieldsExtractor; /** @var array */ private $globalExcludedFields; /** - * @param ObjectInfoInterface $objectInfo - * @param array $globalExcludedFields + * @param FieldsExtractorInterface $fieldsExtractor + * @param array $globalExcludedFields */ - public function __construct(ObjectInfoInterface $objectInfo, array $globalExcludedFields = []) + public function __construct(FieldsExtractorInterface $fieldsExtractor, array $globalExcludedFields = []) { - $this->objectInfo = $objectInfo; + $this->fieldsExtractor = $fieldsExtractor; $this->globalExcludedFields = $globalExcludedFields; } @@ -39,7 +39,7 @@ public function getFieldsConfig(FormInterface $form) $formOptions = $form->getConfig()->getOptions(); $formFields = $formOptions['fields']; - $objectFields = $this->objectInfo->getFieldsConfig($class); + $objectFields = $this->fieldsExtractor->getFieldsConfig($class); $objectFields = $this->filterObjectFields($objectFields, $formOptions['excluded_fields']); if (empty($formFields)) { @@ -49,9 +49,13 @@ public function getFieldsConfig(FormInterface $form) $this->checkUnknownFields($formFields, $objectFields, $class); $fieldsConfig = $this->filterFields($formFields); - array_walk($fieldsConfig, function (&$v, $k, $d) { - $v += $d[$k]; - }, $objectFields); + array_walk( + $fieldsConfig, + function (&$v, $k, $d) { + $v += $d[$k]; + }, + $objectFields + ); return $fieldsConfig; } @@ -93,7 +97,7 @@ private function getDataClass(FormInterface $form) continue; } - return $this->objectInfo->getAssociationTargetClass($dataClass, $form->getName()); + return $this->fieldsExtractor->getAssociationTargetClass($dataClass, $form->getName()); } } @@ -112,8 +116,11 @@ private function filterObjectFields(array $objectFieldsConfig, array $formExclud private function filterFields($fields) { - return array_filter($fields, function ($v) { - return !(null === $v || (array_key_exists('display', $v) && !$v['display'])); - }); + return array_filter( + $fields, + function ($v) { + return !(null === $v || (array_key_exists('display', $v) && !$v['display'])); + } + ); } } diff --git a/src/Form/Type/TranslationsFormsType.php b/src/Form/Type/TranslationsFormsType.php index 447daa0..c93cebe 100644 --- a/src/Form/Type/TranslationsFormsType.php +++ b/src/Form/Type/TranslationsFormsType.php @@ -13,7 +13,7 @@ namespace Koff\Bundle\I18nFormBundle\Form\Type; use Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsFormsListener; -use Koff\Bundle\I18nFormBundle\Locale\LocaleProviderInterface; +use Koff\Bundle\I18nFormBundle\Provider\LocaleProviderInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; diff --git a/src/Form/Type/TranslationsLocalesSelectorType.php b/src/Form/Type/TranslationsLocalesSelectorType.php index 4a69950..5cea62d 100644 --- a/src/Form/Type/TranslationsLocalesSelectorType.php +++ b/src/Form/Type/TranslationsLocalesSelectorType.php @@ -12,7 +12,7 @@ namespace Koff\Bundle\I18nFormBundle\Form\Type; -use Koff\Bundle\I18nFormBundle\Locale\LocaleProviderInterface; +use Koff\Bundle\I18nFormBundle\Provider\LocaleProviderInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormInterface; diff --git a/src/Form/Type/TranslationsType.php b/src/Form/Type/TranslationsType.php index aa7dda5..24e8638 100644 --- a/src/Form/Type/TranslationsType.php +++ b/src/Form/Type/TranslationsType.php @@ -13,7 +13,7 @@ namespace Koff\Bundle\I18nFormBundle\Form\Type; use Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsListener; -use Koff\Bundle\I18nFormBundle\Locale\LocaleProviderInterface; +use Koff\Bundle\I18nFormBundle\Provider\LocaleProviderInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; diff --git a/src/ObjectInfo/ObjectInfoInterface.php b/src/ObjectInfo/ObjectInfoInterface.php deleted file mode 100644 index 1b3369c..0000000 --- a/src/ObjectInfo/ObjectInfoInterface.php +++ /dev/null @@ -1,10 +0,0 @@ - . Reiss Clothing Ltd. +namespace Koff\Bundle\I18nFormBundle\Provider; + +/** + * Class LocaleProvider. * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * @author Gonzalo Vilaseca . Reiss Clothing Ltd. */ - -namespace Koff\Bundle\I18nFormBundle\Locale; - -class DefaultProvider implements LocaleProviderInterface +class LocaleProvider implements LocaleProviderInterface { /** @var array */ protected $locales; @@ -31,10 +27,10 @@ public function __construct(array $locales, $defaultLocale, array $requiredLocal { if (!in_array($defaultLocale, $locales, true)) { if (count($locales)) { - throw new \InvalidArgumentException(sprintf('Default locale `%s` not found within the configured locales `[%s]`. Perhaps you need to add it to your `koff_i18n_form.locales` bundle configuration?', $defaultLocale, implode(',', $locales))); + throw new \InvalidArgumentException(sprintf('Default locale `%s` not found within the configured locales `[%s]`. Perhaps you need to add it to your `i18n_form.locales` bundle configuration?', $defaultLocale, implode(',', $locales))); } - throw new \InvalidArgumentException(sprintf('No locales were configured, but expected at least the default locale `%s`. Perhaps you need to add it to your `koff_i18n_form.locales` bundle configuration?', $defaultLocale)); + throw new \InvalidArgumentException(sprintf('No locales were configured, but expected at least the default locale `%s`. Perhaps you need to add it to your `i18n_form.locales` bundle configuration?', $defaultLocale)); } if (array_diff($requiredLocales, $locales)) { diff --git a/src/Locale/LocaleProviderInterface.php b/src/Provider/LocaleProviderInterface.php similarity index 57% rename from src/Locale/LocaleProviderInterface.php rename to src/Provider/LocaleProviderInterface.php index 72ecab0..f859bd2 100644 --- a/src/Locale/LocaleProviderInterface.php +++ b/src/Provider/LocaleProviderInterface.php @@ -1,16 +1,12 @@ . Reiss Clothing Ltd. +namespace Koff\Bundle\I18nFormBundle\Provider; + +/** + * Interface LocaleProviderInterface. * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * @author Gonzalo Vilaseca . Reiss Clothing Ltd. */ - -namespace Koff\Bundle\I18nFormBundle\Locale; - interface LocaleProviderInterface { /** diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index e8fd685..35c0491 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -8,7 +8,7 @@ - + @@ -16,12 +16,12 @@ - + - - + + diff --git a/tests/Form/TypeTestCase.php b/tests/Form/TypeTestCase.php index 7dd59b8..902ae30 100644 --- a/tests/Form/TypeTestCase.php +++ b/tests/Form/TypeTestCase.php @@ -21,7 +21,7 @@ abstract class TypeTestCase extends BaseTypeTestCase { - /** @var \Koff\Bundle\I18nFormBundle\Form\Manipulator\DefaultManipulator */ + /** @var \Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulator */ protected $defaultFormManipulator; protected function setUp() @@ -55,9 +55,9 @@ protected function getDefaultFormManipulator() $config = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/../Fixtures/Entity'], true, null, null, false); $entityManager = EntityManager::create(['driver' => 'pdo_sqlite'], $config); - $doctrineInfo = new \Koff\Bundle\I18nFormBundle\ObjectInfo\DoctrineInfo($entityManager); + $doctrineInfo = new \Koff\Bundle\I18nFormBundle\Extractor\DoctrineEntityFieldsExtractor($entityManager); - return $this->defaultFormManipulator = new \Koff\Bundle\I18nFormBundle\Form\Manipulator\DefaultManipulator($doctrineInfo, ['id', 'locale', 'translatable']); + return $this->defaultFormManipulator = new \Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulator($doctrineInfo, ['id', 'locale', 'translatable']); } protected function getConfiguredAutoFormType() @@ -70,7 +70,7 @@ protected function getConfiguredAutoFormType() protected function getConfiguredTranslationsType($locales, $defaultLocale, $requiredLocales) { $translationsListener = new \Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsListener($this->getDefaultFormManipulator()); - $localProvider = new \Koff\Bundle\I18nFormBundle\Locale\DefaultProvider($locales, $defaultLocale, $requiredLocales); + $localProvider = new \Koff\Bundle\I18nFormBundle\Provider\LocaleProvider($locales, $defaultLocale, $requiredLocales); return new \Koff\Bundle\I18nFormBundle\Form\Type\TranslationsType($translationsListener, $localProvider); } @@ -78,7 +78,7 @@ protected function getConfiguredTranslationsType($locales, $defaultLocale, $requ protected function getConfiguredTranslationsFormsType($locales, $defaultLocale, $requiredLocales) { $translationsFormsListener = new \Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsFormsListener(); - $localProvider = new \Koff\Bundle\I18nFormBundle\Locale\DefaultProvider($locales, $defaultLocale, $requiredLocales); + $localProvider = new \Koff\Bundle\I18nFormBundle\Provider\LocaleProvider($locales, $defaultLocale, $requiredLocales); return new \Koff\Bundle\I18nFormBundle\Form\Type\TranslationsFormsType($translationsFormsListener, $localProvider); } diff --git a/tests/Locale/DefaultProviderTest.php b/tests/Locale/DefaultProviderTest.php index 0a642f6..0f84da4 100644 --- a/tests/Locale/DefaultProviderTest.php +++ b/tests/Locale/DefaultProviderTest.php @@ -12,7 +12,7 @@ namespace Koff\Bundle\I18nFormBundle\Tests\Locale; use PHPUnit\Framework\TestCase; -use Koff\Bundle\I18nFormBundle\Locale\DefaultProvider; +use Koff\Bundle\I18nFormBundle\Provider\LocaleProvider; class DefaultProviderTest extends TestCase { @@ -27,12 +27,12 @@ public function setUp() $this->defaultLocale = 'en'; $this->requiredLocales = ['es', 'en']; - $this->provider = new DefaultProvider($this->locales, $this->defaultLocale, $this->requiredLocales); + $this->provider = new LocaleProvider($this->locales, $this->defaultLocale, $this->requiredLocales); } public function testDefaultLocaleIsInLocales() { - $classname = 'Koff\Bundle\I18nFormBundle\Locale\DefaultProvider'; + $classname = 'Koff\Bundle\I18nFormBundle\Provider\LocaleProvider'; // Get mock, without the constructor being called $mock = $this->getMockBuilder($classname) @@ -53,7 +53,7 @@ public function testDefaultLocaleIsInLocales() public function testRequiredLocaleAreInLocales() { - $classname = 'Koff\Bundle\I18nFormBundle\Locale\DefaultProvider'; + $classname = 'Koff\Bundle\I18nFormBundle\Provider\LocaleProvider'; // Get mock, without the constructor being called $mock = $this->getMockBuilder($classname)