|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\MessengerBundle\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
6
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
7
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
8
|
|
|
use Twig\Environment; |
|
9
|
|
|
use Yokai\MessengerBundle\Exception\BadMethodCallException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Yann Eugoné <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ContentBuilder |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EngineInterface|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $templating; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Environment|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $twig; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var TranslatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $translator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $defaults; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $options; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param TranslatorInterface $translator |
|
43
|
|
|
* @param array $defaults |
|
44
|
|
|
* @param EngineInterface|null $templating |
|
45
|
|
|
* @param Environment|null $twig |
|
46
|
|
|
*/ |
|
47
|
13 |
|
public function __construct( |
|
48
|
|
|
TranslatorInterface $translator, |
|
49
|
|
|
array $defaults, |
|
50
|
|
|
EngineInterface $templating = null, |
|
51
|
|
|
Environment $twig = null |
|
52
|
|
|
) { |
|
53
|
13 |
|
$this->twig = $twig; |
|
54
|
13 |
|
$this->templating = $templating; |
|
55
|
13 |
|
$this->translator = $translator; |
|
56
|
13 |
|
$this->defaults = $defaults; |
|
57
|
13 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $options |
|
61
|
|
|
*/ |
|
62
|
11 |
|
public function configure($options) |
|
63
|
|
|
{ |
|
64
|
11 |
|
$resolver = (new OptionsResolver) |
|
65
|
11 |
|
->setDefault('template', '') |
|
66
|
11 |
|
->setAllowedTypes('template', ['string', 'null', 'boolean']) |
|
67
|
|
|
|
|
68
|
11 |
|
->setDefault('subject', '') |
|
69
|
11 |
|
->setAllowedTypes('subject', ['string', 'null', 'boolean']) |
|
70
|
|
|
|
|
71
|
11 |
|
->setDefault('translation_catalog', '') |
|
72
|
11 |
|
->setAllowedTypes('translation_catalog', 'string') |
|
73
|
|
|
|
|
74
|
11 |
|
->setDefault('subject_parameters', []) |
|
75
|
11 |
|
->setAllowedTypes('subject_parameters', 'array') |
|
76
|
11 |
|
->setNormalizer('subject_parameters', function ($opts, $value) { |
|
77
|
11 |
|
return array_values((array) $value); |
|
78
|
11 |
|
}) |
|
79
|
|
|
|
|
80
|
11 |
|
->setDefault('template_parameters', []) |
|
81
|
11 |
|
->setAllowedTypes('template_parameters', 'array') |
|
82
|
11 |
|
->setNormalizer('template_parameters', function ($opts, $value) { |
|
83
|
11 |
|
return array_values((array) $value); |
|
84
|
11 |
|
}) |
|
85
|
|
|
|
|
86
|
11 |
|
->setDefault('template_vars', []) |
|
87
|
11 |
|
->setAllowedTypes('template_vars', 'array') |
|
88
|
11 |
|
->setNormalizer('template_vars', function ($opts, $value) { |
|
89
|
11 |
|
return (array) $value; |
|
90
|
11 |
|
}) |
|
91
|
|
|
; |
|
92
|
|
|
|
|
93
|
11 |
|
foreach ($resolver->getDefinedOptions() as $option) { |
|
94
|
11 |
|
if (isset($this->defaults[$option])) { |
|
95
|
11 |
|
$resolver->setDefault($option, $this->defaults[$option]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
11 |
|
$options = array_intersect_key( |
|
100
|
11 |
|
$options, |
|
101
|
11 |
|
array_flip($resolver->getDefinedOptions()) |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
11 |
|
$this->options = $resolver->resolve($options); |
|
105
|
11 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $parameters |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
7 |
|
public function getSubject(array $parameters) |
|
113
|
|
|
{ |
|
114
|
7 |
|
if (null === $this->options) { |
|
115
|
1 |
|
throw BadMethodCallException::createMissingCall( |
|
116
|
1 |
|
__CLASS__.'::configure', |
|
117
|
1 |
|
__METHOD__ |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
6 |
|
if (!$this->options['subject']) { |
|
122
|
3 |
|
return ''; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
3 |
|
return $this->translator->trans( |
|
126
|
3 |
|
$this->options['subject'], |
|
127
|
3 |
|
array_intersect_key($parameters, array_flip($this->options['subject_parameters'])), |
|
128
|
3 |
|
$this->options['translation_catalog'] |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $parameters |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
7 |
|
public function getBody(array $parameters) |
|
138
|
|
|
{ |
|
139
|
7 |
|
if (null === $this->options) { |
|
140
|
1 |
|
throw BadMethodCallException::createMissingCall( |
|
141
|
1 |
|
__CLASS__.'::configure', |
|
142
|
1 |
|
__METHOD__ |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
6 |
|
if (!$this->options['template']) { |
|
147
|
3 |
|
return ''; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
3 |
|
$template = strtr( |
|
151
|
3 |
|
$this->options['template'], |
|
152
|
3 |
|
array_intersect_key($parameters, array_flip($this->options['template_parameters'])) |
|
153
|
|
|
); |
|
154
|
3 |
|
$variables = array_merge($parameters, $this->options['template_vars']); |
|
155
|
|
|
|
|
156
|
3 |
|
if ($this->twig !== null) { |
|
157
|
3 |
|
return $this->twig->render($template, $variables); |
|
158
|
|
|
} elseif ($this->templating !== null) { |
|
159
|
|
|
return $this->templating->render($template, $variables); |
|
160
|
|
|
} else { |
|
161
|
|
|
throw new \LogicException( |
|
162
|
|
|
sprintf('You can not use %s without "symfony/templating" or "symfony/twig-bundle".', __CLASS__) |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|