FormRenderer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 19
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B renderOption() 0 29 7
1
<?php
2
3
/*
4
 * This file is part of the core-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\CoreBundle\Renderer;
13
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
use WBW\Bundle\CoreBundle\Assets\TranslatedChoiceLabelInterface;
16
use WBW\Library\Sorter\Helper\AlphabeticalTreeNodeHelper;
17
use WBW\Library\Sorter\Model\AlphabeticalTreeNodeInterface;
18
use WBW\Library\Symfony\Assets\ChoiceLabelInterface;
19
20
/**
21
 * Form renderer.
22
 *
23
 * @author webeweb <https://github.com/webeweb>
24
 * @package WBW\Bundle\CoreBundle\Renderer
25
 */
26
class FormRenderer {
27
28
    /**
29
     * Render an option.
30
     *
31
     * @param mixed $option The option.
32
     * @param TranslatorInterface|null $translator The translator service.
33
     * @return string Returns the rendered option.
34
     */
35
    public static function renderOption($option, TranslatorInterface $translator = null): string {
36
37
        if (null === $option) {
38
            return null !== $translator ? $translator->trans("label.empty_selection") : "Empty selection";
39
        }
40
41
        if (true === ($option instanceof TranslatedChoiceLabelInterface)) {
42
            $output = $option->getTranslatedChoiceLabel($translator);
43
        } else if (true === ($option instanceof ChoiceLabelInterface)) {
44
            $output = $option->getChoiceLabel();
45
        } else {
46
            $output = "This option must implements [Translated]ChoiceLabelInterface";
47
        }
48
49
        if (true === ($option instanceof AlphabeticalTreeNodeInterface)) {
50
51
            $level  = AlphabeticalTreeNodeHelper::getLevel($option);
52
            $nbsp   = html_entity_decode("&nbsp;");
53
            $symbol = html_entity_decode(0 === $level ? "&#9472;" : "&#9492;");
54
55
            $output = implode("", [
56
                str_repeat($nbsp, $level * 3),
57
                $symbol,
58
                $nbsp,
59
                $output,
60
            ]);
61
        }
62
63
        return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $output could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
}
66