Completed
Push — master ( 6fdefb...81fd90 )
by Vladimir
03:55
created

TranslationsListener::extractLocaleOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of A2lix projects.
5
 *
6
 * (c) David ALLIX
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Koff\Bundle\I18nFormBundle\Form\EventListener;
13
14
use Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulatorInterface;
15
use Koff\Bundle\I18nFormBundle\Form\Type\AutoFormType;
16
use Symfony\Component\Form\FormEvent;
17
use Symfony\Component\Form\FormEvents;
18
use Symfony\Component\Form\FormInterface;
19
20
class TranslationsListener extends KoffI18nListener
21
{
22
    /** @var FormManipulatorInterface */
23
    private $formManipulator;
24
25
    /**
26
     * @param FormManipulatorInterface $formManipulator
27
     */
28
    public function __construct(FormManipulatorInterface $formManipulator)
29
    {
30
        $this->formManipulator = $formManipulator;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public static function getSubscribedEvents()
37
    {
38
        return [
39
            FormEvents::PRE_SET_DATA => 'preSetData',
40
            FormEvents::SUBMIT => 'submit',
41
        ];
42
    }
43
44
    /**
45
     * @param FormEvent $event
46
     */
47
    public function preSetData(FormEvent $event)
48
    {
49
        $form = $event->getForm();
50
        $formOptions = $form->getConfig()->getOptions();
51
52
        $fieldsOptions = $this->getFieldsOptions($form, $formOptions);
53
        $translationClass = $this->getTranslationClass($form->getParent());
54
55
        foreach ($formOptions['locales'] as $locale) {
56
            if (isset($fieldsOptions[$locale])) {
57
                $form->add($locale, AutoFormType::class, [
58
                    'data_class' => $translationClass,
59
                    'required' => in_array($locale, $formOptions['required_locales'], true),
60
                    'block_name' => ('field' === $formOptions['theming_granularity']) ? 'locale' : null,
61
                    'fields' => $fieldsOptions[$locale],
62
                    'excluded_fields' => $formOptions['excluded_fields'],
63
                ]);
64
            }
65
        }
66
    }
67
68
    /**
69
     * @param FormInterface $form
70
     *
71
     * @return string
72
     */
73
    private function getTranslationClass(FormInterface $form)
74
    {
75
        do {
76
            $translatableClass = $form->getConfig()->getDataClass();
77
        } while ((null === $translatableClass) && $form->getConfig()->getVirtual() && ($form = $form->getParent()));
78
79
        // Knp
80
        if (method_exists($translatableClass, 'getTranslationEntityClass')) {
81
            return $translatableClass::getTranslationEntityClass();
82
        }
83
84
        // Gedmo
85
        if (method_exists($translatableClass, 'getTranslationClass')) {
86
            return $translatableClass::getTranslationClass();
87
        }
88
89
        return $translatableClass.'Translation';
90
    }
91
92
    /**
93
     * @param FormInterface $form
94
     * @param array         $formOptions
95
     *
96
     * @return array
97
     */
98
    public function getFieldsOptions(FormInterface $form, array $formOptions)
99
    {
100
        $fieldsOptions = [];
101
        $fieldsConfig = $this->formManipulator->getFieldsConfig($form);
102
103
        foreach ($fieldsConfig as $field => $config) {
104
            $localeOptions = $this->extractLocaleOptions($config);
105
106
            foreach ($formOptions['locales'] as $locale) {
107
                $fieldsOptions[$locale][$field] = (false !== $localeOptions && array_key_exists($locale, $localeOptions)) ? ($localeOptions[$locale] + $config) : $config;
108
            }
109
        }
110
111
        return $fieldsOptions;
112
    }
113
114
    /**
115
     * @param $config
116
     *
117
     * @return array|bool
118
     */
119
    private function extractLocaleOptions(&$config)
120
    {
121
        if (array_key_exists('locale_options', $config)) {
122
            $localeOptions = $config['locale_options'];
123
            unset($config['locale_options']);
124
125
            return $localeOptions;
126
        }
127
128
        return false;
129
    }
130
}
131