|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 Formz project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Service; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Field\Validation\Message as FormzMessage; |
|
17
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
|
18
|
|
|
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait; |
|
19
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
20
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility; |
|
21
|
|
|
use TYPO3\CMS\Extbase\Error\Message; |
|
22
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; |
|
23
|
|
|
|
|
24
|
|
|
class MessageService implements SingletonInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ExtendedFacadeInstanceTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Dispatcher |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $signalSlotDispatcher; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns the validation name of a message: if it is an instance of |
|
35
|
|
|
* `FormzMessageInterface`, we can fetch it, otherwise `unknown` is |
|
36
|
|
|
* returned. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Message $message |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getMessageValidationName(Message $message) |
|
42
|
|
|
{ |
|
43
|
|
|
return ($message instanceof FormzMessageInterface) |
|
44
|
|
|
? $message->getValidationName() |
|
45
|
|
|
: 'unknown'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the key of a message: if it is an instance of |
|
50
|
|
|
* `FormzMessageInterface`, we can fetch it, otherwise `unknown` is |
|
51
|
|
|
* returned. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Message $message |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getMessageKey(Message $message) |
|
57
|
|
|
{ |
|
58
|
|
|
return ($message instanceof FormzMessageInterface) |
|
59
|
|
|
? $message->getMessageKey() |
|
60
|
|
|
: 'unknown'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $message |
|
65
|
|
|
* @param array $arguments |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function parseMessageArray(array $message, array $arguments) |
|
69
|
|
|
{ |
|
70
|
|
|
$result = (isset($message['value']) && $message['value'] !== '') |
|
71
|
|
|
? vsprintf($message['value'], $arguments) |
|
72
|
|
|
: ContextService::get()->translate($message['key'], $message['extension'], $arguments); |
|
73
|
|
|
|
|
74
|
|
|
list($result) = $this->signalSlotDispatcher->dispatch( |
|
75
|
|
|
__CLASS__, |
|
76
|
|
|
'getMessage', |
|
77
|
|
|
[$result, $message, $arguments] |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
return (string)$result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Will return an array by considering the supported messages, and filling |
|
85
|
|
|
* the supported ones with the given values. |
|
86
|
|
|
* |
|
87
|
|
|
* @param FormzMessage[] $messages |
|
88
|
|
|
* @param array $supportedMessages |
|
89
|
|
|
* @param bool $canCreateNewMessages |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function filterMessages(array $messages, array $supportedMessages, $canCreateNewMessages = false) |
|
93
|
|
|
{ |
|
94
|
|
|
// Adding the keys `value` and `extension` to the messages, only if it is missing. |
|
95
|
|
|
$addValueToArray = function (array &$a) { |
|
96
|
|
|
foreach ($a as $k => $v) { |
|
97
|
|
|
if (false === isset($v['value'])) { |
|
98
|
|
|
$a[$k]['value'] = ''; |
|
99
|
|
|
} |
|
100
|
|
|
if (false === isset($v['extension'])) { |
|
101
|
|
|
$a[$k]['extension'] = ''; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $a; |
|
106
|
|
|
}; |
|
107
|
|
|
|
|
108
|
|
|
$messagesArray = []; |
|
109
|
|
|
foreach ($messages as $key => $message) { |
|
110
|
|
|
if ($message instanceof FormzMessage) { |
|
111
|
|
|
$message = $message->toArray(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$messagesArray[$key] = $message; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$addValueToArray($messagesArray); |
|
118
|
|
|
$addValueToArray($supportedMessages); |
|
119
|
|
|
|
|
120
|
|
|
$messagesResult = $supportedMessages; |
|
121
|
|
|
|
|
122
|
|
|
ArrayUtility::mergeRecursiveWithOverrule( |
|
123
|
|
|
$messagesResult, |
|
124
|
|
|
$messagesArray, |
|
125
|
|
|
(bool)$canCreateNewMessages |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
return $messagesResult; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param Dispatcher $signalSlotDispatcher |
|
133
|
|
|
*/ |
|
134
|
|
|
public function injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->signalSlotDispatcher = $signalSlotDispatcher; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|