|
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\Configuration\Form\Field\Validation\Validation; |
|
18
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
|
19
|
|
|
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait; |
|
20
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
21
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility; |
|
22
|
|
|
use TYPO3\CMS\Extbase\Error\Message; |
|
23
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
|
24
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; |
|
25
|
|
|
|
|
26
|
|
|
class MessageService implements SingletonInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use ExtendedFacadeInstanceTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Dispatcher |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $signalSlotDispatcher; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* This function will go through all errors, warnings and notices and check |
|
37
|
|
|
* if they are instances of `FormzMessageInterface`. If not, they are |
|
38
|
|
|
* converted in order to have more informations that are needed later. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Result $result |
|
41
|
|
|
* @param Validation $validation |
|
42
|
|
|
* @return Result |
|
43
|
|
|
*/ |
|
44
|
|
|
public function sanitizeValidatorResult(Result $result, Validation $validation) |
|
45
|
|
|
{ |
|
46
|
|
|
$newResult = new Result; |
|
47
|
|
|
|
|
48
|
|
|
$this->sanitizeValidatorResultMessages('error', $result->getFlattenedErrors(), $newResult, $validation); |
|
49
|
|
|
$this->sanitizeValidatorResultMessages('warning', $result->getFlattenedWarnings(), $newResult, $validation); |
|
50
|
|
|
$this->sanitizeValidatorResultMessages('notice', $result->getFlattenedNotices(), $newResult, $validation); |
|
51
|
|
|
|
|
52
|
|
|
return $newResult; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $type |
|
57
|
|
|
* @param array $messages |
|
58
|
|
|
* @param Result $newResult |
|
59
|
|
|
* @param Validation $validation |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function sanitizeValidatorResultMessages($type, array $messages, Result $newResult, Validation $validation) |
|
62
|
|
|
{ |
|
63
|
|
|
$addMethod = 'add' . ucfirst($type); |
|
64
|
|
|
$objectType = 'Romm\\Formz\\Error\\' . ucfirst($type); |
|
65
|
|
|
$unknownCounter = 0; |
|
66
|
|
|
|
|
67
|
|
|
/** @var Message[] $messagesList */ |
|
68
|
|
|
foreach ($messages as $path => $messagesList) { |
|
69
|
|
|
foreach ($messagesList as $message) { |
|
70
|
|
|
if (false === $message instanceof FormzMessageInterface) { |
|
71
|
|
|
$message = new $objectType( |
|
72
|
|
|
$message->getMessage(), |
|
73
|
|
|
$message->getCode(), |
|
74
|
|
|
$validation->getValidationName(), |
|
75
|
|
|
'unknown-' . ++$unknownCounter, |
|
76
|
|
|
$message->getArguments(), |
|
77
|
|
|
$message->getTitle() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (empty($path)) { |
|
82
|
|
|
$newResult->$addMethod($message); |
|
83
|
|
|
} else { |
|
84
|
|
|
$newResult->forProperty($path)->$addMethod($message); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $message |
|
92
|
|
|
* @param array $arguments |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function parseMessageArray(array $message, array $arguments) |
|
96
|
|
|
{ |
|
97
|
|
|
$result = (isset($message['value']) && $message['value'] !== '') |
|
98
|
|
|
? vsprintf($message['value'], $arguments) |
|
99
|
|
|
: ContextService::get()->translate($message['key'], $message['extension'], $arguments); |
|
100
|
|
|
|
|
101
|
|
|
list($result) = $this->signalSlotDispatcher->dispatch( |
|
102
|
|
|
__CLASS__, |
|
103
|
|
|
'getMessage', |
|
104
|
|
|
[$result, $message, $arguments] |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
return (string)$result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Will return an array by considering the supported messages, and filling |
|
112
|
|
|
* the supported ones with the given values. |
|
113
|
|
|
* |
|
114
|
|
|
* @param FormzMessage[] $messages |
|
115
|
|
|
* @param array $supportedMessages |
|
116
|
|
|
* @param bool $canCreateNewMessages |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function filterMessages(array $messages, array $supportedMessages, $canCreateNewMessages = false) |
|
120
|
|
|
{ |
|
121
|
|
|
// Adding the keys `value` and `extension` to the messages, only if it is missing. |
|
122
|
|
|
$addValueToArray = function (array &$a) { |
|
123
|
|
|
foreach ($a as $k => $v) { |
|
124
|
|
|
if (false === isset($v['value'])) { |
|
125
|
|
|
$a[$k]['value'] = ''; |
|
126
|
|
|
} |
|
127
|
|
|
if (false === isset($v['extension'])) { |
|
128
|
|
|
$a[$k]['extension'] = ''; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $a; |
|
133
|
|
|
}; |
|
134
|
|
|
|
|
135
|
|
|
$messagesArray = []; |
|
136
|
|
|
foreach ($messages as $key => $message) { |
|
137
|
|
|
if ($message instanceof FormzMessage) { |
|
138
|
|
|
$message = $message->toArray(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$messagesArray[$key] = $message; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$addValueToArray($messagesArray); |
|
145
|
|
|
$addValueToArray($supportedMessages); |
|
146
|
|
|
|
|
147
|
|
|
$messagesResult = $supportedMessages; |
|
148
|
|
|
|
|
149
|
|
|
ArrayUtility::mergeRecursiveWithOverrule( |
|
150
|
|
|
$messagesResult, |
|
151
|
|
|
$messagesArray, |
|
152
|
|
|
(bool)$canCreateNewMessages |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
return $messagesResult; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param Dispatcher $signalSlotDispatcher |
|
160
|
|
|
*/ |
|
161
|
|
|
public function injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->signalSlotDispatcher = $signalSlotDispatcher; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|