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(" "); |
53
|
|
|
$symbol = html_entity_decode(0 === $level ? "─" : "└"); |
54
|
|
|
|
55
|
|
|
$output = implode("", [ |
56
|
|
|
str_repeat($nbsp, $level * 3), |
57
|
|
|
$symbol, |
58
|
|
|
$nbsp, |
59
|
|
|
$output, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $output; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|