LocaleCodeType::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Locale\Form\Type;
13
14
use Lug\Component\Locale\Model\LocaleInterface;
15
use Lug\Component\Resource\Repository\RepositoryInterface;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\Extension\Core\Type\LocaleType as LocaleForm;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\FormView;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class LocaleCodeType extends AbstractType
26
{
27
    /**
28
     * @var RepositoryInterface
29
     */
30
    private $localeRepository;
31
32
    /**
33
     * @param RepositoryInterface $localeRepository
34
     */
35 6
    public function __construct(RepositoryInterface $localeRepository)
36
    {
37 6
        $this->localeRepository = $localeRepository;
38 6
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function buildView(FormView $view, FormInterface $form, array $options)
44
    {
45 3
        $locales = array_flip(array_map(function (LocaleInterface $locale) {
46 2
            return (string) $locale->getCode();
47 3
        }, $this->localeRepository->findAll()));
48
49 3
        foreach ($view->vars['choices'] as $index => $choiceView) {
50 3
            if ($view->vars['data'] !== $choiceView->data && isset($locales[$choiceView->data])) {
51 1
                unset($view->vars['choices'][$index]);
52 1
            }
53 3
        }
54 3
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3
    public function configureOptions(OptionsResolver $resolver)
60
    {
61 3
        $resolver->setDefaults([
62 3
            'placeholder'     => '',
63 3
            'invalid_message' => 'lug.locale.code.invalid',
64 3
        ]);
65 3
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function getParent()
71
    {
72 3
        return LocaleForm::class;
73
    }
74
}
75