Completed
Push — master ( 38be02...e1046f )
by Beñat
02:34
created

TwigActionTranslationFilter::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Admin Bundle.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
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 LIN3S\CMSKernel\Infrastructure\Lin3sAdminBundle\Twig;
13
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
/**
17
 * Twig action translation filter.
18
 *
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class TwigActionTranslationFilter extends \Twig_Extension
22
{
23
    /**
24
     * The translator.
25
     *
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param TranslatorInterface $translator The translator
34
     */
35
    public function __construct(TranslatorInterface $translator)
36
    {
37
        $this->translator = $translator;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFilters()
44
    {
45
        return [
46
            'action_translation' => new \Twig_Filter('action_translation', [$this, 'actionTranslation']),
47
        ];
48
    }
49
50
    /**
51
     * Callback of action translation Twig filter that returns resultant array.
52
     *
53
     * @param string $options The action options
54
     *
55
     * @return array|string
56
     */
57
    public function actionTranslation($options)
58
    {
59
        if (!is_array($options)) {
60
            return $this->translator->trans($options, [], 'CmsKernelAdminBridge');
61
        }
62
63
        $resultOptions = [];
64
        foreach ($options as $optionKey => $option) {
65
            $option = null === json_decode($option, true) ? $option : json_decode($option, true);
66
67
            if (is_array($option)) {
68
                foreach ($option as $iterationKey => $iteration) {
69
                    if (is_array($iteration)) {
70
                        foreach ($iteration as $iKey => $i) {
71
                            if (is_array($i)) {
72
                                foreach ($i as $iiKey => $ii) {
73
                                    $resultOptions[$optionKey][$iterationKey][$iKey][$iiKey] = $this->translator->trans($ii, [], 'CmsKernelAdminBridge');
74
                                }
75
                            } else {
76
                                $resultOptions[$optionKey][$iterationKey][$iKey] = $this->translator->trans($i, [], 'CmsKernelAdminBridge');
77
                            }
78
                        }
79
                    } else {
80
                        $resultOptions[$optionKey][$iterationKey] = $this->translator->trans($iteration, [], 'CmsKernelAdminBridge');
81
                    }
82
                }
83
            } else {
84
                $resultOptions[$optionKey] = $this->translator->trans($option, [], 'CmsKernelAdminBridge');
85
            }
86
        }
87
88
        return $resultOptions;
89
    }
90
}
91