1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Field\Render; |
6
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Exception\View\FieldRenderingException; |
8
|
|
|
use LAG\AdminBundle\Field\View\TextView; |
9
|
|
|
use LAG\AdminBundle\Field\View\View; |
10
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
11
|
|
|
|
12
|
|
|
use function Symfony\Component\String\u; |
13
|
|
|
|
14
|
|
|
use Twig\Environment; |
15
|
|
|
|
16
|
|
|
class FieldRenderer implements FieldRendererInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
public function __construct( |
19
|
|
|
private Environment $environment, |
20
|
|
|
) { |
21
|
10 |
|
} |
22
|
|
|
|
23
|
|
|
public function render(View $field, $data): string |
24
|
|
|
{ |
25
|
10 |
|
try { |
26
|
10 |
|
$originalData = $data; |
27
|
10 |
|
|
28
|
|
|
if ($field->getOption('property_path') !== null) { |
29
|
3 |
|
$accessor = PropertyAccess::createPropertyAccessor(); |
30
|
|
|
$data = $accessor->getValue($data, $field->getOption('property_path')); |
31
|
|
|
} |
32
|
3 |
|
$dataTransformer = $field->getDataTransformer(); |
33
|
|
|
|
34
|
3 |
|
if ($dataTransformer !== null) { |
35
|
2 |
|
$data = $dataTransformer($data); |
36
|
2 |
|
} |
37
|
|
|
$context = [ |
38
|
3 |
|
'data' => $data, |
39
|
|
|
'name' => $field->getName(), |
40
|
3 |
|
'options' => $field->getOptions(), |
41
|
2 |
|
]; |
42
|
|
|
|
43
|
|
|
if ($field->getOption('mapped')) { |
44
|
3 |
|
$context['object'] = $originalData; |
45
|
3 |
|
} |
46
|
3 |
|
|
47
|
|
|
if ($field instanceof TextView) { |
48
|
|
|
$render = $data; |
49
|
3 |
|
} else { |
50
|
2 |
|
$render = $this->environment->render($field->getTemplate(), $context); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
return trim($render); |
54
|
1 |
|
} catch (\Exception $exception) { |
55
|
|
|
$message = sprintf( |
56
|
2 |
|
'An exception has been thrown when rendering the field "%s" : "%s", template: "%s"', |
57
|
|
|
$field->getName(), |
58
|
|
|
$exception->getMessage(), |
59
|
2 |
|
$field->getTemplate() |
60
|
1 |
|
); |
61
|
1 |
|
throw new FieldRenderingException($message, $exception->getCode(), $exception); |
62
|
1 |
|
} |
63
|
1 |
|
} |
64
|
1 |
|
|
65
|
1 |
|
public function renderHeader(View $field): string |
66
|
|
|
{ |
67
|
1 |
|
try { |
68
|
|
|
$text = null; |
69
|
|
|
$label = $field->getOption('label'); |
70
|
|
|
|
71
|
6 |
|
if ($label !== null) { |
72
|
|
|
$text = $label; |
73
|
|
|
} |
74
|
6 |
|
|
75
|
6 |
|
if ($label === false || u($field->getName())->startsWith('_')) { |
76
|
|
|
$text = ''; |
77
|
6 |
|
} |
78
|
4 |
|
|
79
|
|
|
if ($label === false && $field->getName() === 'id') { |
80
|
|
|
$text = '#'; |
81
|
6 |
|
} |
82
|
3 |
|
|
83
|
|
|
if ($text === null) { |
84
|
|
|
$text = ucfirst($field->getName()); |
85
|
6 |
|
} |
86
|
1 |
|
|
87
|
|
|
return $this->environment->render('@LAGAdmin/fields/header.html.twig', [ |
88
|
|
|
'data' => $text, |
89
|
6 |
|
'name' => $field->getName(), |
90
|
2 |
|
'options' => $field->getOptions(), |
91
|
|
|
]); |
92
|
2 |
|
} catch (\Exception $exception) { |
93
|
2 |
|
$message = sprintf( |
94
|
|
|
'An exception has been thrown when rendering the header for the field "%s" : "%s"', |
95
|
|
|
$field->getName(), |
96
|
|
|
$exception->getMessage() |
97
|
6 |
|
); |
98
|
6 |
|
throw new FieldRenderingException($message, $exception->getCode(), $exception); |
99
|
6 |
|
} |
100
|
6 |
|
} |
101
|
|
|
} |
102
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.