Completed
Push — master ( aae44d...5e2102 )
by WEBEWEB
07:32
created

FormRenderer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 37
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B render() 0 26 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\Form\Renderer;
13
14
use Symfony\Component\Translation\TranslatorInterface;
15
use WBW\Library\Core\Sorting\AlphabeticalTreeNodeHelper;
16
use WBW\Library\Core\Sorting\AlphabeticalTreeNodeInterface;
17
18
/**
19
 * Form renderer.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Bundle\CoreBundle\Form\Renderer
23
 */
24
class FormRenderer {
25
26
    /**
27
     * Render a select option.
28
     *
29
     * @param mixed $option The option.
30
     * @param TranslatorInterface $translator The translator service.
31
     * @return string Returns the label.
32
     */
33
    public static function render($option, TranslatorInterface $translator = null) {
34
35
        // Check the option.
36
        if (null === $option) {
37
            return null !== $translator ? $translator->trans("label.empty_selection") : "Empty selection";
38
        }
39
40
        // Check the implementation.
41
        if (true === ($option instanceof ChoiceRendererInterface)) {
42
            $output = $option->getChoiceLabel();
43
        } else if (true === ($option instanceof TranslatedChoiceRendererInterface)) {
44
            $output = $option->getTranslatedChoiceLabel($translator);
45
        } else {
46
            $output = "FormRendererInterface not implemented by this object";
47
        }
48
49
        if (true === ($option instanceof AlphabeticalTreeNodeInterface)) {
50
            $multiplier = AlphabeticalTreeNodeHelper::getLevel($option);
51
            $nbsp       = html_entity_decode("&nbsp;");
52
            $symbol     = html_entity_decode(0 === $multiplier ? "&#9472;" : "&#9492;");
53
            $output     = implode("", [str_repeat($nbsp, $multiplier * 3), $symbol, $nbsp, $output]);
54
        }
55
56
        // Return the output.
57
        return $output;
58
    }
59
60
}
61