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