Completed
Push — master ( 84aa69...d17e3c )
by WEBEWEB
02:03
created

FormRenderer::render()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
rs 6.7272
cc 7
eloc 15
nc 8
nop 2
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
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\Library\Core\Form\Renderer;
13
14
use WBW\Library\Core\Sort\Tree\Alphabetical\AlphabeticalTreeSort;
15
use WBW\Library\Core\Sort\Tree\Alphabetical\AlphabeticalTreeSortInterface;
16
17
/**
18
 * Form renderer.
19
 *
20
 * @author NdC/WBW <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Form\Renderer
22
 * @final
23
 */
24
final 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, $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 AlphabeticalTreeSortInterface)) {
50
            $multiplier = AlphabeticalTreeSort::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