webeweb /
bootstrap-bundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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_Environment; |
||
| 18 | use Twig_SimpleFunction; |
||
| 19 | use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\LabelTwigExtension; |
||
| 20 | use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\LabelTwigExtensionTrait; |
||
| 21 | use WBW\Library\Core\Argument\StringHelper; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Role label Twig extension. |
||
| 25 | * |
||
| 26 | * @author webeweb <https://github.com/webeweb/> |
||
| 27 | * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility |
||
| 28 | */ |
||
| 29 | class RoleLabelTwigExtension extends AbstractUtilityTwigExtension { |
||
| 30 | |||
| 31 | use LabelTwigExtensionTrait; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Service name. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const SERVICE_NAME = "webeweb.bootstrap.twig.extension.utility.role_label"; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * @param Twig_Environment $twigEnvironment The Twig environment. |
||
| 44 | * @param TranslatorInterface $translator The translator. |
||
| 45 | * @param LabelTwigExtension $extension The label component Twig extension. |
||
| 46 | */ |
||
| 47 | public function __construct(Twig_Environment $twigEnvironment, TranslatorInterface $translator, LabelTwigExtension $extension) { |
||
| 48 | parent::__construct($twigEnvironment, $translator); |
||
| 49 | $this->setLabelTwigExtension($extension); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Apply the color. |
||
| 54 | * |
||
| 55 | * @param string $label The label. |
||
| 56 | * @param string $content The content. |
||
| 57 | * @param string $color The color. |
||
| 58 | * @return string Returns the label with applied color. |
||
| 59 | */ |
||
| 60 | private function applyColor($label, $content, $color) { |
||
| 61 | $searches = ">" . $content; |
||
| 62 | $replaces = " style=\"background-color:" . $color . ";\"" . $searches; |
||
| 63 | return StringHelper::replace($label, [$searches], [$replaces]); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Display a Bootstrap role label. |
||
| 68 | * |
||
| 69 | * @param UserInterface $user The user. |
||
| 70 | * @param array $roleColors The role colors. |
||
| 71 | * @param array $roleTrans The role translations. |
||
| 72 | * @return string Returns the Bootstrap role label. |
||
| 73 | */ |
||
| 74 | public function bootstrapRoleLabelFunction(UserInterface $user = null, array $roleColors = [], array $roleTrans = []) { |
||
| 75 | |||
| 76 | // Check the user. |
||
| 77 | if (null === $user) { |
||
| 78 | return ""; |
||
| 79 | } |
||
| 80 | |||
| 81 | // Initialize the ouptut. |
||
| 82 | $output = []; |
||
| 83 | |||
| 84 | // Handle each role. |
||
| 85 | foreach ($user->getRoles() as $current) { |
||
| 86 | |||
| 87 | // Get the role. |
||
| 88 | $role = true === $current instanceof Role ? $current->getRole() : $current; |
||
| 89 | |||
| 90 | // Initialize the translation. |
||
| 91 | $trans = $role; |
||
| 92 | if (true === array_key_exists($role, $roleTrans)) { |
||
| 93 | $trans = $this->getTranslator()->trans($roleTrans[$role]); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Initialize the label. |
||
| 97 | $label = $this->getLabelTwigExtension()->bootstrapLabelDefaultFunction(["content" => $trans]); |
||
| 98 | if (true === array_key_exists($role, $roleColors)) { |
||
| 99 | $label = $this->applyColor($label, $trans, $roleColors[$role]); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Add the label. |
||
| 103 | $output[] = $label; |
||
| 104 | } |
||
| 105 | |||
| 106 | // Return the output. |
||
| 107 | return implode(" ", $output); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the Twig functions. |
||
| 112 | * |
||
| 113 | * @return array Returns the Twig functions. |
||
| 114 | */ |
||
| 115 | public function getFunctions() { |
||
| 116 | return [ |
||
| 117 | new Twig_SimpleFunction("bootstrapRoleLabel", [$this, "bootstrapRoleLabelFunction"], ["is_safe" => ["html"]]), |
||
|
0 ignored issues
–
show
|
|||
| 118 | ]; |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.