Completed
Push — master ( 341a7d...fd15d4 )
by WEBEWEB
02:17
created

FormRenderer::render()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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