Completed
Push — master ( 7d0ef3...14c63c )
by WEBEWEB
03:01
created

bootstrapRoleLabelFunction()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.7377
c 0
b 0
f 0
cc 6
nc 10
nop 3
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\BootstrapBundle\Twig\Extension\Utility;
13
14
use Symfony\Component\Security\Core\Role\Role;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Component\Translation\TranslatorInterface;
17
use Twig_SimpleFunction;
18
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\LabelTwigExtension;
19
use WBW\Library\Core\Utility\Argument\StringUtility;
20
21
/**
22
 * Role label Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility
26
 */
27
class RoleLabelTwigExtension extends AbstractUtilityTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.bootstrapbundle.twig.extension.utility.rolelabel";
35
36
    /**
37
     * Extension.
38
     *
39
     * @var LabelTwigExtension
40
     */
41
    private $extension;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param TranslatorInterface $translator The translator.
47
     * @param LabelTwigExtension $extension The label component Twig extension.
48
     */
49
    public function __construct(TranslatorInterface $translator, LabelTwigExtension $extension) {
50
        parent::__construct($translator);
51
        $this->setExtension($extension);
52
    }
53
54
    /**
55
     * Apply the color.
56
     *
57
     * @param string $label The label.
58
     * @param string $content The content.
59
     * @param string $color The color.
60
     * @return string Returns the label with applied color.
61
     */
62
    private function applyColor($label, $content, $color) {
63
        $searches = ">" . $content;
64
        $replaces = " style=\"background-color:" . $color . ";\"" . $searches;
65
        return StringUtility::replace($label, [$searches], [$replaces]);
66
    }
67
68
    /**
69
     * Display a Bootstrap role label.
70
     *
71
     * @param UserInterface $user The user.
72
     * @param array $roleColors The role colors.
73
     * @param array $roleTrans The role translations.
74
     * @return string Returns the Bootstrap role label.
75
     */
76
    public function bootstrapRoleLabelFunction(UserInterface $user = null, array $roleColors = [], array $roleTrans = []) {
77
78
        // Check the user.
79
        if (null === $user) {
80
            return "";
81
        }
82
83
        // Initialize the ouptut.
84
        $output = [];
85
86
        // Handle each role.
87
        foreach ($user->getRoles() as $current) {
88
89
            // Get the role.
90
            $role = true === $current instanceof Role ? $current->getRole() : $current;
91
92
            // Initialize the translation.
93
            $trans = $role;
94
            if (true === array_key_exists($role, $roleTrans)) {
95
                $trans = $this->getTranslator()->trans($roleTrans[$role]);
96
            }
97
98
            // Initialize the label.
99
            $label = $this->getExtension()->bootstrapLabelDefaultFunction(["content" => $trans]);
100
            if (true === array_key_exists($role, $roleColors)) {
101
                $label = $this->applyColor($label, $trans, $roleColors[$role]);
102
            }
103
104
            // Add the label.
105
            $output[] = $label;
106
        }
107
108
        // Return the output.
109
        return implode(" ", $output);
110
    }
111
112
    /**
113
     * Get the Twig functions.
114
     *
115
     * @return array Returns the Twig functions.
116
     */
117
    public function getFunctions() {
118
        return [
119
            new Twig_SimpleFunction("bootstrapRoleLabel", [$this, "bootstrapRoleLabelFunction"], ["is_safe" => ["html"]]),
120
        ];
121
    }
122
123
    /**
124
     * Get the extension.
125
     *
126
     * @return LabelTwigExtension Returns the extension.
127
     */
128
    public function getExtension() {
129
        return $this->extension;
130
    }
131
132
    /**
133
     * Set the extension.
134
     *
135
     * @param LabelTwigExtension $extension The extension.
136
     * @return RoleLabelUtilityTwigExtension Returns this role label utility Twig extension.
137
     */
138
    protected function setExtension(LabelTwigExtension $extension) {
139
        $this->extension = $extension;
140
        return $this;
141
    }
142
143
}
144