Issues (6)

src/Form/EventListener/TranslationsListener.php (2 issues)

Labels
1
<?php
2
3
namespace Koff\Bundle\I18nFormBundle\Form\EventListener;
4
5
use Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulatorInterface;
6
use Koff\Bundle\I18nFormBundle\Form\Type\AutoFormType;
7
use Symfony\Component\Form\FormEvent;
8
use Symfony\Component\Form\FormInterface;
9
10
/**
11
 * Class TranslationsListener.
12
 *
13
 * @author David ALLIX
14
 * @author Sadicov Vladimir <[email protected]>
15
 */
16
class TranslationsListener extends KoffI18nListener
17
{
18
    /** @var FormManipulatorInterface */
19
    private $formManipulator;
20
21
    public function __construct(FormManipulatorInterface $formManipulator)
22
    {
23
        $this->formManipulator = $formManipulator;
24
    }
25
26
    public function preSetData(FormEvent $event)
27
    {
28
        $form = $event->getForm();
29
        $formOptions = $form->getConfig()->getOptions();
30
31
        $fieldsOptions = $this->getFieldsOptions($form, $formOptions);
32
        $translationClass = $this->getTranslationClass($form->getParent());
0 ignored issues
show
It seems like $form->getParent() can also be of type null; however, parameter $form of Koff\Bundle\I18nFormBund...::getTranslationClass() does only seem to accept Symfony\Component\Form\FormInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $translationClass = $this->getTranslationClass(/** @scrutinizer ignore-type */ $form->getParent());
Loading history...
33
34
        foreach ($formOptions['locales'] as $locale) {
35
            if (isset($fieldsOptions[$locale])) {
36
                $form->add($locale, AutoFormType::class, [
37
                    'data_class' => $translationClass,
38
                    'required' => \in_array($locale, $formOptions['required_locales'], true),
39
                    'block_name' => ('field' === $formOptions['theming_granularity']) ? 'locale' : null,
40
                    'fields' => $fieldsOptions[$locale],
41
                    'excluded_fields' => $formOptions['excluded_fields'],
42
                ]);
43
            }
44
        }
45
    }
46
47
    private function getTranslationClass(FormInterface $form): string
48
    {
49
        do {
50
            $translatableClass = $form->getConfig()->getDataClass();
51
        } while ((null === $translatableClass) && $form->getConfig()->getVirtual() && ($form = $form->getParent()));
0 ignored issues
show
The method getVirtual() does not exist on Symfony\Component\Form\FormConfigInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        } while ((null === $translatableClass) && $form->getConfig()->/** @scrutinizer ignore-call */ getVirtual() && ($form = $form->getParent()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
53
        // Knp
54
        if (method_exists($translatableClass, 'getTranslationEntityClass')) {
55
            return $translatableClass::getTranslationEntityClass();
56
        }
57
58
        // Gedmo
59
        if (method_exists($translatableClass, 'getTranslationClass')) {
60
            return $translatableClass::getTranslationClass();
61
        }
62
63
        return $translatableClass.'Translation';
64
    }
65
66
    public function getFieldsOptions(FormInterface $form, array $formOptions): array
67
    {
68
        $fieldsOptions = array_fill_keys($formOptions['locales'], $this->formManipulator->getFieldsConfig($form));
69
70
        foreach ($fieldsOptions as $locale => &$field) {
71
            array_walk($field, function (&$v, $k, $l) {
72
                if (\array_key_exists('locale_options', $v) && \array_key_exists($l, $v['locale_options'])) {
73
                    $lo = $v['locale_options'];
74
                    unset($v['locale_options']);
75
76
                    $v = $lo[$l] + $v;
77
                }
78
            }, $locale);
79
        }
80
81
        return $fieldsOptions;
82
    }
83
}
84